Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #4804 requestIdlePromise broken in react-native #4813

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions src/plugins/utils/utils-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,33 @@ export const PROMISE_RESOLVE_VOID: Promise<void> = Promise.resolve();
* So we have to queue the calls.
*/
let idlePromiseQueue = PROMISE_RESOLVE_VOID;
export function requestIdlePromise(timeout: number | null = null) {
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);
}
});
export function requestIdlePromise(
timeout: number | undefined = undefined
) {
idlePromiseQueue = idlePromiseQueue.then(() => {
/**
* Do not use window.requestIdleCallback
* because some javascript runtimes like react-native,
* do not have a window object, but still have a global
* requestIdleCallback function.
* @link https://github.com/pubkey/rxdb/issues/4804
*/
if (
typeof requestIdleCallback === 'function'
) {
return new Promise<void>(res => {
requestIdleCallback(
() => res(),
{
timeout
}
);
});
} else {
return promiseWait(0);
}
});
return idlePromiseQueue;
}


Expand All @@ -58,10 +70,20 @@ export function requestIdlePromise(timeout: number | null = null) {
* @link https://developer.mozilla.org/de/docs/Web/API/Window/requestIdleCallback
*/
export function requestIdleCallbackIfAvailable(fun: Function): void {
/**
* Do not use window.requestIdleCallback
* because some javascript runtimes like react-native,
* do not have a window object, but still have a global
* requestIdleCallback function.
* @link https://github.com/pubkey/rxdb/issues/4804
*/
if (
typeof window === 'object' &&
(window as any)['requestIdleCallback']
) (window as any)['requestIdleCallback'](fun);
typeof requestIdleCallback === 'function'
) {
requestIdleCallback(() => {
fun();
});
}
}


Expand Down