6 Commits

Author SHA1 Message Date
2dc91dc2f0 2.0.11 2021-06-04 13:54:21 +02:00
3784de03ce fix(core): update 2021-06-04 13:54:21 +02:00
e49be654eb 2.0.10 2020-07-11 21:33:35 +00:00
9f599e79a1 fix(core): update 2020-07-11 21:33:34 +00:00
3821799070 2.0.9 2020-05-27 16:54:33 +00:00
80de670cad fix(core): update 2020-05-27 16:54:32 +00:00
3 changed files with 23307 additions and 1271 deletions

24575
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
{
"name": "@pushrocks/smartdelay",
"private": false,
"version": "2.0.8",
"version": "2.0.11",
"description": "timeouts for the async/await era, written in TypeScript",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
@@ -28,7 +28,7 @@
"@gitzone/tsrun": "^1.2.8",
"@gitzone/tstest": "^1.0.28",
"@pushrocks/tapbundle": "^3.2.1",
"@types/node": "^14.0.5",
"@types/node": "^15.12.0",
"tslint": "^6.1.2",
"tslint-config-prettier": "^1.18.0"
},

View File

@@ -2,41 +2,39 @@ import * as smartpromise from '@pushrocks/smartpromise';
/**
* delay something, works like setTimeout
* @param timeInMillisecond
* @param passOn
* @param timeInMillisecondArg
* @param passOnArg
*/
export let delayFor = async <T>(timeInMillisecond: number, passOn?: T) => {
await new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, timeInMillisecond);
});
return passOn;
export let delayFor = async <T>(timeInMillisecondArg: number, passOnArg?: T, unrefedArg = false) => {
const timeout = new Timeout(timeInMillisecondArg, null, unrefedArg);
await timeout.promise;
return passOnArg;
};
/**
* delay for a random time
*/
export let delayForRandom = async <T>(
timeMinInMillisecond: number,
timeMaxInMillisecond: number,
passOn?: T
timeMinInMillisecondArg: number,
timeMaxInMillisecondArg: number,
passOnArg?: T,
unrefedArg = false
) => {
await new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, Math.random() * (timeMaxInMillisecond - timeMinInMillisecond) + timeMinInMillisecond);
});
return passOn;
await delayFor(Math.random() * (timeMaxInMillisecondArg - timeMinInMillisecondArg) + timeMinInMillisecondArg, null, unrefedArg)
return passOnArg;
};
export class Timeout<T> {
promise: Promise<T>;
private _deferred: smartpromise.Deferred<T>;
private _timeout: any;
private _timeout;
private _cancelled: boolean = false;
constructor(timeInMillisecondArg, passOn?: T) {
private timeoutInMillis: number;
private started: number;
constructor(timeInMillisecondArg, passOn?: T, unrefedArg = false) {
this.timeoutInMillis = timeInMillisecondArg;
this._deferred = smartpromise.defer<T>();
this.promise = this._deferred.promise;
this._timeout = setTimeout(() => {
@@ -44,6 +42,10 @@ export class Timeout<T> {
this._deferred.resolve(passOn);
}
}, timeInMillisecondArg);
this.started = Date.now();
if (unrefedArg) {
this.makeUnrefed();
}
}
/**
@@ -58,6 +60,11 @@ export class Timeout<T> {
*/
public cancel() {
this._cancelled = true;
this.makeUnrefed();
clearTimeout(this._timeout);
}
public getTimeLeft() {
const result = this.started + this.timeoutInMillis - Date.now();
return result > 0 ? result : 0;
}
}