summaryrefslogtreecommitdiffstats
path: root/debian/tests/test_modules/delay/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'debian/tests/test_modules/delay/index.js')
-rw-r--r--debian/tests/test_modules/delay/index.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/debian/tests/test_modules/delay/index.js b/debian/tests/test_modules/delay/index.js
new file mode 100644
index 0000000..7dd2309
--- /dev/null
+++ b/debian/tests/test_modules/delay/index.js
@@ -0,0 +1,72 @@
+'use strict';
+
+// From https://github.com/sindresorhus/random-int/blob/c37741b56f76b9160b0b63dae4e9c64875128146/index.js#L13-L15
+const randomInteger = (minimum, maximum) => Math.floor((Math.random() * (maximum - minimum + 1)) + minimum);
+
+const createAbortError = () => {
+ const error = new Error('Delay aborted');
+ error.name = 'AbortError';
+ return error;
+};
+
+const createDelay = ({clearTimeout: defaultClear, setTimeout: set, willResolve}) => (ms, {value, signal} = {}) => {
+ if (signal && signal.aborted) {
+ return Promise.reject(createAbortError());
+ }
+
+ let timeoutId;
+ let settle;
+ let rejectFn;
+ const clear = defaultClear || clearTimeout;
+
+ const signalListener = () => {
+ clear(timeoutId);
+ rejectFn(createAbortError());
+ };
+
+ const cleanup = () => {
+ if (signal) {
+ signal.removeEventListener('abort', signalListener);
+ }
+ };
+
+ const delayPromise = new Promise((resolve, reject) => {
+ settle = () => {
+ cleanup();
+ if (willResolve) {
+ resolve(value);
+ } else {
+ reject(value);
+ }
+ };
+
+ rejectFn = reject;
+ timeoutId = (set || setTimeout)(settle, ms);
+ });
+
+ if (signal) {
+ signal.addEventListener('abort', signalListener, {once: true});
+ }
+
+ delayPromise.clear = () => {
+ clear(timeoutId);
+ timeoutId = null;
+ settle();
+ };
+
+ return delayPromise;
+};
+
+const createWithTimers = clearAndSet => {
+ const delay = createDelay({...clearAndSet, willResolve: true});
+ delay.reject = createDelay({...clearAndSet, willResolve: false});
+ delay.range = (minimum, maximum, options) => delay(randomInteger(minimum, maximum), options);
+ return delay;
+};
+
+const delay = createWithTimers();
+delay.createWithTimers = createWithTimers;
+
+module.exports = delay;
+// TODO: Remove this for the next major release
+module.exports.default = delay;