@@ -24,13 +24,9 @@ import { RequestInfo } from './implementation/requestinfo';
24
24
import { XhrIoPool } from './implementation/xhriopool' ;
25
25
import { Reference } from './reference' ;
26
26
import { Provider } from '@firebase/component' ;
27
- import {
28
- FirebaseAuthInternalName ,
29
- FirebaseAuthTokenData
30
- } from '@firebase/auth-interop-types' ;
27
+ import { FirebaseAuthInternalName } from '@firebase/auth-interop-types' ;
31
28
import { FirebaseOptions } from '@firebase/app-types-exp' ;
32
29
import * as constants from '../src/implementation/constants' ;
33
- import { RequestMap } from './implementation/requestmap' ;
34
30
import { requestMaker } from './implementation/requestmaker' ;
35
31
import * as errorsExports from './implementation/error' ;
36
32
@@ -42,15 +38,14 @@ import * as errorsExports from './implementation/error';
42
38
*/
43
39
export class StorageService {
44
40
private app_ : FirebaseApp | null ;
45
- private bucket_ : Location | null = null ;
46
- private internals_ : ServiceInternals ;
47
- private authProvider_ : Provider < FirebaseAuthInternalName > ;
48
- private appId_ : string | null = null ;
49
-
50
- private storageRefMaker_ : ( loc : Location ) => Reference ;
51
- private requestMaker_ : requestMaker ;
52
- private pool_ : XhrIoPool ;
53
- private requestMap_ : RequestMap ;
41
+ private readonly bucket_ : Location | null = null ;
42
+ private readonly internals_ : ServiceInternals ;
43
+ private readonly authProvider_ : Provider < FirebaseAuthInternalName > ;
44
+ private readonly appId_ : string | null = null ;
45
+
46
+ private readonly requestMaker_ : requestMaker ;
47
+ private readonly pool_ : XhrIoPool ;
48
+ private readonly requests_ : Set < Request < unknown > > ;
54
49
private deleted_ : boolean = false ;
55
50
private maxOperationRetryTime_ : number ;
56
51
private maxUploadRetryTime_ : number ;
@@ -63,53 +58,36 @@ export class StorageService {
63
58
) {
64
59
this . app_ = app ;
65
60
this . authProvider_ = authProvider ;
66
- this . storageRefMaker_ = ( loc : Location ) : Reference => {
67
- return new Reference ( this , loc ) ;
68
- } ;
69
61
this . requestMaker_ = makeRequest ;
70
62
this . maxOperationRetryTime_ = constants . DEFAULT_MAX_OPERATION_RETRY_TIME ;
71
63
this . maxUploadRetryTime_ = constants . DEFAULT_MAX_UPLOAD_RETRY_TIME ;
72
- this . requestMap_ = new RequestMap ( ) ;
64
+ this . requests_ = new Set ( ) ;
73
65
this . pool_ = pool ;
74
66
if ( url != null ) {
75
67
this . bucket_ = Location . makeFromBucketSpec ( url ) ;
76
68
} else {
77
- const bucketFromOptions =
78
- this . app_ != null
79
- ? StorageService . extractBucket_ ( this . app_ . options )
80
- : null ;
81
- if ( bucketFromOptions != null ) {
82
- this . bucket_ = new Location ( bucketFromOptions , '' ) ;
83
- }
69
+ this . bucket_ = StorageService . extractBucket_ ( this . app_ ?. options ) ;
84
70
}
85
71
this . internals_ = new ServiceInternals ( this ) ;
86
72
}
87
73
88
- private static extractBucket_ ( config : FirebaseOptions ) : string | null {
89
- const bucketString = config [ constants . CONFIG_STORAGE_BUCKET_KEY ] || null ;
74
+ private static extractBucket_ ( config ? : FirebaseOptions ) : Location | null {
75
+ const bucketString = config ?. [ constants . CONFIG_STORAGE_BUCKET_KEY ] ;
90
76
if ( bucketString == null ) {
91
77
return null ;
92
78
}
93
- const loc : Location = Location . makeFromBucketSpec ( bucketString ) ;
94
- return loc . bucket ;
79
+ return Location . makeFromBucketSpec ( bucketString ) ;
95
80
}
96
81
97
- getAuthToken ( ) : Promise < string | null > {
82
+ async getAuthToken ( ) : Promise < string | null > {
98
83
const auth = this . authProvider_ . getImmediate ( { optional : true } ) ;
99
84
if ( auth ) {
100
- return auth . getToken ( ) . then (
101
- ( response : FirebaseAuthTokenData | null ) : string | null => {
102
- if ( response !== null ) {
103
- return response . accessToken ;
104
- } else {
105
- return null ;
106
- }
107
- } ,
108
- ( ) => null
109
- ) ;
110
- } else {
111
- return Promise . resolve ( null ) ;
85
+ const tokenData = await auth . getToken ( ) ;
86
+ if ( tokenData !== null ) {
87
+ return tokenData . accessToken ;
88
+ }
112
89
}
90
+ return null ;
113
91
}
114
92
115
93
/**
@@ -118,7 +96,8 @@ export class StorageService {
118
96
deleteApp ( ) : void {
119
97
this . deleted_ = true ;
120
98
this . app_ = null ;
121
- this . requestMap_ . clear ( ) ;
99
+ this . requests_ . forEach ( request => request . cancel ( ) ) ;
100
+ this . requests_ . clear ( ) ;
122
101
}
123
102
124
103
/**
@@ -128,7 +107,7 @@ export class StorageService {
128
107
* @return A firebaseStorage.Reference.
129
108
*/
130
109
makeStorageReference ( loc : Location ) : Reference {
131
- return this . storageRefMaker_ ( loc ) ;
110
+ return new Reference ( this , loc ) ;
132
111
}
133
112
134
113
makeRequest < T > (
@@ -142,7 +121,12 @@ export class StorageService {
142
121
authToken ,
143
122
this . pool_
144
123
) ;
145
- this . requestMap_ . addRequest ( request ) ;
124
+ this . requests_ . add ( request ) ;
125
+ // Request removes itself from set when complete.
126
+ request . getPromise ( ) . then (
127
+ ( ) => this . requests_ . delete ( request ) ,
128
+ ( ) => this . requests_ . delete ( request )
129
+ ) ;
146
130
return request ;
147
131
} else {
148
132
return new FailRequest ( errorsExports . appDeleted ( ) ) ;
0 commit comments