22/* tslint:disable */
33/* eslint-disable */
44export class CancelError extends Error {
5- constructor ( reason : string = "Promise was canceled" ) {
6- super ( reason ) ;
7- this . name = "CancelError" ;
8- }
9-
10- public get isCancelled ( ) : boolean {
11- return true ;
12- }
5+
6+ constructor ( reason : string = 'Promise was canceled' ) {
7+ super ( reason ) ;
8+ this . name = 'CancelError' ;
9+ }
10+
11+ public get isCancelled ( ) : boolean {
12+ return true ;
13+ }
1314}
1415
1516export interface OnCancel {
16- readonly isPending : boolean ;
17- readonly isCancelled : boolean ;
17+ readonly isPending : boolean ;
18+ readonly isCancelled : boolean ;
1819
19- ( cancelHandler : ( ) => void ) : void ;
20+ ( cancelHandler : ( ) => void ) : void ;
2021}
2122
2223export class CancelablePromise < T > implements Promise < T > {
23- readonly [ Symbol . toStringTag ] : string ;
24-
25- #isPending: boolean ;
26- #isCancelled: boolean ;
27- readonly #cancelHandlers: ( ( ) => void ) [ ] ;
28- readonly #promise: Promise < T > ;
29- #resolve?: ( value : T | PromiseLike < T > ) => void ;
30- #reject?: ( reason ?: any ) => void ;
31-
32- constructor (
33- executor : (
34- resolve : ( value : T | PromiseLike < T > ) => void ,
35- reject : ( reason ?: any ) => void ,
36- onCancel : OnCancel
37- ) => void
38- ) {
39- this . #isPending = true ;
40- this . #isCancelled = false ;
41- this . #cancelHandlers = [ ] ;
42- this . #promise = new Promise < T > ( ( resolve , reject ) => {
43- this . #resolve = resolve ;
44- this . #reject = reject ;
45-
46- const onResolve = ( value : T | PromiseLike < T > ) : void => {
47- if ( ! this . #isCancelled) {
48- this . #isPending = false ;
49- this . #resolve?.( value ) ;
50- }
51- } ;
52-
53- const onReject = ( reason ?: any ) : void => {
54- this . #isPending = false ;
55- this . #reject?.( reason ) ;
56- } ;
57-
58- const onCancel = ( cancelHandler : ( ) => void ) : void => {
59- if ( this . #isPending) {
60- this . #cancelHandlers. push ( cancelHandler ) ;
61- }
62- } ;
63-
64- Object . defineProperty ( onCancel , " isPending" , {
65- get : ( ) : boolean => this . #isPending,
66- } ) ;
67-
68- Object . defineProperty ( onCancel , " isCancelled" , {
69- get : ( ) : boolean => this . #isCancelled,
70- } ) ;
71-
72- return executor ( onResolve , onReject , onCancel as OnCancel ) ;
73- } ) ;
74- }
75-
76- public then < TResult1 = T , TResult2 = never > (
77- onFulfilled ?: ( ( value : T ) => TResult1 | PromiseLike < TResult1 > ) | null ,
78- onRejected ?: ( ( reason : any ) => TResult2 | PromiseLike < TResult2 > ) | null
79- ) : Promise < TResult1 | TResult2 > {
80- return this . #promise. then ( onFulfilled , onRejected ) ;
81- }
82-
83- public catch < TResult = never > (
84- onRejected ?: ( ( reason : any ) => TResult | PromiseLike < TResult > ) | null
85- ) : Promise < T | TResult > {
86- return this . #promise. catch ( onRejected ) ;
87- }
88-
89- public finally ( onFinally ?: ( ( ) => void ) | null ) : Promise < T > {
90- return this . #promise. finally ( onFinally ) ;
91- }
92-
93- public cancel ( ) : void {
94- if ( ! this . #isPending || this . #isCancelled) {
95- return ;
96- }
97- this . #isCancelled = true ;
98- if ( this . #cancelHandlers. length ) {
99- try {
100- for ( const cancelHandler of this . #cancelHandlers) {
101- cancelHandler ( ) ;
102- }
103- } catch ( error ) {
104- this . #reject?.( error ) ;
105- return ;
106- }
107- }
108- }
109-
110- public get isCancelled ( ) : boolean {
111- return this . #isCancelled;
112- }
113- }
24+ readonly [ Symbol . toStringTag ] : string ;
25+
26+ #isPending: boolean ;
27+ #isCancelled: boolean ;
28+ readonly #cancelHandlers: ( ( ) => void ) [ ] ;
29+ readonly #promise: Promise < T > ;
30+ #resolve?: ( value : T | PromiseLike < T > ) => void ;
31+ #reject?: ( reason ?: any ) => void ;
32+
33+ constructor (
34+ executor : (
35+ resolve : ( value : T | PromiseLike < T > ) => void ,
36+ reject : ( reason ?: any ) => void ,
37+ onCancel : OnCancel
38+ ) => void
39+ ) {
40+ this . #isPending = true ;
41+ this . #isCancelled = false ;
42+ this . #cancelHandlers = [ ] ;
43+ this . #promise = new Promise < T > ( ( resolve , reject ) => {
44+ this . #resolve = resolve ;
45+ this . #reject = reject ;
46+
47+ const onResolve = ( value : T | PromiseLike < T > ) : void => {
48+ if ( ! this . #isCancelled) {
49+ this . #isPending = false ;
50+ this . #resolve?.( value ) ;
51+ }
52+ } ;
53+
54+ const onReject = ( reason ?: any ) : void => {
55+ this . #isPending = false ;
56+ this . #reject?.( reason ) ;
57+ } ;
58+
59+ const onCancel = ( cancelHandler : ( ) => void ) : void => {
60+ if ( this . #isPending) {
61+ this . #cancelHandlers. push ( cancelHandler ) ;
62+ }
63+ } ;
64+
65+ Object . defineProperty ( onCancel , ' isPending' , {
66+ get : ( ) : boolean => this . #isPending,
67+ } ) ;
68+
69+ Object . defineProperty ( onCancel , ' isCancelled' , {
70+ get : ( ) : boolean => this . #isCancelled,
71+ } ) ;
72+
73+ return executor ( onResolve , onReject , onCancel as OnCancel ) ;
74+ } ) ;
75+ }
76+
77+ public then < TResult1 = T , TResult2 = never > (
78+ onFulfilled ?: ( ( value : T ) => TResult1 | PromiseLike < TResult1 > ) | null ,
79+ onRejected ?: ( ( reason : any ) => TResult2 | PromiseLike < TResult2 > ) | null
80+ ) : Promise < TResult1 | TResult2 > {
81+ return this . #promise. then ( onFulfilled , onRejected ) ;
82+ }
83+
84+ public catch < TResult = never > (
85+ onRejected ?: ( ( reason : any ) => TResult | PromiseLike < TResult > ) | null
86+ ) : Promise < T | TResult > {
87+ return this . #promise. catch ( onRejected ) ;
88+ }
89+
90+ public finally ( onFinally ?: ( ( ) => void ) | null ) : Promise < T > {
91+ return this . #promise. finally ( onFinally ) ;
92+ }
93+
94+ public cancel ( ) : void {
95+ if ( ! this . #isPending || this . #isCancelled) {
96+ return ;
97+ }
98+ this . #isCancelled = true ;
99+ if ( this . #cancelHandlers. length ) {
100+ try {
101+ for ( const cancelHandler of this . #cancelHandlers) {
102+ cancelHandler ( ) ;
103+ }
104+ } catch ( error ) {
105+ this . #reject?.( error ) ;
106+ return ;
107+ }
108+ }
109+ }
110+
111+ public get isCancelled ( ) : boolean {
112+ return this . #isCancelled;
113+ }
114+ }
0 commit comments