Skip to content

Commit 38ebf94

Browse files
authored
Further get rid of global variables (#11)
2 parents 8aa1f79 + a1cc16f commit 38ebf94

File tree

2 files changed

+20
-53
lines changed

2 files changed

+20
-53
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "async-wait-until",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "Waits for a given predicate callback to return a truthy value and resolves",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,70 +31,37 @@ export class TimeoutError extends Error {
3131
}
3232
}
3333

34-
/**
35-
* Unsupported platform error, which is thrown when the module is used
36-
* neither from a web browser nor from Node.js
37-
* @public
38-
* @class
39-
* @exception
40-
* @category Exceptions
41-
*/
42-
export class UnsupportedPlatformError extends Error {
43-
/**
44-
* Creates an UnsupportedPlatformError instance
45-
* @public
46-
*/
47-
constructor() {
48-
super('Unsupported platform');
49-
50-
Object.setPrototypeOf(this, UnsupportedPlatformError.prototype);
51-
}
52-
}
53-
5434
/**
5535
* A utility function for cross-platform type-safe scheduling
5636
* @private
5737
* @returns Returns a proper scheduler instance depending on the current environment
58-
* @throws [[UnsupportedPlatformError]] If the current environment is not supported, e.g. it's neither a web browser nor Node.js
5938
* @throws Error
6039
* @category Utilities
6140
*/
62-
const getScheduler = (): Scheduler => {
63-
if (
64-
// Not a web browser
65-
window == null ||
66-
// Not Node.js
67-
module == null ||
68-
global == null
69-
) {
70-
throw new UnsupportedPlatformError();
71-
}
41+
const getScheduler = (): Scheduler => ({
42+
schedule: (fn, interval) => {
43+
let scheduledTimer: number | NodeJS.Timeout | undefined = undefined;
7244

73-
return {
74-
schedule: (fn, interval) => {
75-
let scheduledTimer: number | NodeJS.Timeout | undefined = undefined;
76-
77-
const cleanUp = (timer: number | NodeJS.Timeout | undefined) => {
78-
if (timer != null) {
79-
(window != null ? window : global).clearTimeout(timer as number);
80-
}
45+
const cleanUp = (timer: number | NodeJS.Timeout | undefined) => {
46+
if (timer != null) {
47+
clearTimeout(timer as number);
48+
}
8149

82-
scheduledTimer = undefined;
83-
};
50+
scheduledTimer = undefined;
51+
};
8452

85-
const iteration = () => {
86-
cleanUp(scheduledTimer);
87-
fn();
88-
};
53+
const iteration = () => {
54+
cleanUp(scheduledTimer);
55+
fn();
56+
};
8957

90-
scheduledTimer = (window != null ? window : global).setTimeout(iteration, interval);
58+
scheduledTimer = setTimeout(iteration, interval);
9159

92-
return {
93-
cancel: () => cleanUp(scheduledTimer),
94-
};
95-
},
96-
};
97-
};
60+
return {
61+
cancel: () => cleanUp(scheduledTimer),
62+
};
63+
},
64+
});
9865

9966
/**
10067
* Delays the execution by the given interval, in milliseconds

0 commit comments

Comments
 (0)