-
Notifications
You must be signed in to change notification settings - Fork 891
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
Add ConnectivityMonitor #1808
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f496af
connectivity monitor init
d8ef76d
remove settings
035cd89
add comments, bind this to self
0c8492d
using anonymous functions
b5d8d3d
[AUTOMATED]: Prettier Code Styling
6ae6c3b
implement test platform
e88d690
update comments
e11fa72
[AUTOMATED]: Prettier Code Styling
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/firestore/src/platform_browser/browser_connectivity_monitor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
28
packages/firestore/src/remote/connectivity_monitor_noop.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 atypeof window !== undefined
check here. As merged, I believe this change will cause issues on Electron and other browser-like environments.There was a problem hiding this comment.
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.