@bffr/tools
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -5,6 +5,8 @@ export declare type Timer<T> = (fn: () => unknown, duration: number) => T; | ||
export declare function createTimer<T>(set: Timer<T>, clear: (v: T) => unknown): Timer<() => void>; | ||
declare type CreateIdleCallbackProps<T> = IdleRequestOptions & { | ||
export declare type CreateIdleCallbackProps<T> = IdleRequestOptions & { | ||
fallback?: (effect: T, options: IdleRequestOptions) => () => void; | ||
}; | ||
export declare function createIdleCallback<T extends IdleRequestCallback>(effect: T, { fallback, ...options }?: CreateIdleCallbackProps<T>): () => void; | ||
export {}; | ||
export declare namespace createIdleCallback { | ||
var timeout: number; | ||
} |
@@ -14,8 +14,8 @@ "use strict"; | ||
exports.createTimer = createTimer; | ||
const timeout = 4 * 16; | ||
const requestIdleCallback = window?.requestIdleCallback; | ||
function createIdleCallback(effect, { fallback, ...options } = {}) { | ||
options = { timeout: createIdleCallback.timeout, ...options }; | ||
// not supported by all browsers or environments | ||
if (requestIdleCallback != null) { | ||
const id = requestIdleCallback(a => effect?.(a), { timeout, ...options }); | ||
const id = requestIdleCallback(a => effect?.(a), options); | ||
return () => window.cancelIdleCallback(id); | ||
@@ -26,5 +26,5 @@ } | ||
const timer = fallback(effect, options); | ||
// requires the fallbacks to return a timer destructor | ||
// requires the fallbacks to return a timer cancelation | ||
if (typeof timer !== 'function') | ||
throw new Error('createIdleCallback expected a destructor `() => void` from the fallback'); | ||
throw new Error('createIdleCallback expected a cancelation `() => void` be returned from fallback'); | ||
else | ||
@@ -35,3 +35,3 @@ return timer; | ||
else { | ||
const id = setTimeout(effect, timeout); | ||
const id = setTimeout(effect, options.timeout); | ||
return () => clearTimeout(id); | ||
@@ -41,2 +41,3 @@ } | ||
exports.createIdleCallback = createIdleCallback; | ||
createIdleCallback.timeout = 4 * 16; | ||
//# sourceMappingURL=timer.js.map |
{ | ||
"name": "@bffr/tools", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "Hammers, saws, and more pure function tools.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -18,9 +18,8 @@ export type Timer<T> = (fn: () => unknown, duration: number) => T; | ||
const timeout = 4 * 16; | ||
const requestIdleCallback = window?.requestIdleCallback; | ||
type CreateIdleCallbackProps<T> = IdleRequestOptions & { | ||
export type CreateIdleCallbackProps<T> = IdleRequestOptions & { | ||
fallback?: (effect: T, options: IdleRequestOptions) => () => void; | ||
}; | ||
const requestIdleCallback = window?.requestIdleCallback; | ||
export function createIdleCallback<T extends IdleRequestCallback>( | ||
@@ -30,5 +29,7 @@ effect: T, | ||
) { | ||
options = { timeout: createIdleCallback.timeout, ...options }; | ||
// not supported by all browsers or environments | ||
if (requestIdleCallback != null) { | ||
const id = requestIdleCallback(a => effect?.(a), { timeout, ...options }); | ||
const id = requestIdleCallback(a => effect?.(a), options); | ||
return () => window.cancelIdleCallback(id); | ||
@@ -48,5 +49,6 @@ } | ||
else { | ||
const id = setTimeout(effect, timeout); | ||
const id = setTimeout(effect, options.timeout); | ||
return () => clearTimeout(id); | ||
} | ||
} | ||
createIdleCallback.timeout = 4 * 16; |
Sorry, the diff of this file is not supported yet
70559
1481