Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e49be654eb | |||
| 9f599e79a1 | |||
| 3821799070 | |||
| 80de670cad |
2
package-lock.json
generated
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdelay",
|
"name": "@pushrocks/smartdelay",
|
||||||
"version": "2.0.8",
|
"version": "2.0.10",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartdelay",
|
"name": "@pushrocks/smartdelay",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "2.0.8",
|
"version": "2.0.10",
|
||||||
"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",
|
||||||
|
|||||||
14
ts/index.ts
14
ts/index.ts
@@ -33,10 +33,14 @@ export let delayForRandom = async <T>(
|
|||||||
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 started: number;
|
||||||
|
|
||||||
constructor(timeInMillisecondArg, passOn?: T) {
|
constructor(timeInMillisecondArg, passOn?: T) {
|
||||||
|
this.timeoutInMillis = timeInMillisecondArg;
|
||||||
this._deferred = smartpromise.defer<T>();
|
this._deferred = smartpromise.defer<T>();
|
||||||
this.promise = this._deferred.promise;
|
this.promise = this._deferred.promise;
|
||||||
this._timeout = setTimeout(() => {
|
this._timeout = setTimeout(() => {
|
||||||
@@ -44,6 +48,7 @@ export class Timeout<T> {
|
|||||||
this._deferred.resolve(passOn);
|
this._deferred.resolve(passOn);
|
||||||
}
|
}
|
||||||
}, timeInMillisecondArg);
|
}, timeInMillisecondArg);
|
||||||
|
this.started = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,6 +63,11 @@ export class Timeout<T> {
|
|||||||
*/
|
*/
|
||||||
public cancel() {
|
public cancel() {
|
||||||
this._cancelled = true;
|
this._cancelled = true;
|
||||||
this.makeUnrefed();
|
clearTimeout(this._timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getTimeLeft() {
|
||||||
|
const result = this.started + this.timeoutInMillis - Date.now();
|
||||||
|
return result > 0 ? result : 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user