Skip to content

Commit cb01df4

Browse files
Use explicit String constants for OnlineState (#2688)
1 parent c251f43 commit cb01df4

File tree

9 files changed

+62
-62
lines changed

9 files changed

+62
-62
lines changed

packages/firestore/src/core/event_manager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class EventManager implements SyncEngineListener {
5151
q.canonicalId()
5252
);
5353

54-
private onlineState: OnlineState = OnlineState.Unknown;
54+
private onlineState: OnlineState = 'Unknown';
5555

5656
private snapshotsInSyncListeners: Set<Observer<void>> = new Set();
5757

@@ -209,7 +209,7 @@ export class QueryListener {
209209

210210
private snap: ViewSnapshot | null = null;
211211

212-
private onlineState: OnlineState = OnlineState.Unknown;
212+
private onlineState: OnlineState = 'Unknown';
213213

214214
constructor(
215215
readonly query: Query,
@@ -300,7 +300,7 @@ export class QueryListener {
300300

301301
// NOTE: We consider OnlineState.Unknown as online (it should become Offline
302302
// or Online if we wait long enough).
303-
const maybeOnline = onlineState !== OnlineState.Offline;
303+
const maybeOnline = onlineState !== 'Offline';
304304
// Don't raise the event if we're online, aren't synced yet (checked
305305
// above) and are waiting for a sync.
306306
if (this.options.waitForSyncWhenOnline && maybeOnline) {
@@ -312,7 +312,7 @@ export class QueryListener {
312312
}
313313

314314
// Raise data from cache if we have any documents or we are offline
315-
return !snap.docs.isEmpty() || onlineState === OnlineState.Offline;
315+
return !snap.docs.isEmpty() || onlineState === 'Offline';
316316
}
317317

318318
private shouldRaiseEvent(snap: ViewSnapshot): boolean {

packages/firestore/src/core/sync_engine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
167167
// startup. In the interim, a client should only be considered primary if
168168
// `isPrimary` is true.
169169
private isPrimary: undefined | boolean = undefined;
170-
private onlineState: OnlineState = OnlineState.Unknown;
170+
private onlineState: OnlineState = 'Unknown';
171171

172172
constructor(
173173
private localStore: LocalStore,
@@ -253,7 +253,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
253253
const viewDocChanges = view.computeDocChanges(queryResult.documents);
254254
const synthesizedTargetChange = TargetChange.createSynthesizedTargetChangeForCurrentChange(
255255
targetId,
256-
current && this.onlineState !== OnlineState.Offline
256+
current && this.onlineState !== 'Offline'
257257
);
258258
const viewChange = view.applyChanges(
259259
viewDocChanges,

packages/firestore/src/core/types.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,28 @@ export type MutationBatchState = 'pending' | 'acknowledged' | 'rejected';
3939
* offline (e.g. get() calls shouldn't wait for data from the server and
4040
* snapshot events should set metadata.isFromCache=true).
4141
*/
42-
export enum OnlineState {
42+
export type OnlineState =
4343
/**
4444
* The Firestore client is in an unknown online state. This means the client
4545
* is either not actively trying to establish a connection or it is currently
4646
* trying to establish a connection, but it has not succeeded or failed yet.
4747
* Higher-level components should not operate in offline mode.
4848
*/
49-
Unknown,
49+
| 'Unknown'
5050

5151
/**
5252
* The client is connected and the connections are healthy. This state is
5353
* reached after a successful connection and there has been at least one
5454
* successful message received from the backends.
5555
*/
56-
Online,
56+
| 'Online'
5757

5858
/**
5959
* The client is either trying to establish a connection but failing, or it
6060
* has been explicitly marked offline via a call to disableNetwork().
6161
* Higher-level components should operate in offline mode.
6262
*/
63-
Offline
64-
}
63+
| 'Offline';
6564

6665
/** The source of an online state event. */
6766
export enum OnlineStateSource {

packages/firestore/src/core/view.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export class View {
336336
* ViewChange if the view's syncState changes as a result.
337337
*/
338338
applyOnlineStateChange(onlineState: OnlineState): ViewChange {
339-
if (this.current && onlineState === OnlineState.Offline) {
339+
if (this.current && onlineState === 'Offline') {
340340
// If we're offline, set `current` to false and then call applyChanges()
341341
// to refresh our syncState and generate a ViewChange as appropriate. We
342342
// are guaranteed to get a new TargetChange that sets `current` back to

packages/firestore/src/local/shared_client_state.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,14 @@ export class SharedOnlineState {
409409

410410
const validData =
411411
typeof onlineState === 'object' &&
412-
onlineState.onlineState in OnlineState &&
412+
['Unknown', 'Online', 'Offline'].indexOf(onlineState.onlineState) !==
413+
-1 &&
413414
typeof onlineState.clientId === 'string';
414415

415416
if (validData) {
416417
return new SharedOnlineState(
417418
onlineState.clientId,
418-
OnlineState[onlineState.onlineState as keyof typeof OnlineState]
419+
onlineState.onlineState as OnlineState
419420
);
420421
} else {
421422
error(LOG_TAG, `Failed to parse online state: ${value}`);
@@ -860,7 +861,7 @@ export class WebStorageSharedClientState implements SharedClientState {
860861
private persistOnlineState(onlineState: OnlineState): void {
861862
const entry: SharedOnlineStateSchema = {
862863
clientId: this.localClientId,
863-
onlineState: OnlineState[onlineState]
864+
onlineState
864865
};
865866
this.storage.setItem(this.onlineStateKey, JSON.stringify(entry));
866867
}

packages/firestore/src/remote/online_state_tracker.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const ONLINE_STATE_TIMEOUT_MS = 10 * 1000;
5050
*/
5151
export class OnlineStateTracker {
5252
/** The current OnlineState. */
53-
private state = OnlineState.Unknown;
53+
private state: OnlineState = 'Unknown';
5454

5555
/**
5656
* A count of consecutive failures to open the stream. If it reaches the
@@ -87,7 +87,7 @@ export class OnlineStateTracker {
8787
*/
8888
handleWatchStreamStart(): void {
8989
if (this.watchStreamFailures === 0) {
90-
this.setAndBroadcast(OnlineState.Unknown);
90+
this.setAndBroadcast('Unknown');
9191

9292
assert(
9393
this.onlineStateTimer === null,
@@ -99,14 +99,14 @@ export class OnlineStateTracker {
9999
() => {
100100
this.onlineStateTimer = null;
101101
assert(
102-
this.state === OnlineState.Unknown,
102+
this.state === 'Unknown',
103103
'Timer should be canceled if we transitioned to a different state.'
104104
);
105105
this.logClientOfflineWarningIfNecessary(
106106
`Backend didn't respond within ${ONLINE_STATE_TIMEOUT_MS / 1000} ` +
107107
`seconds.`
108108
);
109-
this.setAndBroadcast(OnlineState.Offline);
109+
this.setAndBroadcast('Offline');
110110

111111
// NOTE: handleWatchStreamFailure() will continue to increment
112112
// watchStreamFailures even though we are already marked Offline,
@@ -125,8 +125,8 @@ export class OnlineStateTracker {
125125
* actually transition to the 'Offline' state.
126126
*/
127127
handleWatchStreamFailure(error: FirestoreError): void {
128-
if (this.state === OnlineState.Online) {
129-
this.setAndBroadcast(OnlineState.Unknown);
128+
if (this.state === 'Online') {
129+
this.setAndBroadcast('Unknown');
130130

131131
// To get to OnlineState.Online, set() must have been called which would
132132
// have reset our heuristics.
@@ -142,7 +142,7 @@ export class OnlineStateTracker {
142142
`times. Most recent error: ${error.toString()}`
143143
);
144144

145-
this.setAndBroadcast(OnlineState.Offline);
145+
this.setAndBroadcast('Offline');
146146
}
147147
}
148148
}
@@ -158,7 +158,7 @@ export class OnlineStateTracker {
158158
this.clearOnlineStateTimer();
159159
this.watchStreamFailures = 0;
160160

161-
if (newState === OnlineState.Online) {
161+
if (newState === 'Online') {
162162
// We've connected to watch at least once. Don't warn the developer
163163
// about being offline going forward.
164164
this.shouldWarnClientIsOffline = false;

packages/firestore/src/remote/remote_store.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export class RemoteStore implements TargetMetadataProvider {
191191
if (this.shouldStartWatchStream()) {
192192
this.startWatchStream();
193193
} else {
194-
this.onlineStateTracker.set(OnlineState.Unknown);
194+
this.onlineStateTracker.set('Unknown');
195195
}
196196

197197
// This will start the write stream if necessary.
@@ -208,7 +208,7 @@ export class RemoteStore implements TargetMetadataProvider {
208208
await this.disableNetworkInternal();
209209

210210
// Set the OnlineState to Offline so get()s return from cache, etc.
211-
this.onlineStateTracker.set(OnlineState.Offline);
211+
this.onlineStateTracker.set('Offline');
212212
}
213213

214214
private async disableNetworkInternal(): Promise<void> {
@@ -234,7 +234,7 @@ export class RemoteStore implements TargetMetadataProvider {
234234

235235
// Set the OnlineState to Unknown (rather than Offline) to avoid potentially
236236
// triggering spurious listener events with cached data, etc.
237-
this.onlineStateTracker.set(OnlineState.Unknown);
237+
this.onlineStateTracker.set('Unknown');
238238
}
239239

240240
/**
@@ -279,7 +279,7 @@ export class RemoteStore implements TargetMetadataProvider {
279279
// Revert to OnlineState.Unknown if the watch stream is not open and we
280280
// have no listeners, since without any listens to send we cannot
281281
// confirm if the stream is healthy and upgrade to OnlineState.Online.
282-
this.onlineStateTracker.set(OnlineState.Unknown);
282+
this.onlineStateTracker.set('Unknown');
283283
}
284284
}
285285
}
@@ -371,7 +371,7 @@ export class RemoteStore implements TargetMetadataProvider {
371371
// No need to restart watch stream because there are no active targets.
372372
// The online state is set to unknown because there is no active attempt
373373
// at establishing a connection
374-
this.onlineStateTracker.set(OnlineState.Unknown);
374+
this.onlineStateTracker.set('Unknown');
375375
}
376376
}
377377

@@ -380,7 +380,7 @@ export class RemoteStore implements TargetMetadataProvider {
380380
snapshotVersion: SnapshotVersion
381381
): Promise<void> {
382382
// Mark the client as online since we got a message from the server
383-
this.onlineStateTracker.set(OnlineState.Online);
383+
this.onlineStateTracker.set('Online');
384384

385385
if (
386386
watchChange instanceof WatchTargetChange &&
@@ -708,7 +708,7 @@ export class RemoteStore implements TargetMetadataProvider {
708708
private async restartNetwork(): Promise<void> {
709709
this.networkEnabled = false;
710710
await this.disableNetworkInternal();
711-
this.onlineStateTracker.set(OnlineState.Unknown);
711+
this.onlineStateTracker.set('Unknown');
712712
await this.enableNetwork();
713713
}
714714

@@ -732,7 +732,7 @@ export class RemoteStore implements TargetMetadataProvider {
732732
await this.enableNetwork();
733733
} else if (!isPrimary) {
734734
await this.disableNetworkInternal();
735-
this.onlineStateTracker.set(OnlineState.Unknown);
735+
this.onlineStateTracker.set('Unknown');
736736
}
737737
}
738738
}

packages/firestore/test/unit/core/event_manager.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ describe('EventManager', () => {
144144
const eventManager = new EventManager(syncEngineSpy);
145145

146146
await eventManager.listen(fakeListener1);
147-
expect(events).to.deep.equal([OnlineState.Unknown]);
148-
eventManager.onOnlineStateChange(OnlineState.Online);
149-
expect(events).to.deep.equal([OnlineState.Unknown, OnlineState.Online]);
147+
expect(events).to.deep.equal(['Unknown']);
148+
eventManager.onOnlineStateChange('Online');
149+
expect(events).to.deep.equal(['Unknown', 'Online']);
150150
});
151151
});
152152

@@ -478,10 +478,10 @@ describe('QueryListener', () => {
478478
const snap3 = view.applyChanges(changes3, true, ackTarget(doc1, doc2))
479479
.snapshot!;
480480

481-
listener.applyOnlineStateChange(OnlineState.Online); // no event
481+
listener.applyOnlineStateChange('Online'); // no event
482482
listener.onViewSnapshot(snap1); // no event
483-
listener.applyOnlineStateChange(OnlineState.Unknown); // no event
484-
listener.applyOnlineStateChange(OnlineState.Online); // no event
483+
listener.applyOnlineStateChange('Unknown'); // no event
484+
listener.applyOnlineStateChange('Online'); // no event
485485
listener.onViewSnapshot(snap2); // no event
486486
listener.onViewSnapshot(snap3); // event because synced
487487

@@ -516,11 +516,11 @@ describe('QueryListener', () => {
516516
const changes2 = view.computeDocChanges(documentUpdates(doc2));
517517
const snap2 = view.applyChanges(changes2, true).snapshot!;
518518

519-
listener.applyOnlineStateChange(OnlineState.Online); // no event
519+
listener.applyOnlineStateChange('Online'); // no event
520520
listener.onViewSnapshot(snap1); // no event
521-
listener.applyOnlineStateChange(OnlineState.Offline); // event
522-
listener.applyOnlineStateChange(OnlineState.Online); // no event
523-
listener.applyOnlineStateChange(OnlineState.Offline); // no event
521+
listener.applyOnlineStateChange('Offline'); // event
522+
listener.applyOnlineStateChange('Online'); // no event
523+
listener.applyOnlineStateChange('Offline'); // no event
524524
listener.onViewSnapshot(snap2); // another event
525525

526526
const expectedSnap1 = {
@@ -554,9 +554,9 @@ describe('QueryListener', () => {
554554
const changes1 = view.computeDocChanges(documentUpdates());
555555
const snap1 = view.applyChanges(changes1, true).snapshot!;
556556

557-
listener.applyOnlineStateChange(OnlineState.Online); // no event
557+
listener.applyOnlineStateChange('Online'); // no event
558558
listener.onViewSnapshot(snap1); // no event
559-
listener.applyOnlineStateChange(OnlineState.Offline); // event
559+
listener.applyOnlineStateChange('Offline'); // event
560560

561561
const expectedSnap = {
562562
query,
@@ -580,7 +580,7 @@ describe('QueryListener', () => {
580580
const changes1 = view.computeDocChanges(documentUpdates());
581581
const snap1 = view.applyChanges(changes1, true).snapshot!;
582582

583-
listener.applyOnlineStateChange(OnlineState.Offline);
583+
listener.applyOnlineStateChange('Offline');
584584
listener.onViewSnapshot(snap1);
585585

586586
const expectedSnap = {

0 commit comments

Comments
 (0)