Skip to content

Commit f14ebc2

Browse files
authored
Refactor PersistentStream (no behavior changes). (#1041)
This breaks out a number of changes I made as prep for b/80402781 (Continue retrying streams for 1 minute (idle delay)). PersistentStream changes: * Rather than providing a stream event listener to every call of start(), the stream listener is now provided once to the constructor and cannot be changed. * Streams can now be restarted indefinitely, even after a call to stop(). * PersistentStreamState.Stopped was removed and we just return to 'Initial' after a stop() call. * Added `closeCount` member to PersistentStream in order to avoid bleedthrough issues with auth and stream events once stop() has been called. * Calling stop() now triggers the onClose() event listener, which simplifies stream cleanup. * PersistentStreamState.Auth renamed to 'Starting' to better reflect that it encompasses both authentication and opening the stream. RemoteStore changes: * Creates streams once and just stop() / start()s them as necessary, never recreating them completely. * Added networkEnabled flag to track whether the network is enabled or not, since we no longer null out the streams. * Refactored disableNetwork() / enableNetwork() to remove stream re-creation. Misc: * Comment improvements including a state diagram on PersistentStream. * Fixed spec test shutdown to schedule via the AsyncQueue to fix sequencing order I ran into.
1 parent a80a597 commit f14ebc2

File tree

5 files changed

+278
-250
lines changed

5 files changed

+278
-250
lines changed

packages/firestore/src/remote/datastore.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WatchStreamListener, WriteStreamListener } from './persistent_stream';
12
/**
23
* Copyright 2017 Google Inc.
34
*
@@ -54,21 +55,27 @@ export class Datastore {
5455
private serializer: JsonProtoSerializer
5556
) {}
5657

57-
newPersistentWriteStream(): PersistentWriteStream {
58+
newPersistentWriteStream(
59+
listener: WriteStreamListener
60+
): PersistentWriteStream {
5861
return new PersistentWriteStream(
5962
this.queue,
6063
this.connection,
6164
this.credentials,
62-
this.serializer
65+
this.serializer,
66+
listener
6367
);
6468
}
6569

66-
newPersistentWatchStream(): PersistentListenStream {
70+
newPersistentWatchStream(
71+
listener: WatchStreamListener
72+
): PersistentListenStream {
6773
return new PersistentListenStream(
6874
this.queue,
6975
this.connection,
7076
this.credentials,
71-
this.serializer
77+
this.serializer,
78+
listener
7279
);
7380
}
7481

0 commit comments

Comments
 (0)