-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up subscriptions using requestIdleCallback instead of setTimeout
- Loading branch information
1 parent
b69d348
commit 531a959
Showing
5 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@apollo/client": patch | ||
--- | ||
|
||
Clean up subscriptions using requestIdleCallback instead of setTimeout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { requestIdleCallback } from "../requestIdleCallback"; | ||
|
||
describe("requestIdleCallback", () => { | ||
const originalRequestIdleCallback = window.requestIdleCallback; | ||
|
||
afterAll(() => { | ||
Object.defineProperty(window, "requestIdleCallback", { | ||
value: originalRequestIdleCallback, | ||
}); | ||
}); | ||
|
||
it("should use the window method when possible", () => { | ||
Object.defineProperty(window, "requestIdleCallback", { | ||
value: jest.fn((callback) => callback()), | ||
configurable: true, | ||
}); | ||
|
||
const task = jest.fn(); | ||
requestIdleCallback(task); | ||
expect(task).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should fall back to setTimeout when the window method is not available", () => { | ||
// @ts-expect-error | ||
delete window.requestIdleCallback; | ||
|
||
jest.useFakeTimers(); | ||
|
||
const task = jest.fn(); | ||
requestIdleCallback(task); | ||
|
||
expect(task).not.toHaveBeenCalled(); | ||
|
||
jest.runAllTimers(); | ||
expect(task).toHaveBeenCalled(); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Light polyfill for requestIdleCallback when used in non-browser environments. | ||
*/ | ||
export function requestIdleCallback( | ||
callback: () => void, | ||
options?: IdleRequestOptions | ||
) { | ||
if ( | ||
!Object.prototype.hasOwnProperty.call(window, "requestIdleCallback") || | ||
typeof window.requestIdleCallback === "undefined" | ||
) { | ||
return setTimeout(callback, options?.timeout ?? 0); | ||
} | ||
return window.requestIdleCallback(callback, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters