Skip to content

Commit ac25570

Browse files
Remove enum overhead (#2697)
1 parent 4282432 commit ac25570

20 files changed

+77
-73
lines changed

packages/firestore/src/api/user_data_converter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class ParsedUpdateData {
126126
* for determining which error conditions apply during parsing and providing
127127
* better error messages.
128128
*/
129-
enum UserDataSource {
129+
const enum UserDataSource {
130130
Set,
131131
Update,
132132
MergeSet,

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 = '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 = '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 !== 'Offline';
303+
const maybeOnline = onlineState !== 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 === 'Offline';
315+
return !snap.docs.isEmpty() || onlineState === OnlineState.Offline;
316316
}
317317

318318
private shouldRaiseEvent(snap: ViewSnapshot): boolean {

packages/firestore/src/core/query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { Code, FirestoreError } from '../util/error';
3030
import { isNullOrUndefined } from '../util/types';
3131
import { Target } from './target';
3232

33-
export enum LimitType {
33+
export const enum LimitType {
3434
First = 'F',
3535
Last = 'L'
3636
}

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 = '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 !== 'Offline'
256+
current && this.onlineState !== OnlineState.Offline
257257
);
258258
const viewChange = view.applyChanges(
259259
viewDocChanges,

packages/firestore/src/core/target_id_generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { TargetId } from './types';
2020

2121
const RESERVED_BITS = 1;
2222

23-
enum GeneratorIds {
23+
const enum GeneratorIds {
2424
QueryCache = 0, // The target IDs for user-issued queries are even (end in 0).
2525
SyncEngine = 1 // The target IDs for limbo detection are odd (end in 1).
2626
}

packages/firestore/src/core/types.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,36 @@ export type MutationBatchState = 'pending' | 'acknowledged' | 'rejected';
3838
* primarily used by the View / EventManager code to change their behavior while
3939
* offline (e.g. get() calls shouldn't wait for data from the server and
4040
* snapshot events should set metadata.isFromCache=true).
41+
*
42+
* The string values should not be changed since they are persisted in
43+
* WebStorage.
4144
*/
42-
export type OnlineState =
45+
export const enum OnlineState {
4346
/**
4447
* The Firestore client is in an unknown online state. This means the client
4548
* is either not actively trying to establish a connection or it is currently
4649
* trying to establish a connection, but it has not succeeded or failed yet.
4750
* Higher-level components should not operate in offline mode.
4851
*/
49-
| 'Unknown'
52+
Unknown = 'Unknown',
5053

5154
/**
5255
* The client is connected and the connections are healthy. This state is
5356
* reached after a successful connection and there has been at least one
5457
* successful message received from the backends.
5558
*/
56-
| 'Online'
59+
Online = 'Online',
5760

5861
/**
5962
* The client is either trying to establish a connection but failing, or it
6063
* has been explicitly marked offline via a call to disableNetwork().
6164
* Higher-level components should operate in offline mode.
6265
*/
63-
| 'Offline';
66+
Offline = 'Offline'
67+
}
6468

6569
/** The source of an online state event. */
66-
export enum OnlineStateSource {
70+
export const enum OnlineStateSource {
6771
RemoteStore,
6872
SharedClientState
6973
}

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 === 'Offline') {
339+
if (this.current && onlineState === 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/core/view_snapshot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { SortedMap } from '../util/sorted_map';
2424
import { DocumentKeySet } from '../model/collections';
2525
import { Query } from './query';
2626

27-
export enum ChangeType {
27+
export const enum ChangeType {
2828
Added,
2929
Removed,
3030
Modified,
@@ -36,7 +36,7 @@ export interface DocumentViewChange {
3636
doc: Document;
3737
}
3838

39-
export enum SyncState {
39+
export const enum SyncState {
4040
Local,
4141
Synced
4242
}

packages/firestore/src/local/target_data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { ListenSequenceNumber, TargetId } from '../core/types';
2121
import { ByteString } from '../util/byte_string';
2222

2323
/** An enumeration of the different purposes we have for targets. */
24-
export enum TargetPurpose {
24+
export const enum TargetPurpose {
2525
/** A regular, normal query target. */
2626
Listen,
2727

packages/firestore/src/model/field_value.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface JsonObject<T> {
4848
[name: string]: T;
4949
}
5050

51-
export enum TypeOrder {
51+
export const enum TypeOrder {
5252
// This order is defined by the backend.
5353
NullValue = 0,
5454
BooleanValue = 1,
@@ -63,7 +63,7 @@ export enum TypeOrder {
6363
}
6464

6565
/** Defines the return value for pending server timestamps. */
66-
export enum ServerTimestampBehavior {
66+
export const enum ServerTimestampBehavior {
6767
Default,
6868
Estimate,
6969
Previous

packages/firestore/src/model/mutation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class MutationResult {
117117
) {}
118118
}
119119

120-
export enum MutationType {
120+
export const enum MutationType {
121121
Set,
122122
Patch,
123123
Transform,

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('Unknown');
90+
this.setAndBroadcast(OnlineState.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 === 'Unknown',
102+
this.state === OnlineState.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('Offline');
109+
this.setAndBroadcast(OnlineState.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 === 'Online') {
129-
this.setAndBroadcast('Unknown');
128+
if (this.state === OnlineState.Online) {
129+
this.setAndBroadcast(OnlineState.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('Offline');
145+
this.setAndBroadcast(OnlineState.Offline);
146146
}
147147
}
148148
}
@@ -158,7 +158,7 @@ export class OnlineStateTracker {
158158
this.clearOnlineStateTimer();
159159
this.watchStreamFailures = 0;
160160

161-
if (newState === 'Online') {
161+
if (newState === OnlineState.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/persistent_stream.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export interface WriteRequest extends api.WriteRequest {
6363
* stop() called or
6464
* idle timer expired
6565
*/
66-
enum PersistentStreamState {
66+
const enum PersistentStreamState {
6767
/**
6868
* The streaming RPC is not yet running and there's no error condition.
6969
* Calling start() will start the stream immediately without backoff.

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('Unknown');
194+
this.onlineStateTracker.set(OnlineState.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('Offline');
211+
this.onlineStateTracker.set(OnlineState.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('Unknown');
237+
this.onlineStateTracker.set(OnlineState.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('Unknown');
282+
this.onlineStateTracker.set(OnlineState.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('Unknown');
374+
this.onlineStateTracker.set(OnlineState.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('Online');
383+
this.onlineStateTracker.set(OnlineState.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('Unknown');
711+
this.onlineStateTracker.set(OnlineState.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('Unknown');
735+
this.onlineStateTracker.set(OnlineState.Unknown);
736736
}
737737
}
738738
}

packages/firestore/src/remote/rpc_error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import * as log from '../util/log';
2727
*
2828
* Important! The names of these identifiers matter because the string forms
2929
* are used for reverse lookups from the webchannel stream. Do NOT change the
30-
* names of these identifiers.
30+
* names of these identifiers or change this into a const enum.
3131
*/
3232
enum RpcCode {
3333
OK = 0,

packages/firestore/src/remote/watch_change.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class ExistenceFilterChange {
7474
) {}
7575
}
7676

77-
export enum WatchTargetChangeState {
77+
export const enum WatchTargetChangeState {
7878
NoChange,
7979
Added,
8080
Removed,

packages/firestore/src/util/async_queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type TimerHandle = any;
3131
*
3232
* The string values are used when encoding these timer IDs in JSON spec tests.
3333
*/
34-
export enum TimerId {
34+
export const enum TimerId {
3535
/** All can be used with runDelayedOperationsEarly() to run all timers. */
3636
All = 'all',
3737

packages/firestore/src/util/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { PlatformSupport } from '../platform/platform';
2121

2222
const logClient = new Logger('@firebase/firestore');
2323

24-
export enum LogLevel {
24+
export const enum LogLevel {
2525
DEBUG,
2626
ERROR,
2727
SILENT

0 commit comments

Comments
 (0)