-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add visibility handling to allow proactive event flushing. (#607)
Additionally use the `keep-alive` setting of fetch to ensure delivery of events even when the page may be closing. Also better encapsulating browser APIs to ensure that the SDK can operate smoothly in service workers and browser extensions. Also set browser appropriate flush interval. Currently no testing for the state detector because of: jestjs/jest#10025 I think we need to switch to a babel based Jest setup to get things working better. So far Jest has not been a pleasant experience for ESM.
- Loading branch information
1 parent
04d347b
commit 819a311
Showing
13 changed files
with
216 additions
and
20 deletions.
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
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,118 @@ | ||
/** | ||
* All access to browser specific APIs should be limited to this file. | ||
* Care should be taken to ensure that any given method will work in the service worker API. So if | ||
* something isn't available in the service worker API attempt to provide reasonable defaults. | ||
*/ | ||
|
||
export function isDocument() { | ||
return typeof document !== undefined; | ||
} | ||
|
||
export function isWindow() { | ||
return typeof window !== undefined; | ||
} | ||
|
||
/** | ||
* Register an event handler on the document. If there is no document, such as when running in | ||
* a service worker, then no operation is performed. | ||
* | ||
* @param type The event type to register a handler for. | ||
* @param listener The handler to register. | ||
* @param options Event registration options. | ||
* @returns a function which unregisters the handler. | ||
*/ | ||
export function addDocumentEventListener( | ||
type: string, | ||
listener: (this: Document, ev: Event) => any, | ||
options?: boolean | AddEventListenerOptions, | ||
): () => void { | ||
if (isDocument()) { | ||
document.addEventListener(type, listener, options); | ||
return () => { | ||
document.removeEventListener(type, listener, options); | ||
}; | ||
} | ||
// No document, so no need to unregister anything. | ||
return () => {}; | ||
} | ||
|
||
/** | ||
* Register an event handler on the window. If there is no window, such as when running in | ||
* a service worker, then no operation is performed. | ||
* | ||
* @param type The event type to register a handler for. | ||
* @param listener The handler to register. | ||
* @param options Event registration options. | ||
* @returns a function which unregisters the handler. | ||
*/ | ||
export function addWindowEventListener( | ||
type: string, | ||
listener: (this: Document, ev: Event) => any, | ||
options?: boolean | AddEventListenerOptions, | ||
): () => void { | ||
if (isDocument()) { | ||
window.addEventListener(type, listener, options); | ||
return () => { | ||
window.removeEventListener(type, listener, options); | ||
}; | ||
} | ||
// No document, so no need to unregister anything. | ||
return () => {}; | ||
} | ||
|
||
/** | ||
* For non-window code this will always be an empty string. | ||
*/ | ||
export function getHref(): string { | ||
if (isWindow()) { | ||
return window.location.href; | ||
} | ||
return ''; | ||
} | ||
|
||
/** | ||
* For non-window code this will always be an empty string. | ||
*/ | ||
export function getLocationSearch(): string { | ||
if (isWindow()) { | ||
return window.location.search; | ||
} | ||
return ''; | ||
} | ||
|
||
/** | ||
* For non-window code this will always be an empty string. | ||
*/ | ||
export function getLocationHash(): string { | ||
if (isWindow()) { | ||
return window.location.hash; | ||
} | ||
return ''; | ||
} | ||
|
||
export function getCrypto(): Crypto { | ||
if (typeof crypto !== undefined) { | ||
return crypto; | ||
} | ||
// This would indicate running in an environment that doesn't have window.crypto or self.crypto. | ||
throw Error('Access to a web crypto API is required'); | ||
} | ||
|
||
/** | ||
* Get the visibility state. For non-documents this will always be 'invisible'. | ||
* | ||
* @returns The document visibility. | ||
*/ | ||
export function getVisibility(): string { | ||
if (isDocument()) { | ||
return document.visibilityState; | ||
} | ||
return 'visibile'; | ||
} | ||
|
||
export function querySelectorAll(selector: string): NodeListOf<Element> | undefined { | ||
if (isDocument()) { | ||
return document.querySelectorAll(selector); | ||
} | ||
return undefined; | ||
} |
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,27 @@ | ||
import { addDocumentEventListener, addWindowEventListener, getVisibility } from './BrowserApi'; | ||
|
||
export function registerStateDetection(requestFlush: () => void): () => void { | ||
// When the visibility of the page changes to hidden we want to flush any pending events. | ||
// | ||
// This is handled with visibility, instead of beforeunload/unload | ||
// because those events are not compatible with the bfcache and are unlikely | ||
// to be called in many situations. For more information see: https://developer.chrome.com/blog/page-lifecycle-api/ | ||
// | ||
// Redundancy is included by using both the visibilitychange handler as well as | ||
// pagehide, because different browsers, and versions have different bugs with each. | ||
// This also may provide more opportunity for the events to get flushed. | ||
// | ||
const handleVisibilityChange = () => { | ||
if (getVisibility() === 'hidden') { | ||
requestFlush(); | ||
} | ||
}; | ||
|
||
const removeDocListener = addDocumentEventListener('visibilitychange', handleVisibilityChange); | ||
const removeWindowListener = addWindowEventListener('pagehide', requestFlush); | ||
|
||
return () => { | ||
removeDocListener(); | ||
removeWindowListener(); | ||
}; | ||
} |
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
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
Oops, something went wrong.