Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2dc91dc2f0 | |||
| 3784de03ce | |||
| e49be654eb | |||
| 9f599e79a1 |
24575
package-lock.json
generated
24575
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdelay",
|
"name": "@pushrocks/smartdelay",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "2.0.9",
|
"version": "2.0.11",
|
||||||
"description": "timeouts for the async/await era, written in TypeScript",
|
"description": "timeouts for the async/await era, written in TypeScript",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
"@gitzone/tsrun": "^1.2.8",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@gitzone/tstest": "^1.0.28",
|
"@gitzone/tstest": "^1.0.28",
|
||||||
"@pushrocks/tapbundle": "^3.2.1",
|
"@pushrocks/tapbundle": "^3.2.1",
|
||||||
"@types/node": "^14.0.5",
|
"@types/node": "^15.12.0",
|
||||||
"tslint": "^6.1.2",
|
"tslint": "^6.1.2",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
|
|||||||
39
ts/index.ts
39
ts/index.ts
@@ -2,44 +2,38 @@ import * as smartpromise from '@pushrocks/smartpromise';
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* delay something, works like setTimeout
|
* delay something, works like setTimeout
|
||||||
* @param timeInMillisecond
|
* @param timeInMillisecondArg
|
||||||
* @param passOn
|
* @param passOnArg
|
||||||
*/
|
*/
|
||||||
export let delayFor = async <T>(timeInMillisecond: number, passOn?: T) => {
|
export let delayFor = async <T>(timeInMillisecondArg: number, passOnArg?: T, unrefedArg = false) => {
|
||||||
await new Promise((resolve, reject) => {
|
const timeout = new Timeout(timeInMillisecondArg, null, unrefedArg);
|
||||||
setTimeout(() => {
|
await timeout.promise;
|
||||||
resolve();
|
return passOnArg;
|
||||||
}, timeInMillisecond);
|
|
||||||
});
|
|
||||||
return passOn;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* delay for a random time
|
* delay for a random time
|
||||||
*/
|
*/
|
||||||
export let delayForRandom = async <T>(
|
export let delayForRandom = async <T>(
|
||||||
timeMinInMillisecond: number,
|
timeMinInMillisecondArg: number,
|
||||||
timeMaxInMillisecond: number,
|
timeMaxInMillisecondArg: number,
|
||||||
passOn?: T
|
passOnArg?: T,
|
||||||
|
unrefedArg = false
|
||||||
) => {
|
) => {
|
||||||
await new Promise((resolve, reject) => {
|
await delayFor(Math.random() * (timeMaxInMillisecondArg - timeMinInMillisecondArg) + timeMinInMillisecondArg, null, unrefedArg)
|
||||||
setTimeout(() => {
|
return passOnArg;
|
||||||
resolve();
|
|
||||||
}, Math.random() * (timeMaxInMillisecond - timeMinInMillisecond) + timeMinInMillisecond);
|
|
||||||
});
|
|
||||||
return passOn;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export class Timeout<T> {
|
export class Timeout<T> {
|
||||||
promise: Promise<T>;
|
promise: Promise<T>;
|
||||||
private _deferred: smartpromise.Deferred<T>;
|
private _deferred: smartpromise.Deferred<T>;
|
||||||
private _timeout: any;
|
private _timeout;
|
||||||
private _cancelled: boolean = false;
|
private _cancelled: boolean = false;
|
||||||
|
|
||||||
private timeoutInMillis: number;
|
private timeoutInMillis: number;
|
||||||
private started: number;
|
private started: number;
|
||||||
|
|
||||||
constructor(timeInMillisecondArg, passOn?: T) {
|
constructor(timeInMillisecondArg, passOn?: T, unrefedArg = false) {
|
||||||
this.timeoutInMillis = timeInMillisecondArg;
|
this.timeoutInMillis = timeInMillisecondArg;
|
||||||
this._deferred = smartpromise.defer<T>();
|
this._deferred = smartpromise.defer<T>();
|
||||||
this.promise = this._deferred.promise;
|
this.promise = this._deferred.promise;
|
||||||
@@ -49,6 +43,9 @@ export class Timeout<T> {
|
|||||||
}
|
}
|
||||||
}, timeInMillisecondArg);
|
}, timeInMillisecondArg);
|
||||||
this.started = Date.now();
|
this.started = Date.now();
|
||||||
|
if (unrefedArg) {
|
||||||
|
this.makeUnrefed();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +60,7 @@ export class Timeout<T> {
|
|||||||
*/
|
*/
|
||||||
public cancel() {
|
public cancel() {
|
||||||
this._cancelled = true;
|
this._cancelled = true;
|
||||||
this.makeUnrefed();
|
clearTimeout(this._timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getTimeLeft() {
|
public getTimeLeft() {
|
||||||
|
|||||||
Reference in New Issue
Block a user