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
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 |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ import { BrowserRequestInfoPlugin } from "./plugins/BrowserRequestInfoPlugin.js" | |
| export class BrowserExceptionlessClient extends ExceptionlessClient { | ||
| public async startup(configurationOrApiKey?: ((config: Configuration) => void) | string): Promise<void> { | ||
| const config = this.config; | ||
| if (configurationOrApiKey && !this._initialized) { | ||
| if (!this._initialized) { | ||
| config.useLocalStorage(); | ||
|
Comment on lines
+13
to
14
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.
When a browser client is configured beforehand with a custom AGENTS.md reference: AGENTS.md:L19-L19 Useful? React with 👍 / 👎. |
||
|
|
||
| config.addPlugin(new BrowserGlobalHandlerPlugin()); | ||
|
|
||
| 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); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -347,7 +347,8 @@ export class Configuration { | |
| * The list of plugins that will be used in this configuration. | ||
| */ | ||
| public get plugins(): IEventPlugin[] { | ||
| return this._plugins.sort((p1: IEventPlugin, p2: IEventPlugin) => { | ||
| // Sort a copy: callers iterate this list while plugins may add or remove plugins. | ||
| return [...this._plugins].sort((p1: IEventPlugin, p2: IEventPlugin) => { | ||
|
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. sort mutates but js is single threaded (browsers) so how would there ever be an issue with this? Did we ever hit an issue with this with node??
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. Not a threading problem, and I have no report of it in the wild. The getter sorted |
||
| if (p1 == null && p2 == null) { | ||
| return 0; | ||
| } | ||
|
|
@@ -521,16 +522,58 @@ export class Configuration { | |
| } | ||
| } | ||
|
|
||
| private originalSettings?: Record<string, string>; | ||
| /** Local values of settings the server has overridden, restored when the server stops sending them. */ | ||
| private _overriddenLocalSettings: Record<string, string> = {}; | ||
| /** The server settings that were applied last. */ | ||
| private _appliedServerSettings: Record<string, string> = {}; | ||
|
|
||
| public applyServerSettings(serverSettings: ServerSettings): void { | ||
| if (!this.originalSettings) { | ||
| this.originalSettings = JSON.parse(JSON.stringify(this.settings)) as Record<string, string>; | ||
| this.services.log.trace(`Applying saved settings: v${serverSettings?.version}`); | ||
|
|
||
| const received: Record<string, string> = {}; | ||
| const value: unknown = serverSettings?.settings; | ||
| if (value && typeof value === "object") { | ||
| for (const [key, setting] of Object.entries(value)) { | ||
| if (typeof setting === "string") { | ||
| received[key] = setting; | ||
| } else { | ||
| this.services.log.warn(`Ignoring server setting "${key}": expected a string value`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Settings still holding the value the server last sent are server-owned: drop them (restoring any local value they | ||
| // replaced) so settings the server no longer sends are removed. Anything else, including local changes made after | ||
| // startup, is kept. | ||
| const settings: Record<string, string> = { | ||
| ...this.settings | ||
| }; | ||
|
ejsmith marked this conversation as resolved.
Resolved
|
||
| for (const [key, setting] of Object.entries(this._appliedServerSettings)) { | ||
| if (settings[key] !== setting) { | ||
| delete this._overriddenLocalSettings[key]; | ||
| continue; | ||
| } | ||
|
|
||
| if (key in this._overriddenLocalSettings) { | ||
| settings[key] = this._overriddenLocalSettings[key]; | ||
| delete this._overriddenLocalSettings[key]; | ||
| } else { | ||
| delete settings[key]; | ||
| } | ||
| } | ||
|
|
||
| for (const [key, setting] of Object.entries(received)) { | ||
| // Server-owned keys were removed above, so any remaining value is local, even if it equals the server value. | ||
| if (key in settings) { | ||
| this._overriddenLocalSettings[key] = settings[key]; | ||
| } | ||
|
|
||
| settings[key] = setting; | ||
| } | ||
|
|
||
| this.services.log.trace(`Applying saved settings: v${serverSettings.version}`); | ||
| this.settings = Object.assign(this.originalSettings, serverSettings.settings); | ||
| this.settingsVersion = serverSettings.version; | ||
| this._appliedServerSettings = received; | ||
| this.settings = settings; | ||
| this.settingsVersion = typeof serverSettings?.version === "number" ? serverSettings.version : 0; | ||
| this.notifySubscribers(); | ||
| } | ||
|
|
||
|
|
||
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.