Skip to content

Commit a07978c

Browse files
committed
Address PR comments
1 parent 40882b1 commit a07978c

File tree

2 files changed

+29
-99
lines changed

2 files changed

+29
-99
lines changed

packages/storage/src/implementation/requestmap.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

packages/storage/src/service.ts

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ import { RequestInfo } from './implementation/requestinfo';
2424
import { XhrIoPool } from './implementation/xhriopool';
2525
import { Reference } from './reference';
2626
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';
3128
import { FirebaseOptions } from '@firebase/app-types-exp';
3229
import * as constants from '../src/implementation/constants';
33-
import { RequestMap } from './implementation/requestmap';
3430
import { requestMaker } from './implementation/requestmaker';
3531
import * as errorsExports from './implementation/error';
3632

@@ -42,15 +38,14 @@ import * as errorsExports from './implementation/error';
4238
*/
4339
export class StorageService {
4440
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>>;
5449
private deleted_: boolean = false;
5550
private maxOperationRetryTime_: number;
5651
private maxUploadRetryTime_: number;
@@ -63,53 +58,36 @@ export class StorageService {
6358
) {
6459
this.app_ = app;
6560
this.authProvider_ = authProvider;
66-
this.storageRefMaker_ = (loc: Location): Reference => {
67-
return new Reference(this, loc);
68-
};
6961
this.requestMaker_ = makeRequest;
7062
this.maxOperationRetryTime_ = constants.DEFAULT_MAX_OPERATION_RETRY_TIME;
7163
this.maxUploadRetryTime_ = constants.DEFAULT_MAX_UPLOAD_RETRY_TIME;
72-
this.requestMap_ = new RequestMap();
64+
this.requests_ = new Set();
7365
this.pool_ = pool;
7466
if (url != null) {
7567
this.bucket_ = Location.makeFromBucketSpec(url);
7668
} 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);
8470
}
8571
this.internals_ = new ServiceInternals(this);
8672
}
8773

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];
9076
if (bucketString == null) {
9177
return null;
9278
}
93-
const loc: Location = Location.makeFromBucketSpec(bucketString);
94-
return loc.bucket;
79+
return Location.makeFromBucketSpec(bucketString);
9580
}
9681

97-
getAuthToken(): Promise<string | null> {
82+
async getAuthToken(): Promise<string | null> {
9883
const auth = this.authProvider_.getImmediate({ optional: true });
9984
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+
}
11289
}
90+
return null;
11391
}
11492

11593
/**
@@ -118,7 +96,8 @@ export class StorageService {
11896
deleteApp(): void {
11997
this.deleted_ = true;
12098
this.app_ = null;
121-
this.requestMap_.clear();
99+
this.requests_.forEach(request => request.cancel());
100+
this.requests_.clear();
122101
}
123102

124103
/**
@@ -128,7 +107,7 @@ export class StorageService {
128107
* @return A firebaseStorage.Reference.
129108
*/
130109
makeStorageReference(loc: Location): Reference {
131-
return this.storageRefMaker_(loc);
110+
return new Reference(this, loc);
132111
}
133112

134113
makeRequest<T>(
@@ -142,7 +121,12 @@ export class StorageService {
142121
authToken,
143122
this.pool_
144123
);
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+
);
146130
return request;
147131
} else {
148132
return new FailRequest(errorsExports.appDeleted());

0 commit comments

Comments
 (0)