Repository navigation
Fix client startup, session and settings lifecycle #223
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
base: fix/queue-submission-reliability
Are you sure you want to change the base?
Changes from all commits
f731623
eda30ab
275c6bc
ed4cdbf
4e9b490
e66c52d
23abcbd
2c45ed9
7059a05
ea0e8fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,14 +13,22 @@ export class BrowserLifeCyclePlugin implements IEventPlugin { | |
|
|
||
| this._client = context.client; | ||
|
|
||
| globalThis.addEventListener("beforeunload", () => { | ||
| // pagehide fires reliably on mobile and doesn't stop the page from entering the back/forward cache, unlike beforeunload. | ||
| globalThis.addEventListener("pagehide", () => { | ||
|
Member
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. beforeunload fires before pagehide and gives more time for submission.
Member
Author
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. I kept
If you still want |
||
| if (this._client?.config.sessionsEnabled) { | ||
| void this._client?.submitSessionEnd(); | ||
| } | ||
|
|
||
| void this._client?.suspend(); | ||
| }); | ||
|
|
||
| // A page restored from the back/forward cache starts a new session. | ||
| globalThis.addEventListener("pageshow", (event: PageTransitionEvent) => { | ||
| if (event.persisted) { | ||
| void this._client?.startup(); | ||
| } | ||
| }); | ||
|
|
||
| document.addEventListener("visibilitychange", () => { | ||
| if (document.visibilityState === "visible") { | ||
| void this._client?.startup(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { afterEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| import { ExceptionlessClient } from "@exceptionless/core"; | ||
|
|
||
| import { BrowserLifeCyclePlugin } from "../../src/plugins/BrowserLifeCyclePlugin.js"; | ||
|
|
||
| describe("BrowserLifeCyclePlugin", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| async function startPlugin() { | ||
| const listeners = new Map<string, EventListener>(); | ||
| vi.spyOn(globalThis, "addEventListener").mockImplementation((type: string, listener: EventListenerOrEventListenerObject) => { | ||
| listeners.set(type, listener as EventListener); | ||
| }); | ||
|
|
||
| const client = new ExceptionlessClient(); | ||
| client.config.sessionsEnabled = true; | ||
| const suspend = vi.spyOn(client, "suspend").mockResolvedValue(); | ||
| const startup = vi.spyOn(client, "startup").mockResolvedValue(); | ||
| const submitSessionEnd = vi.spyOn(client, "submitSessionEnd").mockResolvedValue(); | ||
|
|
||
| await new BrowserLifeCyclePlugin().startup({ | ||
| client, | ||
| log: client.config.services.log | ||
| }); | ||
|
|
||
| return { | ||
| listeners, | ||
| suspend, | ||
| startup, | ||
| submitSessionEnd | ||
| }; | ||
| } | ||
|
|
||
| test("ends the session and flushes on pagehide instead of beforeunload", async () => { | ||
| const { listeners, suspend, submitSessionEnd } = await startPlugin(); | ||
|
|
||
| expect(listeners.has("beforeunload")).toBe(false); | ||
| listeners.get("pagehide")?.(new Event("pagehide")); | ||
|
|
||
| expect(submitSessionEnd).toHaveBeenCalledTimes(1); | ||
| expect(suspend).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test("resumes when the page is restored from the back/forward cache", async () => { | ||
| const { listeners, startup } = await startPlugin(); | ||
| const pageshow = listeners.get("pageshow"); | ||
|
|
||
| pageshow?.( | ||
| Object.assign(new Event("pageshow"), { | ||
| persisted: false | ||
| }) | ||
| ); | ||
| expect(startup).not.toHaveBeenCalled(); | ||
|
|
||
| pageshow?.( | ||
| Object.assign(new Event("pageshow"), { | ||
| persisted: true | ||
| }) | ||
| ); | ||
| expect(startup).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
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.
not sure why we had this check that we removed wasn't sure if was for double init
Uh oh!
There was an error while loading. Please reload this page.
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.
It wasn't a double-init guard.
_initializedalready covers that, andaddPluginskips a plugin whose name is already registered. The check only skipped registering the browser plugins whenstartup()was called with no arguments, for example a no-argstartup()afterconfig.apiKey = ..., which then ran without global error capture. The test "should configure browser plugins when startup is called without arguments" covers it.