@horat1us/request-idle-callback
Advanced tools
Comparing version 1.0.3 to 1.0.4
import { IdleCallbackOptions, IdleDeadline } from "./implementation"; | ||
export declare class IdlePromiseCancel extends Error { | ||
readonly reason?: string | undefined; | ||
constructor(reason?: string | undefined); | ||
export interface IdlePromiseCancel extends Error { | ||
readonly reason: string | undefined; | ||
} | ||
export declare class IdlePromise extends Promise<IdleDeadline> { | ||
constructor(options?: IdleCallbackOptions); | ||
readonly cancel: (reason?: string) => void; | ||
export declare const IdlePromiseCancel: (reason?: string | undefined) => IdlePromiseCancel; | ||
export interface IdlePromise extends Promise<IdleDeadline> { | ||
cancel(reason?: string): void; | ||
} | ||
export declare const IdlePromise: (options?: IdleCallbackOptions | undefined) => IdlePromise; |
import { cancelIdleCallback, requestIdleCallback } from "./polyfill"; | ||
export class IdlePromiseCancel extends Error { | ||
constructor(reason) { | ||
super("Idle Promise Cancel"); | ||
this.reason = reason; | ||
} | ||
} | ||
export class IdlePromise extends Promise { | ||
constructor(options) { | ||
let handleCancellation = undefined; | ||
super((resolve, reject) => { | ||
let handle; | ||
handleCancellation = (reason) => { | ||
handle = undefined; | ||
cancelIdleCallback(handle); | ||
handleCancellation = undefined; | ||
reject(new IdlePromiseCancel()); | ||
}; | ||
const idleCallback = (deadLine => { | ||
handle = undefined; | ||
handleCancellation = undefined; | ||
resolve(deadLine); | ||
}); | ||
requestIdleCallback(idleCallback, options); | ||
export const IdlePromiseCancel = (reason) => { | ||
const error = new Error("Idle Promise Cancel"); | ||
Object.defineProperty(error, "reason", { | ||
value: reason, | ||
writable: false, | ||
configurable: false, | ||
enumerable: false, | ||
}); | ||
return error; | ||
}; | ||
export const IdlePromise = (options) => { | ||
let handleCancellation = undefined; | ||
const promise = new Promise((resolve, reject) => { | ||
let handle; | ||
handleCancellation = (reason) => { | ||
handle = undefined; | ||
cancelIdleCallback(handle); | ||
handleCancellation = undefined; | ||
reject(IdlePromiseCancel(reason)); | ||
}; | ||
const idleCallback = (deadLine => { | ||
handle = undefined; | ||
handleCancellation = undefined; | ||
resolve(deadLine); | ||
}); | ||
this.cancel = (reason) => handleCancellation && handleCancellation(reason); | ||
} | ||
} | ||
requestIdleCallback(idleCallback, options); | ||
}); | ||
Object.defineProperty(promise, "cancel", { | ||
value: (reason) => handleCancellation && handleCancellation(reason), | ||
writable: false, | ||
configurable: false, | ||
enumerable: false, | ||
}); | ||
return promise; | ||
}; |
{ | ||
"name": "@horat1us/request-idle-callback", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Request Idle Callback Polyfill", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -53,3 +53,3 @@ # IdleCallback | ||
// Create Promise | ||
const promise = new IdlePromise({ | ||
const promise = IdlePromise({ | ||
timeout: 5000, | ||
@@ -65,3 +65,3 @@ }); | ||
.catch((error) => { | ||
if (error instanceof IdlePromiseCancel) { | ||
if (error.message === "Idle Promise Cancel") { | ||
// handle cancellation | ||
@@ -68,0 +68,0 @@ return; |
14490
27
290