Skip to content

Remove enum overhead #2697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 4, 2020
2 changes: 1 addition & 1 deletion packages/firestore/src/api/user_data_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class ParsedUpdateData {
* for determining which error conditions apply during parsing and providing
* better error messages.
*/
enum UserDataSource {
const enum UserDataSource {
Set,
Update,
MergeSet,
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/core/event_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class EventManager implements SyncEngineListener {
q.canonicalId()
);

private onlineState: OnlineState = 'Unknown';
private onlineState = OnlineState.Unknown;

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

Expand Down Expand Up @@ -209,7 +209,7 @@ export class QueryListener {

private snap: ViewSnapshot | null = null;

private onlineState: OnlineState = 'Unknown';
private onlineState = OnlineState.Unknown;

constructor(
readonly query: Query,
Expand Down Expand Up @@ -300,7 +300,7 @@ export class QueryListener {

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

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

private shouldRaiseEvent(snap: ViewSnapshot): boolean {
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Code, FirestoreError } from '../util/error';
import { isNullOrUndefined } from '../util/types';
import { Target } from './target';

export enum LimitType {
export const enum LimitType {
First = 'F',
Last = 'L'
}
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
// startup. In the interim, a client should only be considered primary if
// `isPrimary` is true.
private isPrimary: undefined | boolean = undefined;
private onlineState: OnlineState = 'Unknown';
private onlineState = OnlineState.Unknown;

constructor(
private localStore: LocalStore,
Expand Down Expand Up @@ -253,7 +253,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
const viewDocChanges = view.computeDocChanges(queryResult.documents);
const synthesizedTargetChange = TargetChange.createSynthesizedTargetChangeForCurrentChange(
targetId,
current && this.onlineState !== 'Offline'
current && this.onlineState !== OnlineState.Offline
);
const viewChange = view.applyChanges(
viewDocChanges,
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/core/target_id_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { TargetId } from './types';

const RESERVED_BITS = 1;

enum GeneratorIds {
const enum GeneratorIds {
QueryCache = 0, // The target IDs for user-issued queries are even (end in 0).
SyncEngine = 1 // The target IDs for limbo detection are odd (end in 1).
}
Expand Down
14 changes: 9 additions & 5 deletions packages/firestore/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,36 @@ export type MutationBatchState = 'pending' | 'acknowledged' | 'rejected';
* primarily used by the View / EventManager code to change their behavior while
* offline (e.g. get() calls shouldn't wait for data from the server and
* snapshot events should set metadata.isFromCache=true).
*
* The string values should not be changed since they are persisted in
* WebStorage.
*/
export type OnlineState =
export const enum OnlineState {
/**
* The Firestore client is in an unknown online state. This means the client
* is either not actively trying to establish a connection or it is currently
* trying to establish a connection, but it has not succeeded or failed yet.
* Higher-level components should not operate in offline mode.
*/
| 'Unknown'
Unknown = 'Unknown',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the strings in here anymore? Can these just be numeric?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string values are persisted in WebStorage (but it is mostly transient data). I added a comment to explain this.


/**
* The client is connected and the connections are healthy. This state is
* reached after a successful connection and there has been at least one
* successful message received from the backends.
*/
| 'Online'
Online = 'Online',

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

/** The source of an online state event. */
export enum OnlineStateSource {
export const enum OnlineStateSource {
RemoteStore,
SharedClientState
}
2 changes: 1 addition & 1 deletion packages/firestore/src/core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export class View {
* ViewChange if the view's syncState changes as a result.
*/
applyOnlineStateChange(onlineState: OnlineState): ViewChange {
if (this.current && onlineState === 'Offline') {
if (this.current && onlineState === OnlineState.Offline) {
// If we're offline, set `current` to false and then call applyChanges()
// to refresh our syncState and generate a ViewChange as appropriate. We
// are guaranteed to get a new TargetChange that sets `current` back to
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/core/view_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { SortedMap } from '../util/sorted_map';
import { DocumentKeySet } from '../model/collections';
import { Query } from './query';

export enum ChangeType {
export const enum ChangeType {
Added,
Removed,
Modified,
Expand All @@ -36,7 +36,7 @@ export interface DocumentViewChange {
doc: Document;
}

export enum SyncState {
export const enum SyncState {
Local,
Synced
}
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/target_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ListenSequenceNumber, TargetId } from '../core/types';
import { ByteString } from '../util/byte_string';

/** An enumeration of the different purposes we have for targets. */
export enum TargetPurpose {
export const enum TargetPurpose {
/** A regular, normal query target. */
Listen,

Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/model/field_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface JsonObject<T> {
[name: string]: T;
}

export enum TypeOrder {
export const enum TypeOrder {
// This order is defined by the backend.
NullValue = 0,
BooleanValue = 1,
Expand All @@ -63,7 +63,7 @@ export enum TypeOrder {
}

/** Defines the return value for pending server timestamps. */
export enum ServerTimestampBehavior {
export const enum ServerTimestampBehavior {
Default,
Estimate,
Previous
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/model/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class MutationResult {
) {}
}

export enum MutationType {
export const enum MutationType {
Set,
Patch,
Transform,
Expand Down
16 changes: 8 additions & 8 deletions packages/firestore/src/remote/online_state_tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const ONLINE_STATE_TIMEOUT_MS = 10 * 1000;
*/
export class OnlineStateTracker {
/** The current OnlineState. */
private state: OnlineState = 'Unknown';
private state = OnlineState.Unknown;

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

assert(
this.onlineStateTimer === null,
Expand All @@ -99,14 +99,14 @@ export class OnlineStateTracker {
() => {
this.onlineStateTimer = null;
assert(
this.state === 'Unknown',
this.state === OnlineState.Unknown,
'Timer should be canceled if we transitioned to a different state.'
);
this.logClientOfflineWarningIfNecessary(
`Backend didn't respond within ${ONLINE_STATE_TIMEOUT_MS / 1000} ` +
`seconds.`
);
this.setAndBroadcast('Offline');
this.setAndBroadcast(OnlineState.Offline);

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

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

this.setAndBroadcast('Offline');
this.setAndBroadcast(OnlineState.Offline);
}
}
}
Expand All @@ -158,7 +158,7 @@ export class OnlineStateTracker {
this.clearOnlineStateTimer();
this.watchStreamFailures = 0;

if (newState === 'Online') {
if (newState === OnlineState.Online) {
// We've connected to watch at least once. Don't warn the developer
// about being offline going forward.
this.shouldWarnClientIsOffline = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/remote/persistent_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface WriteRequest extends api.WriteRequest {
* stop() called or
* idle timer expired
*/
enum PersistentStreamState {
const enum PersistentStreamState {
/**
* The streaming RPC is not yet running and there's no error condition.
* Calling start() will start the stream immediately without backoff.
Expand Down
16 changes: 8 additions & 8 deletions packages/firestore/src/remote/remote_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class RemoteStore implements TargetMetadataProvider {
if (this.shouldStartWatchStream()) {
this.startWatchStream();
} else {
this.onlineStateTracker.set('Unknown');
this.onlineStateTracker.set(OnlineState.Unknown);
}

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

// Set the OnlineState to Offline so get()s return from cache, etc.
this.onlineStateTracker.set('Offline');
this.onlineStateTracker.set(OnlineState.Offline);
}

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

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

/**
Expand Down Expand Up @@ -279,7 +279,7 @@ export class RemoteStore implements TargetMetadataProvider {
// Revert to OnlineState.Unknown if the watch stream is not open and we
// have no listeners, since without any listens to send we cannot
// confirm if the stream is healthy and upgrade to OnlineState.Online.
this.onlineStateTracker.set('Unknown');
this.onlineStateTracker.set(OnlineState.Unknown);
}
}
}
Expand Down Expand Up @@ -371,7 +371,7 @@ export class RemoteStore implements TargetMetadataProvider {
// No need to restart watch stream because there are no active targets.
// The online state is set to unknown because there is no active attempt
// at establishing a connection
this.onlineStateTracker.set('Unknown');
this.onlineStateTracker.set(OnlineState.Unknown);
}
}

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

if (
watchChange instanceof WatchTargetChange &&
Expand Down Expand Up @@ -708,7 +708,7 @@ export class RemoteStore implements TargetMetadataProvider {
private async restartNetwork(): Promise<void> {
this.networkEnabled = false;
await this.disableNetworkInternal();
this.onlineStateTracker.set('Unknown');
this.onlineStateTracker.set(OnlineState.Unknown);
await this.enableNetwork();
}

Expand All @@ -732,7 +732,7 @@ export class RemoteStore implements TargetMetadataProvider {
await this.enableNetwork();
} else if (!isPrimary) {
await this.disableNetworkInternal();
this.onlineStateTracker.set('Unknown');
this.onlineStateTracker.set(OnlineState.Unknown);
}
}
}
2 changes: 1 addition & 1 deletion packages/firestore/src/remote/rpc_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import * as log from '../util/log';
*
* Important! The names of these identifiers matter because the string forms
* are used for reverse lookups from the webchannel stream. Do NOT change the
* names of these identifiers.
* names of these identifiers or change this into a const enum.
*/
enum RpcCode {
OK = 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/remote/watch_change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class ExistenceFilterChange {
) {}
}

export enum WatchTargetChangeState {
export const enum WatchTargetChangeState {
NoChange,
Added,
Removed,
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/util/async_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type TimerHandle = any;
*
* The string values are used when encoding these timer IDs in JSON spec tests.
*/
export enum TimerId {
export const enum TimerId {
/** All can be used with runDelayedOperationsEarly() to run all timers. */
All = 'all',

Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/util/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { PlatformSupport } from '../platform/platform';

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

export enum LogLevel {
export const enum LogLevel {
DEBUG,
ERROR,
SILENT
Expand Down
Loading