Using async for polling.

This commit is contained in:
Dessalines 2023-06-14 12:19:33 -04:00
parent 10b78acb3d
commit caf445cae7
2 changed files with 6 additions and 4 deletions

View file

@ -66,7 +66,7 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
if (isBrowser()) {
// On the first load, check the unreads
this.requestNotificationPermission();
await this.fetchUnreads();
this.fetchUnreads();
this.requestNotificationPermission();
document.addEventListener("mouseup", this.handleOutsideMenuClick);
@ -408,7 +408,7 @@ export class Navbar extends Component<NavbarProps, NavbarState> {
return amAdmin() || moderatesS;
}
async fetchUnreads() {
fetchUnreads() {
poll(async () => {
const auth = myAuth();
if (auth) {

View file

@ -1499,6 +1499,8 @@ function sleep<T>(millis: number): Promise<T> {
/**
* Polls / repeatedly runs a promise, every X milliseconds
*/
export function poll(promiseFn: any, millis: number) {
promiseFn().then(sleep(millis).then(() => poll(promiseFn, millis)));
export async function poll(promiseFn: any, millis: number) {
await promiseFn();
await sleep(millis);
return poll(promiseFn, millis);
}