| 15 | * of managinng a subscription to an event emitter. |
| 16 | */ |
| 17 | export class Signal<T> implements Promise<T>, Resolveable<T> { |
| 18 | [Symbol.toStringTag] = 'Promise'; |
| 19 | |
| 20 | private promise = new ManualPromise<T>(); |
| 21 | |
| 22 | /** |
| 23 | * Attaches callbacks for the resolution and/or rejection of the Promise. |
| 24 | * @param onfulfilled The callback to execute when the Promise is resolved. |
| 25 | * @param onrejected The callback to execute when the Promise is rejected. |
| 26 | * @returns A Promise for the completion of which ever callback is executed. |
| 27 | */ |
| 28 | then<TResult1 = T, TResult2 = never>(onfulfilled?: | ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: | ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2> { |
| 29 | return this.promise.then(onfulfilled, onrejected); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Attaches a callback for only the rejection of the Promise. |
| 34 | * @param onrejected The callback to execute when the Promise is rejected. |
| 35 | * @returns A Promise for the completion of the callback. |
| 36 | */ |
| 37 | catch<TResult = never>(onrejected?: | ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult> { |
| 38 | return this.promise.catch(onrejected); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The |
| 43 | * resolved value cannot be modified from the callback. |
| 44 | * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected). |
| 45 | * @returns A Promise for the completion of the callback. |
| 46 | */ |
| 47 | finally(onfinally?: (() => void) | null | undefined): Promise<T> { |
| 48 | return this.promise.finally(onfinally); |
| 49 | } |
| 50 | |
| 51 | get isPending(): boolean { |
| 52 | return this.promise.isPending; |
| 53 | } |
| 54 | get isCompleted(): boolean { |
| 55 | return this.promise.isCompleted; |
| 56 | } |
| 57 | get isResolved(): boolean { |
| 58 | return this.promise.isResolved; |
| 59 | } |
| 60 | get isRejected(): boolean { |
| 61 | return this.promise.isRejected; |
| 62 | } |
| 63 | /** |
| 64 | * A method to manually resolve the Promise. |
| 65 | * |
| 66 | * This also resets this instance to a new promise interally, so that any new awaiters will not be instantly resolved. |
| 67 | * @param value |
| 68 | */ |
| 69 | resolve(value: T): Resolveable<T> { |
| 70 | if (!this.isCompleted) { |
| 71 | const p = this.promise; |
| 72 | this.promise = new ManualPromise<T>(); |
| 73 | p.resolve(value); |
| 74 | } |
nothing calls this directly
no outgoing calls
no test coverage detected