Initial commit
This commit is contained in:
107
node_modules/delay/index.d.ts
generated
vendored
Normal file
107
node_modules/delay/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
declare namespace delay {
|
||||
interface ClearablePromise<T> extends Promise<T> {
|
||||
/**
|
||||
Clears the delay and settles the promise.
|
||||
*/
|
||||
clear(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
Minimal subset of `AbortSignal` that delay will use if passed.
|
||||
This avoids a dependency on dom.d.ts.
|
||||
The dom.d.ts `AbortSignal` is compatible with this one.
|
||||
*/
|
||||
interface AbortSignal {
|
||||
readonly aborted: boolean;
|
||||
addEventListener(
|
||||
type: 'abort',
|
||||
listener: () => void,
|
||||
options?: {once?: boolean}
|
||||
): void;
|
||||
removeEventListener(type: 'abort', listener: () => void): void;
|
||||
}
|
||||
|
||||
interface Options {
|
||||
/**
|
||||
An optional AbortSignal to abort the delay.
|
||||
If aborted, the Promise will be rejected with an AbortError.
|
||||
*/
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
}
|
||||
|
||||
type Delay = {
|
||||
/**
|
||||
Create a promise which resolves after the specified `milliseconds`.
|
||||
|
||||
@param milliseconds - Milliseconds to delay the promise.
|
||||
@returns A promise which resolves after the specified `milliseconds`.
|
||||
*/
|
||||
(milliseconds: number, options?: delay.Options): delay.ClearablePromise<void>;
|
||||
|
||||
/**
|
||||
Create a promise which resolves after the specified `milliseconds`.
|
||||
|
||||
@param milliseconds - Milliseconds to delay the promise.
|
||||
@returns A promise which resolves after the specified `milliseconds`.
|
||||
*/
|
||||
<T>(
|
||||
milliseconds: number,
|
||||
options?: delay.Options & {
|
||||
/**
|
||||
Value to resolve in the returned promise.
|
||||
*/
|
||||
value: T;
|
||||
}
|
||||
): delay.ClearablePromise<T>;
|
||||
|
||||
/**
|
||||
Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.
|
||||
|
||||
Useful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead.
|
||||
|
||||
@param minimum - Minimum amount of milliseconds to delay the promise.
|
||||
@param maximum - Maximum amount of milliseconds to delay the promise.
|
||||
@returns A promise which resolves after a random amount of milliseconds between `maximum` and `maximum` has passed.
|
||||
*/
|
||||
range<T>(
|
||||
minimum: number,
|
||||
maximum: number,
|
||||
options?: delay.Options & {
|
||||
/**
|
||||
Value to resolve in the returned promise.
|
||||
*/
|
||||
value: T;
|
||||
}
|
||||
): delay.ClearablePromise<T>;
|
||||
|
||||
// TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 is resolved.
|
||||
/**
|
||||
Create a promise which rejects after the specified `milliseconds`.
|
||||
|
||||
@param milliseconds - Milliseconds to delay the promise.
|
||||
@returns A promise which rejects after the specified `milliseconds`.
|
||||
*/
|
||||
reject(
|
||||
milliseconds: number,
|
||||
options?: delay.Options & {
|
||||
/**
|
||||
Value to reject in the returned promise.
|
||||
*/
|
||||
value?: unknown;
|
||||
}
|
||||
): delay.ClearablePromise<never>;
|
||||
};
|
||||
|
||||
declare const delay: Delay & {
|
||||
// The types are intentionally loose to make it work with both Node.js and browser versions of these methods.
|
||||
createWithTimers(timers: {
|
||||
clearTimeout: (timeoutId: any) => void;
|
||||
setTimeout: (callback: (...args: any[]) => void, milliseconds: number, ...args: any[]) => unknown;
|
||||
}): Delay;
|
||||
|
||||
// TODO: Remove this for the next major release.
|
||||
default: typeof delay;
|
||||
};
|
||||
|
||||
export = delay;
|
||||
72
node_modules/delay/index.js
generated
vendored
Normal file
72
node_modules/delay/index.js
generated
vendored
Normal file
@@ -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;
|
||||
9
node_modules/delay/license
generated
vendored
Normal file
9
node_modules/delay/license
generated
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
54
node_modules/delay/package.json
generated
vendored
Normal file
54
node_modules/delay/package.json
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"name": "delay",
|
||||
"version": "5.0.0",
|
||||
"description": "Delay a promise a specified amount of time",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/delay",
|
||||
"funding": "https://github.com/sponsors/sindresorhus",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "https://sindresorhus.com"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"promise",
|
||||
"resolve",
|
||||
"delay",
|
||||
"defer",
|
||||
"wait",
|
||||
"stall",
|
||||
"timeout",
|
||||
"settimeout",
|
||||
"event",
|
||||
"loop",
|
||||
"next",
|
||||
"tick",
|
||||
"delay",
|
||||
"async",
|
||||
"await",
|
||||
"promises",
|
||||
"bluebird",
|
||||
"threshold",
|
||||
"range",
|
||||
"random"
|
||||
],
|
||||
"devDependencies": {
|
||||
"abort-controller": "^3.0.0",
|
||||
"ava": "1.4.1",
|
||||
"currently-unhandled": "^0.4.1",
|
||||
"in-range": "^1.0.0",
|
||||
"time-span": "^3.0.0",
|
||||
"tsd": "^0.7.1",
|
||||
"xo": "^0.24.0"
|
||||
}
|
||||
}
|
||||
173
node_modules/delay/readme.md
generated
vendored
Normal file
173
node_modules/delay/readme.md
generated
vendored
Normal file
@@ -0,0 +1,173 @@
|
||||
# delay
|
||||
|
||||
> Delay a promise a specified amount of time
|
||||
|
||||
*If you target [Node.js 15](https://medium.com/@nodejs/node-js-v15-0-0-is-here-deb00750f278) or later, you can do `await require('timers/promises').setTimeout(1000)` instead.*
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install delay
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const delay = require('delay');
|
||||
|
||||
(async () => {
|
||||
bar();
|
||||
|
||||
await delay(100);
|
||||
|
||||
// Executed 100 milliseconds later
|
||||
baz();
|
||||
})();
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### delay(milliseconds, options?)
|
||||
|
||||
Create a promise which resolves after the specified `milliseconds`.
|
||||
|
||||
### delay.reject(milliseconds, options?)
|
||||
|
||||
Create a promise which rejects after the specified `milliseconds`.
|
||||
|
||||
### delay.range(minimum, maximum, options?)
|
||||
|
||||
Create a promise which resolves after a random amount of milliseconds between `minimum` and `maximum` has passed.
|
||||
|
||||
Useful for tests and web scraping since they can have unpredictable performance. For example, if you have a test that asserts a method should not take longer than a certain amount of time, and then run it on a CI, it could take longer. So with `.range()`, you could give it a threshold instead.
|
||||
|
||||
#### milliseconds
|
||||
#### mininum
|
||||
#### maximum
|
||||
|
||||
Type: `number`
|
||||
|
||||
Milliseconds to delay the promise.
|
||||
|
||||
#### options
|
||||
|
||||
Type: `object`
|
||||
|
||||
##### value
|
||||
|
||||
Type: `unknown`
|
||||
|
||||
Optional value to resolve or reject in the returned promise.
|
||||
|
||||
##### signal
|
||||
|
||||
Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
|
||||
|
||||
The returned promise will be rejected with an AbortError if the signal is aborted. AbortSignal is available in all modern browsers and there is a [ponyfill for Node.js](https://github.com/mysticatea/abort-controller).
|
||||
|
||||
### delayPromise.clear()
|
||||
|
||||
Clears the delay and settles the promise.
|
||||
|
||||
### delay.createWithTimers({clearTimeout, setTimeout})
|
||||
|
||||
Creates a new `delay` instance using the provided functions for clearing and setting timeouts. Useful if you're about to stub timers globally, but you still want to use `delay` to manage your tests.
|
||||
|
||||
## Advanced usage
|
||||
|
||||
Passing a value:
|
||||
|
||||
```js
|
||||
const delay = require('delay');
|
||||
|
||||
(async() => {
|
||||
const result = await delay(100, {value: '🦄'});
|
||||
|
||||
// Executed after 100 milliseconds
|
||||
console.log(result);
|
||||
//=> '🦄'
|
||||
})();
|
||||
```
|
||||
|
||||
Using `delay.reject()`, which optionally accepts a value and rejects it `ms` later:
|
||||
|
||||
```js
|
||||
const delay = require('delay');
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await delay.reject(100, {value: new Error('🦄')});
|
||||
|
||||
console.log('This is never executed');
|
||||
} catch (error) {
|
||||
// 100 milliseconds later
|
||||
console.log(error);
|
||||
//=> [Error: 🦄]
|
||||
}
|
||||
})();
|
||||
```
|
||||
|
||||
You can settle the delay early by calling `.clear()`:
|
||||
|
||||
```js
|
||||
const delay = require('delay');
|
||||
|
||||
(async () => {
|
||||
const delayedPromise = delay(1000, {value: 'Done'});
|
||||
|
||||
setTimeout(() => {
|
||||
delayedPromise.clear();
|
||||
}, 500);
|
||||
|
||||
// 500 milliseconds later
|
||||
console.log(await delayedPromise);
|
||||
//=> 'Done'
|
||||
})();
|
||||
```
|
||||
|
||||
You can abort the delay with an AbortSignal:
|
||||
|
||||
```js
|
||||
const delay = require('delay');
|
||||
|
||||
(async () => {
|
||||
const abortController = new AbortController();
|
||||
|
||||
setTimeout(() => {
|
||||
abortController.abort();
|
||||
}, 500);
|
||||
|
||||
try {
|
||||
await delay(1000, {signal: abortController.signal});
|
||||
} catch (error) {
|
||||
// 500 milliseconds later
|
||||
console.log(error.name)
|
||||
//=> 'AbortError'
|
||||
}
|
||||
})();
|
||||
```
|
||||
|
||||
Create a new instance that is unaffected by libraries such as [lolex](https://github.com/sinonjs/lolex/):
|
||||
|
||||
```js
|
||||
const delay = require('delay');
|
||||
|
||||
const customDelay = delay.createWithTimers({clearTimeout, setTimeout});
|
||||
|
||||
(async() => {
|
||||
const result = await customDelay(100, {value: '🦄'});
|
||||
|
||||
// Executed after 100 milliseconds
|
||||
console.log(result);
|
||||
//=> '🦄'
|
||||
})();
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
- [delay-cli](https://github.com/sindresorhus/delay-cli) - CLI for this module
|
||||
- [p-cancelable](https://github.com/sindresorhus/p-cancelable) - Create a promise that can be canceled
|
||||
- [p-min-delay](https://github.com/sindresorhus/p-min-delay) - Delay a promise a minimum amount of time
|
||||
- [p-immediate](https://github.com/sindresorhus/p-immediate) - Returns a promise resolved in the next event loop - think `setImmediate()`
|
||||
- [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a specified amount of time
|
||||
- [More…](https://github.com/sindresorhus/promise-fun)
|
||||
Reference in New Issue
Block a user