Skip to content
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

Add ConnectivityMonitor #1808

Merged
merged 8 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/firestore/src/platform/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ProtoByteString } from '../core/types';
import { Connection } from '../remote/connection';
import { JsonProtoSerializer } from '../remote/serializer';
import { fail } from '../util/assert';
import { ConnectivityMonitor } from './../remote/connectivity_monitor';

/**
* Provides a common interface to load anything platform dependent, e.g.
Expand All @@ -31,6 +32,7 @@ import { fail } from '../util/assert';
// use in our client.
export interface Platform {
loadConnection(databaseInfo: DatabaseInfo): Promise<Connection>;
newConnectivityMonitor(): ConnectivityMonitor;
newSerializer(databaseId: DatabaseId): JsonProtoSerializer;

/** Formats an object as a JSON string, suitable for logging. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @license
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { debug } from '../util/log';
import {
ConnectivityMonitor,
ConnectivityMonitorCallback,
NetworkStatus
} from './../remote/connectivity_monitor';

const LOG_TAG = 'ConnectivityMonitor';

/**
* Browser implementation of ConnectivityMonitor.
*/
export class BrowserConnectivityMonitor implements ConnectivityMonitor {
private readonly networkAvailableListener = () => this.onNetworkAvailable();
private readonly networkUnavailableListener = () =>
this.onNetworkUnavailable();
private callbacks: ConnectivityMonitorCallback[] = [];

constructor() {
this.configureNetworkMonitoring();
}

addCallback(callback: (status: NetworkStatus) => void): void {
this.callbacks.push(callback);
}

shutdown(): void {
window.removeEventListener('online', this.networkAvailableListener);
window.removeEventListener('offline', this.networkUnavailableListener);
}

private configureNetworkMonitoring(): void {
window.addEventListener('online', this.networkAvailableListener);
Copy link
Contributor

Choose a reason for hiding this comment

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

@thebrianchen I believe we need to fix this still as per our earlier discussion.

Ideally, you would change BrowserWindow to only return BrowserConnectivityMonitor if window is defined. The smaller fix would be to add a typeof window !== undefined check here. As merged, I believe this change will cause issues on Electron and other browser-like environments.

Copy link

@rauldeheer rauldeheer May 27, 2019

Choose a reason for hiding this comment

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

This is probably causing issue #1824.

window.addEventListener('offline', this.networkUnavailableListener);
}

private onNetworkAvailable(): void {
debug(LOG_TAG, 'Network connectivity changed: AVAILABLE');
for (const callback of this.callbacks) {
callback(NetworkStatus.AVAILABLE);
}
}

private onNetworkUnavailable(): void {
debug(LOG_TAG, 'Network connectivity changed: UNAVAILABLE');
for (const callback of this.callbacks) {
callback(NetworkStatus.UNAVAILABLE);
}
}
}
6 changes: 6 additions & 0 deletions packages/firestore/src/platform_browser/browser_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import { DatabaseId, DatabaseInfo } from '../core/database_info';
import { Platform } from '../platform/platform';
import { Connection } from '../remote/connection';
import { JsonProtoSerializer } from '../remote/serializer';
import { ConnectivityMonitor } from './../remote/connectivity_monitor';

import { BrowserConnectivityMonitor } from './browser_connectivity_monitor';
import { WebChannelConnection } from './webchannel_connection';

export class BrowserPlatform implements Platform {
Expand All @@ -43,6 +45,10 @@ export class BrowserPlatform implements Platform {
return Promise.resolve(new WebChannelConnection(databaseInfo));
}

newConnectivityMonitor(): ConnectivityMonitor {
return new BrowserConnectivityMonitor();
}

newSerializer(databaseId: DatabaseId): JsonProtoSerializer {
return new JsonProtoSerializer(databaseId, { useProto3Json: true });
}
Expand Down
6 changes: 6 additions & 0 deletions packages/firestore/src/platform_node/node_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { Platform } from '../platform/platform';
import { Connection } from '../remote/connection';
import { JsonProtoSerializer } from '../remote/serializer';
import { Code, FirestoreError } from '../util/error';
import { ConnectivityMonitor } from './../remote/connectivity_monitor';
import { NoopConnectivityMonitor } from './../remote/connectivity_monitor_noop';

import { GrpcConnection } from './grpc_connection';
import { loadProtos } from './load_protos';
Expand All @@ -46,6 +48,10 @@ export class NodePlatform implements Platform {
return Promise.resolve(new GrpcConnection(protos, databaseInfo));
}

newConnectivityMonitor(): ConnectivityMonitor {
return new NoopConnectivityMonitor();
}

newSerializer(partitionId: DatabaseId): JsonProtoSerializer {
return new JsonProtoSerializer(partitionId, { useProto3Json: false });
}
Expand Down
51 changes: 51 additions & 0 deletions packages/firestore/src/remote/connectivity_monitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* The set of network states is deliberately simplified -- we only care about
* states such that transition between them should break currently
* established connections.
*/
export const enum NetworkStatus {
thebrianchen marked this conversation as resolved.
Show resolved Hide resolved
AVAILABLE,
UNAVAILABLE
}

export type ConnectivityMonitorCallback = (status: NetworkStatus) => void;

/**
* A base class for monitoring changes in network connectivity; it is expected
* that each platform will have its own system-dependent implementation.
*/
export interface ConnectivityMonitor {
/**
* Adds a callback to be called when connectivity changes.
*
* Callbacks are not made on the initial state of connectivity, since this
* monitor is primarily used for resetting backoff in the remote store when
* connectivity changes. As such, the initial connectivity state is
* irrelevant here.
*/
addCallback(callback: ConnectivityMonitorCallback): void;
Copy link
Contributor

Choose a reason for hiding this comment

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

More comments on what these do please.

Specifically, does the callback get called on the initial state?


/**
* Stops monitoring connectivity. After this call completes, no further
* callbacks will be triggered. After shutdown() is called, no further calls
* are allowed on this instance.
*/
shutdown(): void;
}
28 changes: 28 additions & 0 deletions packages/firestore/src/remote/connectivity_monitor_noop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2019 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ConnectivityMonitor, NetworkStatus } from './connectivity_monitor';

export class NoopConnectivityMonitor implements ConnectivityMonitor {
addCallback(callback: (status: NetworkStatus) => void): void {
// No-op.
}

shutdown(): void {
// No-op.
}
}
6 changes: 6 additions & 0 deletions packages/firestore/test/util/test_platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { Platform } from '../../src/platform/platform';
import { Connection } from '../../src/remote/connection';
import { JsonProtoSerializer } from '../../src/remote/serializer';
import { assert, fail } from '../../src/util/assert';
import { ConnectivityMonitor } from './../../src/remote/connectivity_monitor';
import { NoopConnectivityMonitor } from './../../src/remote/connectivity_monitor_noop';

/**
* `Window` fake that implements the event and storage API that is used by
Expand Down Expand Up @@ -245,6 +247,10 @@ export class TestPlatform implements Platform {
return this.basePlatform.loadConnection(databaseInfo);
}

newConnectivityMonitor(): ConnectivityMonitor {
return new NoopConnectivityMonitor();
}

newSerializer(databaseId: DatabaseId): JsonProtoSerializer {
return this.basePlatform.newSerializer(databaseId);
}
Expand Down