-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Task): Refactor task to use
Promise.race
Don't reinvent the wheel
- Loading branch information
Showing
7 changed files
with
28 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,48 @@ | ||
import { sleep } from './objectUtils'; | ||
|
||
/** | ||
* Wraps a promise in a Task api for convenience. | ||
*/ | ||
export default class Task<T = void> { | ||
export class Task<T = void> { | ||
|
||
private _promise: Promise<T>; | ||
protected _promise: Promise<T>; | ||
private resolveFn: (value?: T | PromiseLike<T>) => void; | ||
private rejectFn: (reason: any) => void; | ||
private _isResolved = false; | ||
private timeout: NodeJS.Timer; | ||
|
||
constructor(timeoutMs?: number, private timeoutHandler?: () => PromiseLike<T>) { | ||
constructor() { | ||
this._promise = new Promise<T>((resolve, reject) => { | ||
this.resolveFn = resolve; | ||
this.rejectFn = reject; | ||
}); | ||
if (timeoutMs) { | ||
this.timeout = setTimeout(() => this.handleTimeout(), timeoutMs); | ||
} | ||
} | ||
|
||
get isResolved() { | ||
return this._isResolved; | ||
} | ||
|
||
get promise() { | ||
return this._promise; | ||
} | ||
|
||
handleTimeout() { | ||
if (this.timeoutHandler) { | ||
this.timeoutHandler().then(val => this.resolve(val)); | ||
} else { | ||
this.resolve(undefined); | ||
} | ||
} | ||
|
||
chainTo(promise: Promise<T>) { | ||
promise.then(value => this.resolve(value), reason => this.reject(reason)); | ||
get isResolved() { | ||
return this._isResolved; | ||
} | ||
|
||
resolve(result: undefined | T | PromiseLike<T>) { | ||
this.resolveTimeout(); | ||
this._isResolved = true; | ||
this.resolveFn(result); | ||
} | ||
|
||
reject(reason: any) { | ||
this.resolveTimeout(); | ||
this._isResolved = true; | ||
this.rejectFn(reason); | ||
} | ||
} | ||
|
||
private resolveTimeout() { | ||
if (this.timeout) { | ||
clearTimeout(this.timeout); | ||
} | ||
this._isResolved = true; | ||
/** | ||
* A task that can expire after the given time. | ||
* If that happens, the inner promise is resolved | ||
*/ | ||
export class ExpirableTask<T = void> extends Task<T | void> { | ||
constructor(timeoutMS: number) { | ||
super(); | ||
this._promise = Promise.race([this._promise, sleep(timeoutMS)]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters