@@ -28,7 +28,7 @@ import {
28
28
canceled ,
29
29
retryLimitExceeded
30
30
} from './error' ;
31
- import { RequestInfo } from './requestinfo' ;
31
+ import { RequestHandler , RequestInfo } from './requestinfo' ;
32
32
import { isJustDef } from './type' ;
33
33
import { makeQueryString } from './url' ;
34
34
import { Headers , Connection , ErrorCode } from './connection' ;
@@ -48,54 +48,28 @@ export interface Request<T> {
48
48
}
49
49
50
50
class NetworkRequest < T > implements Request < T > {
51
- private url_ : string ;
52
- private method_ : string ;
53
- private headers_ : Headers ;
54
- private body_ : string | Blob | Uint8Array | null ;
55
- private successCodes_ : number [ ] ;
56
- private additionalRetryCodes_ : number [ ] ;
57
51
private pendingConnection_ : Connection | null = null ;
58
52
private backoffId_ : backoffId | null = null ;
59
53
private resolve_ ! : ( value ?: T | PromiseLike < T > ) => void ;
60
54
// eslint-disable-next-line @typescript-eslint/no-explicit-any
61
55
private reject_ ! : ( reason ?: any ) => void ;
62
56
private canceled_ : boolean = false ;
63
57
private appDelete_ : boolean = false ;
64
- private callback_ : ( p1 : Connection , p2 : string ) => T ;
65
- private errorCallback_ :
66
- | ( ( p1 : Connection , p2 : StorageError ) => StorageError )
67
- | null ;
68
- private progressCallback_ : ( ( p1 : number , p2 : number ) => void ) | null ;
69
- private timeout_ : number ;
70
- private pool_ : ConnectionPool ;
71
58
promise_ : Promise < T > ;
72
59
73
60
constructor (
74
- url : string ,
75
- method : string ,
76
- headers : Headers ,
77
- body : string | Blob | Uint8Array | null ,
78
- successCodes : number [ ] ,
79
- additionalRetryCodes : number [ ] ,
80
- callback : ( p1 : Connection , p2 : string ) => T ,
81
- errorCallback :
82
- | ( ( p1 : Connection , p2 : StorageError ) => StorageError )
83
- | null ,
84
- timeout : number ,
85
- progressCallback : ( ( p1 : number , p2 : number ) => void ) | null ,
86
- pool : ConnectionPool
61
+ private url_ : string ,
62
+ private method_ : string ,
63
+ private headers_ : Headers ,
64
+ private body_ : string | Blob | Uint8Array | null ,
65
+ private successCodes_ : number [ ] ,
66
+ private additionalRetryCodes_ : number [ ] ,
67
+ private callback_ : RequestHandler < string , T > ,
68
+ private errorCallback_ : RequestHandler < StorageError , StorageError > | null ,
69
+ private timeout_ : number ,
70
+ private progressCallback_ : ( ( p1 : number , p2 : number ) => void ) | null ,
71
+ private pool_ : ConnectionPool
87
72
) {
88
- this . url_ = url ;
89
- this . method_ = method ;
90
- this . headers_ = headers ;
91
- this . body_ = body ;
92
- this . successCodes_ = successCodes . slice ( ) ;
93
- this . additionalRetryCodes_ = additionalRetryCodes . slice ( ) ;
94
- this . callback_ = callback ;
95
- this . errorCallback_ = errorCallback ;
96
- this . progressCallback_ = progressCallback ;
97
- this . timeout_ = timeout ;
98
- this . pool_ = pool ;
99
73
this . promise_ = new Promise ( ( resolve , reject ) => {
100
74
this . resolve_ = resolve as ( value ?: T | PromiseLike < T > ) => void ;
101
75
this . reject_ = reject ;
@@ -107,67 +81,68 @@ class NetworkRequest<T> implements Request<T> {
107
81
* Actually starts the retry loop.
108
82
*/
109
83
private start_ ( ) : void {
110
- const self = this ;
111
-
112
- function doTheRequest (
113
- backoffCallback : ( p1 : boolean , ...p2 : unknown [ ] ) => void ,
84
+ const doTheRequest : (
85
+ backoffCallback : ( success : boolean , ...p2 : unknown [ ] ) => void ,
114
86
canceled : boolean
115
- ) : void {
87
+ ) => void = ( backoffCallback , canceled ) => {
116
88
if ( canceled ) {
117
89
backoffCallback ( false , new RequestEndStatus ( false , null , true ) ) ;
118
90
return ;
119
91
}
120
- const connection = self . pool_ . createConnection ( ) ;
121
- self . pendingConnection_ = connection ;
92
+ const connection = this . pool_ . createConnection ( ) ;
93
+ this . pendingConnection_ = connection ;
122
94
123
- function progressListener ( progressEvent : ProgressEvent ) : void {
124
- const loaded = progressEvent . loaded ;
125
- const total = progressEvent . lengthComputable ? progressEvent . total : - 1 ;
126
- if ( self . progressCallback_ !== null ) {
127
- self . progressCallback_ ( loaded , total ) ;
128
- }
129
- }
130
- if ( self . progressCallback_ !== null ) {
95
+ const progressListener : ( progressEvent : ProgressEvent ) => void =
96
+ progressEvent => {
97
+ const loaded = progressEvent . loaded ;
98
+ const total = progressEvent . lengthComputable
99
+ ? progressEvent . total
100
+ : - 1 ;
101
+ if ( this . progressCallback_ !== null ) {
102
+ this . progressCallback_ ( loaded , total ) ;
103
+ }
104
+ } ;
105
+ if ( this . progressCallback_ !== null ) {
131
106
connection . addUploadProgressListener ( progressListener ) ;
132
107
}
133
108
134
109
// eslint-disable-next-line @typescript-eslint/no-floating-promises
135
110
connection
136
- . send ( self . url_ , self . method_ , self . body_ , self . headers_ )
111
+ . send ( this . url_ , this . method_ , this . body_ , this . headers_ )
137
112
. then ( ( ) => {
138
- if ( self . progressCallback_ !== null ) {
113
+ if ( this . progressCallback_ !== null ) {
139
114
connection . removeUploadProgressListener ( progressListener ) ;
140
115
}
141
- self . pendingConnection_ = null ;
116
+ this . pendingConnection_ = null ;
142
117
const hitServer = connection . getErrorCode ( ) === ErrorCode . NO_ERROR ;
143
118
const status = connection . getStatus ( ) ;
144
- if ( ! hitServer || self . isRetryStatusCode_ ( status ) ) {
119
+ if ( ! hitServer || this . isRetryStatusCode_ ( status ) ) {
145
120
const wasCanceled = connection . getErrorCode ( ) === ErrorCode . ABORT ;
146
121
backoffCallback (
147
122
false ,
148
123
new RequestEndStatus ( false , null , wasCanceled )
149
124
) ;
150
125
return ;
151
126
}
152
- const successCode = self . successCodes_ . indexOf ( status ) !== - 1 ;
127
+ const successCode = this . successCodes_ . indexOf ( status ) !== - 1 ;
153
128
backoffCallback ( true , new RequestEndStatus ( successCode , connection ) ) ;
154
129
} ) ;
155
- }
130
+ } ;
156
131
157
132
/**
158
133
* @param requestWentThrough - True if the request eventually went
159
134
* through, false if it hit the retry limit or was canceled.
160
135
*/
161
- function backoffDone (
136
+ const backoffDone : (
162
137
requestWentThrough : boolean ,
163
138
status : RequestEndStatus
164
- ) : void {
165
- const resolve = self . resolve_ ;
166
- const reject = self . reject_ ;
139
+ ) => void = ( requestWentThrough , status ) => {
140
+ const resolve = this . resolve_ ;
141
+ const reject = this . reject_ ;
167
142
const connection = status . connection as Connection ;
168
143
if ( status . wasSuccessCode ) {
169
144
try {
170
- const result = self . callback_ (
145
+ const result = this . callback_ (
171
146
connection ,
172
147
connection . getResponseText ( )
173
148
) ;
@@ -183,22 +158,22 @@ class NetworkRequest<T> implements Request<T> {
183
158
if ( connection !== null ) {
184
159
const err = unknown ( ) ;
185
160
err . serverResponse = connection . getResponseText ( ) ;
186
- if ( self . errorCallback_ ) {
187
- reject ( self . errorCallback_ ( connection , err ) ) ;
161
+ if ( this . errorCallback_ ) {
162
+ reject ( this . errorCallback_ ( connection , err ) ) ;
188
163
} else {
189
164
reject ( err ) ;
190
165
}
191
166
} else {
192
167
if ( status . canceled ) {
193
- const err = self . appDelete_ ? appDeleted ( ) : canceled ( ) ;
168
+ const err = this . appDelete_ ? appDeleted ( ) : canceled ( ) ;
194
169
reject ( err ) ;
195
170
} else {
196
171
const err = retryLimitExceeded ( ) ;
197
172
reject ( err ) ;
198
173
}
199
174
}
200
175
}
201
- }
176
+ } ;
202
177
if ( this . canceled_ ) {
203
178
backoffDone ( false , new RequestEndStatus ( false , null , true ) ) ;
204
179
} else {
0 commit comments