-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from /issues/33
Issues/33
- Loading branch information
Showing
5 changed files
with
154 additions
and
31 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
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
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,91 @@ | ||
type Options = { | ||
interval: number; | ||
timeout?: number; | ||
}; | ||
|
||
type State = { | ||
intervalId?: string | number | NodeJS.Timeout; | ||
requestIdleCallbackId?: number; | ||
isRequestIdleCallbackScheduled: boolean; | ||
}; | ||
|
||
type CancelCallback = () => void; | ||
|
||
/** | ||
* `callback` function is invoked when after `interval` msec and environment is idled. | ||
* `callback` which have a `timeout` specified may be called out-of-order if necessary in order to run them before the timeout elapses. | ||
* @param callback | ||
* @param options | ||
* @return {function} return cancelRequestIdleInterval function | ||
*/ | ||
export function requestIdleInterval(callback: () => void, options: Options): CancelCallback { | ||
polyfill(); | ||
|
||
if (options.interval <= options.timeout) { | ||
throw new Error( | ||
`options.timeout should be less than options.interval. Recommended: options.timeout is less than half of options.interval.`, | ||
); | ||
} | ||
|
||
const state: State = { | ||
isRequestIdleCallbackScheduled: false, | ||
}; | ||
|
||
state.intervalId = setInterval(() => { | ||
// Only schedule the rIC if one has not already been set. | ||
if (state.isRequestIdleCallbackScheduled) { | ||
return; | ||
} | ||
|
||
state.isRequestIdleCallbackScheduled = true; | ||
state.requestIdleCallbackId = requestIdleCallback( | ||
() => { | ||
// Reset the boolean so future rICs can be set. | ||
state.isRequestIdleCallbackScheduled = false; | ||
callback(); | ||
}, | ||
{ | ||
timeout: options.timeout, | ||
}, | ||
); | ||
}, options.interval); | ||
|
||
// Return cancel function | ||
return () => { | ||
if (state.intervalId !== undefined) { | ||
clearInterval(state.intervalId); | ||
} | ||
|
||
if (state.requestIdleCallbackId !== undefined) { | ||
cancelIdleCallback(state.requestIdleCallbackId); | ||
} | ||
}; | ||
} | ||
|
||
// https://developer.chrome.com/blog/using-requestidlecallback | ||
function polyfill(): void { | ||
if ('requestIdleCallback' in window) { | ||
return; | ||
} | ||
|
||
(window as Window).requestIdleCallback = function ( | ||
callback: IdleRequestCallback, | ||
options?: IdleRequestOptions, | ||
) { | ||
const start = Date.now(); | ||
const intervalId = setTimeout(function () { | ||
callback({ | ||
didTimeout: false, | ||
timeRemaining: function () { | ||
return Math.max(0, 50 - (Date.now() - start)); | ||
}, | ||
}); | ||
}, 1); | ||
|
||
return intervalId as unknown as number; | ||
}; | ||
|
||
(window as Window).cancelIdleCallback = function (id) { | ||
clearTimeout(id); | ||
}; | ||
} |
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