From a1cc16f0ce1e863f735a2eff8279d1ab4cbe29e9 Mon Sep 17 00:00:00 2001 From: Denis Tokarev Date: Sat, 20 Feb 2021 22:15:18 +1100 Subject: [PATCH] Further get rid of global variables --- package.json | 2 +- src/index.ts | 71 ++++++++++++++-------------------------------------- 2 files changed, 20 insertions(+), 53 deletions(-) diff --git a/package.json b/package.json index 7858db7..a98824d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "async-wait-until", - "version": "2.0.2", + "version": "2.0.3", "description": "Waits for a given predicate callback to return a truthy value and resolves", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 951411d..778e569 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,70 +31,37 @@ export class TimeoutError extends Error { } } -/** - * Unsupported platform error, which is thrown when the module is used - * neither from a web browser nor from Node.js - * @public - * @class - * @exception - * @category Exceptions - */ -export class UnsupportedPlatformError extends Error { - /** - * Creates an UnsupportedPlatformError instance - * @public - */ - constructor() { - super('Unsupported platform'); - - Object.setPrototypeOf(this, UnsupportedPlatformError.prototype); - } -} - /** * A utility function for cross-platform type-safe scheduling * @private * @returns Returns a proper scheduler instance depending on the current environment - * @throws [[UnsupportedPlatformError]] If the current environment is not supported, e.g. it's neither a web browser nor Node.js * @throws Error * @category Utilities */ -const getScheduler = (): Scheduler => { - if ( - // Not a web browser - window == null || - // Not Node.js - module == null || - global == null - ) { - throw new UnsupportedPlatformError(); - } +const getScheduler = (): Scheduler => ({ + schedule: (fn, interval) => { + let scheduledTimer: number | NodeJS.Timeout | undefined = undefined; - return { - schedule: (fn, interval) => { - let scheduledTimer: number | NodeJS.Timeout | undefined = undefined; - - const cleanUp = (timer: number | NodeJS.Timeout | undefined) => { - if (timer != null) { - (window != null ? window : global).clearTimeout(timer as number); - } + const cleanUp = (timer: number | NodeJS.Timeout | undefined) => { + if (timer != null) { + clearTimeout(timer as number); + } - scheduledTimer = undefined; - }; + scheduledTimer = undefined; + }; - const iteration = () => { - cleanUp(scheduledTimer); - fn(); - }; + const iteration = () => { + cleanUp(scheduledTimer); + fn(); + }; - scheduledTimer = (window != null ? window : global).setTimeout(iteration, interval); + scheduledTimer = setTimeout(iteration, interval); - return { - cancel: () => cleanUp(scheduledTimer), - }; - }, - }; -}; + return { + cancel: () => cleanUp(scheduledTimer), + }; + }, +}); /** * Delays the execution by the given interval, in milliseconds