Skip to content

Commit eeec389

Browse files
committed
addressing comments #1
1 parent 1438df0 commit eeec389

File tree

7 files changed

+48
-65
lines changed

7 files changed

+48
-65
lines changed

packages/firebase/index.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6225,11 +6225,7 @@ declare namespace firebase.firestore {
62256225
/**
62266226
* @hidden
62276227
*/
6228-
INTERNAL: {
6229-
delete: () => Promise<void>;
6230-
shutdown: () => Promise<void>;
6231-
isShutdown: () => boolean;
6232-
};
6228+
INTERNAL: { delete: () => Promise<void> };
62336229
}
62346230

62356231
/**

packages/firestore-types/index.d.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,7 @@ export class FirebaseFirestore {
276276
*/
277277
disableNetwork(): Promise<void>;
278278

279-
INTERNAL: {
280-
delete: () => Promise<void>;
281-
shutdown: () => Promise<void>;
282-
isShutdown: () => boolean;
283-
};
279+
INTERNAL: { delete: () => Promise<void> };
284280
}
285281

286282
/**

packages/firestore/src/api/database.ts

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,39 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
447447
return deferred.promise;
448448
}
449449

450+
/**
451+
* Shuts down this Firestore instance.
452+
*
453+
* After shutdown only the `clearPersistence()` method may be used. Any other method
454+
* will throw a `FirestoreError`.
455+
*
456+
* To restart after shutdown, simply create a new instance of FirebaseFirestore with
457+
* `firebase.firestore()`.
458+
*
459+
* Shutdown does not cancel any pending writes and any promises that are awaiting a response
460+
* from the server will not be resolved. If you have persistence enabled, the next time you
461+
* start this instance, it will resume attempting to send these writes to the server.
462+
*
463+
* Note: Under normal circumstances, calling `shutdown()` is not required. This
464+
* method is useful only when you want to force this instance to release all of its resources or
465+
* in combination with `clearPersistence()` to ensure that all local state is destroyed
466+
* between test runs.
467+
*
468+
* @return A promise that is resolved when the instance has been successfully shut down.
469+
*/
470+
// TODO(b/135755126): make this public.
471+
_shutdown(): Promise<void> {
472+
(this.app as _FirebaseApp)._removeServiceInstance(
473+
'firestore'
474+
);
475+
return this.INTERNAL.delete();
476+
}
477+
478+
get _isShutdown(): boolean {
479+
this.ensureClientConfigured();
480+
return this._firestoreClient!.clientShutdown;
481+
}
482+
450483
ensureClientConfigured(): FirestoreClient {
451484
if (!this._firestoreClient) {
452485
// Kick off starting the client but don't actually wait for it.
@@ -538,44 +571,11 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
538571
}
539572

540573
INTERNAL = {
541-
// TODO(b/135755126): Make this public
542574
delete: async (): Promise<void> => {
543575
// The client must be initalized to ensure that all subsequent API usage
544576
// throws an exception.
545577
this.ensureClientConfigured();
546578
await this._firestoreClient!.shutdown();
547-
},
548-
549-
/**
550-
* Shuts down this Firestore instance.
551-
*
552-
* After shutdown only the `clearPersistence()` method may be used. Any other method
553-
* will throw an `FirestoreError`.
554-
*
555-
* To restart after shutdown, simply create a new instance of FirebaseFirestore with
556-
* `Firebase.firestore()`.
557-
*
558-
* Shutdown does not cancel any pending writes and any promises that are awaiting a response
559-
* from the server will not be resolved. The next time you start this instance,
560-
* it will resume attempting to send these writes to the server.
561-
*
562-
* Note: Under normal circumstances, calling `shutdown()` is not required. This
563-
* method is useful only when you want to force this instance to release all of its resources or
564-
* in combination with `clearPersistence()` to ensure that all local state is destroyed
565-
* between test runs.
566-
*
567-
* @return A promise that is resolved when the instance has been successfully shut down.
568-
*/
569-
// TODO(b/135755126): make this public.
570-
shutdown: (): Promise<void> => {
571-
(this._config.firebaseApp as _FirebaseApp)._removeServiceInstance(
572-
'firestore'
573-
);
574-
return this.INTERNAL.delete();
575-
},
576-
577-
isShutdown: (): boolean => {
578-
return this._firestoreClient!.clientShutdown;
579579
}
580580
};
581581

packages/firestore/src/core/firestore_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ export class FirestoreClient {
506506
}
507507

508508
shutdown(): Promise<void> {
509-
return this.asyncQueue.enqueueAndInitilizeShutdown(async () => {
509+
return this.asyncQueue.enqueueAndInitiateShutdown(async () => {
510510
// PORTING NOTE: LocalStore does not need an explicit shutdown on web.
511511
if (this.lruScheduler) {
512512
this.lruScheduler.stop();

packages/firestore/src/util/async_queue.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ export class AsyncQueue {
226226
op: () => Promise<T>
227227
): void {
228228
this.verifyNotFailed();
229-
// tslint:disable-next-line:no-floating-promises
230229
this.enqueueInternal(op);
231230
}
232231

@@ -238,7 +237,6 @@ export class AsyncQueue {
238237
op: () => Promise<T>
239238
): Promise<T> {
240239
this.verifyNotFailed();
241-
// tslint:disable-next-line:no-floating-promises
242240
return this.enqueueInternal(op);
243241
}
244242

@@ -249,18 +247,12 @@ export class AsyncQueue {
249247
* Once this method is called, the only possible way to request running an operation
250248
* is through `enqueueAndForgetEvenAfterShutdown`.
251249
*/
252-
enqueueAndInitilizeShutdown<T extends unknown>(
253-
op: () => Promise<T>
254-
): Promise<T> {
250+
async enqueueAndInitiateShutdown(op: () => Promise<void>): Promise<void> {
255251
this.verifyNotFailed();
256-
if (this._isShuttingDown) {
257-
// Return a Promise resolves right away if it is already shutdown.
258-
return new Promise<T>(resolve => resolve(undefined));
252+
if (!this._isShuttingDown) {
253+
this._isShuttingDown = true;
254+
await this.enqueueEvenAfterShutdown(op);
259255
}
260-
261-
const promise = this.enqueueInternal(op);
262-
this._isShuttingDown = true;
263-
return promise;
264256
}
265257

266258
/**

packages/firestore/test/integration/api/database.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ apiDescribe('Database', (persistence: boolean) => {
10731073
it('can start a new instance after shut down', async () => {
10741074
return withTestDoc(persistence, async docRef => {
10751075
const firestore = docRef.firestore;
1076-
await firestore.INTERNAL.shutdown();
1076+
await (firestore as any)._shutdown();
10771077

10781078
const newFirestore = firebase.firestore!(firestore.app);
10791079
expect(newFirestore).to.not.equal(firestore);
@@ -1092,14 +1092,14 @@ apiDescribe('Database', (persistence: boolean) => {
10921092
const app = docRef.firestore.app;
10931093
await app.delete();
10941094

1095-
expect(docRef.firestore.INTERNAL.isShutdown()).to.be.true;
1095+
expect((docRef.firestore as any)._isShutdown).to.be.true;
10961096
});
10971097
});
10981098

10991099
it('new operation after shutdown should throw', async () => {
11001100
await withTestDoc(persistence, async docRef => {
11011101
const firestore = docRef.firestore;
1102-
await firestore.INTERNAL.shutdown();
1102+
await (firestore as any)._shutdown();
11031103

11041104
expect(() => {
11051105
firestore.doc(docRef.path).set({ foo: 'bar' });
@@ -1110,8 +1110,8 @@ apiDescribe('Database', (persistence: boolean) => {
11101110
it('calling shutdown mutiple times should proceed', async () => {
11111111
await withTestDoc(persistence, async docRef => {
11121112
const firestore = docRef.firestore;
1113-
await firestore.INTERNAL.shutdown();
1114-
await firestore.INTERNAL.shutdown();
1113+
await (firestore as any)._shutdown();
1114+
await (firestore as any)._shutdown();
11151115

11161116
expect(() => {
11171117
firestore.doc(docRef.path).set({ foo: 'bar' });

packages/firestore/test/unit/util/async_queue.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,17 @@ describe('AsyncQueue', () => {
213213
it('Schedules operaions with respect to shut down', async () => {
214214
const queue = new AsyncQueue();
215215
const completedSteps: number[] = [];
216-
const doStep = (n: number): Promise<number> =>
216+
const doStep = (n: number): Promise<void> =>
217217
defer(() => {
218-
const result = completedSteps.push(n);
219-
return result;
218+
completedSteps.push(n);
220219
});
221220

222221
queue.enqueueAndForget(() => doStep(1));
223222

224223
// After this call, only operations requested via
225224
// `enqueueAndForgetEvenAfterShutdown` gets executed.
226225
// tslint:disable-next-line:no-floating-promises
227-
queue.enqueueAndInitilizeShutdown(() => doStep(2));
226+
queue.enqueueAndInitiateShutdown(() => doStep(2));
228227
queue.enqueueAndForget(() => doStep(3));
229228
queue.enqueueAndForgetEvenAfterShutdown(() => doStep(4));
230229

0 commit comments

Comments
 (0)