@@ -28,7 +28,7 @@ import {
2828 canceled ,
2929 retryLimitExceeded
3030} from './error' ;
31- import { RequestInfo } from './requestinfo' ;
31+ import { RequestHandler , RequestInfo } from './requestinfo' ;
3232import { isJustDef } from './type' ;
3333import { makeQueryString } from './url' ;
3434import { Headers , Connection , ErrorCode } from './connection' ;
@@ -48,54 +48,28 @@ export interface Request<T> {
4848}
4949
5050class 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 [ ] ;
5751 private pendingConnection_ : Connection | null = null ;
5852 private backoffId_ : backoffId | null = null ;
5953 private resolve_ ! : ( value ?: T | PromiseLike < T > ) => void ;
6054 // eslint-disable-next-line @typescript-eslint/no-explicit-any
6155 private reject_ ! : ( reason ?: any ) => void ;
6256 private canceled_ : boolean = false ;
6357 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 ;
7158 promise_ : Promise < T > ;
7259
7360 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
8772 ) {
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 ;
9973 this . promise_ = new Promise ( ( resolve , reject ) => {
10074 this . resolve_ = resolve as ( value ?: T | PromiseLike < T > ) => void ;
10175 this . reject_ = reject ;
@@ -107,67 +81,68 @@ class NetworkRequest<T> implements Request<T> {
10781 * Actually starts the retry loop.
10882 */
10983 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 ,
11486 canceled : boolean
115- ) : void {
87+ ) => void = ( backoffCallback , canceled ) => {
11688 if ( canceled ) {
11789 backoffCallback ( false , new RequestEndStatus ( false , null , true ) ) ;
11890 return ;
11991 }
120- const connection = self . pool_ . createConnection ( ) ;
121- self . pendingConnection_ = connection ;
92+ const connection = this . pool_ . createConnection ( ) ;
93+ this . pendingConnection_ = connection ;
12294
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 ) {
131106 connection . addUploadProgressListener ( progressListener ) ;
132107 }
133108
134109 // eslint-disable-next-line @typescript-eslint/no-floating-promises
135110 connection
136- . send ( self . url_ , self . method_ , self . body_ , self . headers_ )
111+ . send ( this . url_ , this . method_ , this . body_ , this . headers_ )
137112 . then ( ( ) => {
138- if ( self . progressCallback_ !== null ) {
113+ if ( this . progressCallback_ !== null ) {
139114 connection . removeUploadProgressListener ( progressListener ) ;
140115 }
141- self . pendingConnection_ = null ;
116+ this . pendingConnection_ = null ;
142117 const hitServer = connection . getErrorCode ( ) === ErrorCode . NO_ERROR ;
143118 const status = connection . getStatus ( ) ;
144- if ( ! hitServer || self . isRetryStatusCode_ ( status ) ) {
119+ if ( ! hitServer || this . isRetryStatusCode_ ( status ) ) {
145120 const wasCanceled = connection . getErrorCode ( ) === ErrorCode . ABORT ;
146121 backoffCallback (
147122 false ,
148123 new RequestEndStatus ( false , null , wasCanceled )
149124 ) ;
150125 return ;
151126 }
152- const successCode = self . successCodes_ . indexOf ( status ) !== - 1 ;
127+ const successCode = this . successCodes_ . indexOf ( status ) !== - 1 ;
153128 backoffCallback ( true , new RequestEndStatus ( successCode , connection ) ) ;
154129 } ) ;
155- }
130+ } ;
156131
157132 /**
158133 * @param requestWentThrough - True if the request eventually went
159134 * through, false if it hit the retry limit or was canceled.
160135 */
161- function backoffDone (
136+ const backoffDone : (
162137 requestWentThrough : boolean ,
163138 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_ ;
167142 const connection = status . connection as Connection ;
168143 if ( status . wasSuccessCode ) {
169144 try {
170- const result = self . callback_ (
145+ const result = this . callback_ (
171146 connection ,
172147 connection . getResponseText ( )
173148 ) ;
@@ -183,22 +158,22 @@ class NetworkRequest<T> implements Request<T> {
183158 if ( connection !== null ) {
184159 const err = unknown ( ) ;
185160 err . serverResponse = connection . getResponseText ( ) ;
186- if ( self . errorCallback_ ) {
187- reject ( self . errorCallback_ ( connection , err ) ) ;
161+ if ( this . errorCallback_ ) {
162+ reject ( this . errorCallback_ ( connection , err ) ) ;
188163 } else {
189164 reject ( err ) ;
190165 }
191166 } else {
192167 if ( status . canceled ) {
193- const err = self . appDelete_ ? appDeleted ( ) : canceled ( ) ;
168+ const err = this . appDelete_ ? appDeleted ( ) : canceled ( ) ;
194169 reject ( err ) ;
195170 } else {
196171 const err = retryLimitExceeded ( ) ;
197172 reject ( err ) ;
198173 }
199174 }
200175 }
201- }
176+ } ;
202177 if ( this . canceled_ ) {
203178 backoffDone ( false , new RequestEndStatus ( false , null , true ) ) ;
204179 } else {
0 commit comments