Skip to content

Commit

Permalink
FIX requestIdlePromise() must run in a queue
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Apr 1, 2023
1 parent 347b6ff commit 51bb3a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# RxDB Changelog

<!-- CHANGELOG NEWEST -->

- FIX `requestIdlePromise()` must run in a queue.
<!-- ADD new changes here! -->

<!-- /CHANGELOG NEWEST -->
Expand Down
32 changes: 20 additions & 12 deletions src/plugins/utils/utils-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@ export const PROMISE_RESOLVE_NULL: Promise<null> = Promise.resolve(null);
export const PROMISE_RESOLVE_VOID: Promise<void> = Promise.resolve();


/**
* If multiple operations wait for an requestIdlePromise
* we do not want them to resolve all at the same time.
* So we have to queue the calls.
*/
let idlePromiseQueue = PROMISE_RESOLVE_VOID;
export function requestIdlePromise(timeout: number | null = null) {
if (
typeof window === 'object' &&
(window as any)['requestIdleCallback']
) {
return new Promise(
res => (window as any)['requestIdleCallback'](res, {
timeout
})
);
} else {
return promiseWait(0);
}
return new Promise(res => {
idlePromiseQueue = idlePromiseQueue.then(() => {
if (
typeof window === 'object' &&
(window as any)['requestIdleCallback']
) {
(window as any)['requestIdleCallback'](res, {
timeout
});
} else {
promiseWait(0).then(res);
}
});
});
}


Expand Down

0 comments on commit 51bb3a6

Please sign in to comment.