-
Notifications
You must be signed in to change notification settings - Fork 963
Manage HTTP connections based on tab visibility #9202
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
Draft
Samruddhi90
wants to merge
5
commits into
realtime-backoff
Choose a base branch
from
realtime-visibility-api
base: realtime-backoff
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+233
−7
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c16eadd
Added the visibilityMonitor
Samruddhi90 4002ed9
minor changes
Samruddhi90 52686fc
Merge branch 'realtime-backoff' into realtime-visibility-api
Samruddhi90 6105412
resolving the spacing problem
Samruddhi90 a98f140
Merge branch 'realtime-backoff' into realtime-visibility-api
Samruddhi90 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 hidden or 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,104 @@ | ||
/** | ||
* @license | ||
* Copyright 2025 Google LLC | ||
* | ||
* 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 { assert } from '@firebase/util'; | ||
|
||
// TODO: Consolidate the Visibility monitoring API code into a shared utility function in firebase/util to be used by both packages/database and packages/remote-config. | ||
/** | ||
* Base class to be used if you want to emit events. Call the constructor with | ||
* the set of allowed event names. | ||
*/ | ||
export abstract class EventEmitter { | ||
private listeners_: { | ||
[eventType: string]: Array<{ | ||
callback(...args: unknown[]): void; | ||
context: unknown; | ||
}>; | ||
} = {}; | ||
|
||
constructor(private allowedEvents_: string[]) { | ||
assert( | ||
Array.isArray(allowedEvents_) && allowedEvents_.length > 0, | ||
'Requires a non-empty array' | ||
); | ||
} | ||
|
||
/** | ||
* To be overridden by derived classes in order to fire an initial event when | ||
* somebody subscribes for data. | ||
* | ||
* @returns {Array.<*>} Array of parameters to trigger initial event with. | ||
*/ | ||
abstract getInitialEvent(eventType: string): unknown[]; | ||
|
||
/** | ||
* To be called by derived classes to trigger events. | ||
*/ | ||
protected trigger(eventType: string, ...varArgs: unknown[]): void { | ||
if (Array.isArray(this.listeners_[eventType])) { | ||
// Clone the list, since callbacks could add/remove listeners. | ||
const listeners = [...this.listeners_[eventType]]; | ||
|
||
for (let i = 0; i < listeners.length; i++) { | ||
listeners[i].callback.apply(listeners[i].context, varArgs); | ||
} | ||
} | ||
} | ||
|
||
on( | ||
eventType: string, | ||
callback: (a: unknown) => void, | ||
context: unknown | ||
): void { | ||
this.validateEventType_(eventType); | ||
this.listeners_[eventType] = this.listeners_[eventType] || []; | ||
this.listeners_[eventType].push({ callback, context }); | ||
|
||
const eventData = this.getInitialEvent(eventType); | ||
if (eventData) { | ||
//@ts-ignore | ||
callback.apply(context, eventData); | ||
} | ||
} | ||
|
||
off( | ||
eventType: string, | ||
callback: (a: unknown) => void, | ||
context: unknown | ||
): void { | ||
this.validateEventType_(eventType); | ||
const listeners = this.listeners_[eventType] || []; | ||
for (let i = 0; i < listeners.length; i++) { | ||
if ( | ||
listeners[i].callback === callback && | ||
(!context || context === listeners[i].context) | ||
) { | ||
listeners.splice(i, 1); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
private validateEventType_(eventType: string): void { | ||
assert( | ||
this.allowedEvents_.find(et => { | ||
return et === eventType; | ||
}), | ||
'Unknown event: ' + eventType | ||
); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,86 @@ | ||
/** | ||
* @license | ||
* Copyright 2025 Google LLC | ||
* | ||
* 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 { assert } from '@firebase/util'; | ||
|
||
import { EventEmitter } from './eventEmitter'; | ||
|
||
declare const document: Document; | ||
|
||
// TODO: Consolidate the Visibility monitoring API code into a shared utility function in firebase/util to be used by both packages/database and packages/remote-config. | ||
export class VisibilityMonitor extends EventEmitter { | ||
private visible_: boolean; | ||
|
||
static getInstance(): VisibilityMonitor { | ||
return new VisibilityMonitor(); | ||
} | ||
|
||
constructor() { | ||
super(['visible']); | ||
let hidden: string; | ||
let visibilityChange: string; | ||
if ( | ||
typeof document !== 'undefined' && | ||
typeof document.addEventListener !== 'undefined' | ||
) { | ||
if (typeof document['hidden'] !== 'undefined') { | ||
// Opera 12.10 and Firefox 18 and later support | ||
visibilityChange = 'visibilitychange'; | ||
hidden = 'hidden'; | ||
} // @ts-ignore | ||
else if (typeof document['mozHidden'] !== 'undefined') { | ||
visibilityChange = 'mozvisibilitychange'; | ||
hidden = 'mozHidden'; | ||
} // @ts-ignore | ||
else if (typeof document['msHidden'] !== 'undefined') { | ||
visibilityChange = 'msvisibilitychange'; | ||
hidden = 'msHidden'; | ||
} // @ts-ignore | ||
else if (typeof document['webkitHidden'] !== 'undefined') { | ||
visibilityChange = 'webkitvisibilitychange'; | ||
hidden = 'webkitHidden'; | ||
} | ||
} | ||
|
||
// Initially, we always assume we are visible. This ensures that in browsers | ||
// without page visibility support or in cases where we are never visible | ||
// (e.g. chrome extension), we act as if we are visible, i.e. don't delay | ||
// reconnects | ||
this.visible_ = true; | ||
|
||
// @ts-ignore | ||
if (visibilityChange) { | ||
document.addEventListener( | ||
visibilityChange, | ||
() => { | ||
// @ts-ignore | ||
const visible = !document[hidden]; | ||
if (visible !== this.visible_) { | ||
this.visible_ = visible; | ||
this.trigger('visible', visible); | ||
} | ||
}, | ||
false | ||
); | ||
} | ||
} | ||
|
||
getInitialEvent(eventType: string): boolean[] { | ||
assert(eventType === 'visible', 'Unknown event type: ' + eventType); | ||
return [this.visible_]; | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.