From 49ce312ab20e2dc224bfec2067074129f1ab01c2 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 9 Feb 2026 18:16:15 +0200 Subject: [PATCH] chore(standalone): use a simpler CLS mechanism considering lack of multi-threading --- .../src/lightweight/cls_provider.ts | 46 +++++++------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/apps/client-standalone/src/lightweight/cls_provider.ts b/apps/client-standalone/src/lightweight/cls_provider.ts index 75cb0a3adb..0ef9a35c95 100644 --- a/apps/client-standalone/src/lightweight/cls_provider.ts +++ b/apps/client-standalone/src/lightweight/cls_provider.ts @@ -1,46 +1,32 @@ import { ExecutionContext } from "@triliumnext/core"; +/** + * Browser execution context implementation. + * + * Unlike the server (which uses cls-hooked for per-request isolation), + * the browser is single-threaded with a single user and doesn't need + * request-level isolation. We maintain a single persistent context + * throughout the page lifetime. + */ export default class BrowserExecutionContext implements ExecutionContext { - private store: Map | null = null; + private store: Map = new Map(); - get(key: string): T | undefined { - return this.store?.get(key); + get(key: string): T { + return this.store.get(key); } set(key: string, value: any): void { - if (!this.store) { - throw new Error("ExecutionContext not initialized"); - } this.store.set(key, value); } reset(): void { - this.store = null; + this.store.clear(); } init(callback: () => T): T { - // Create a fresh context for this request - const prev = this.store; - this.store = new Map(); - - try { - const result = callback(); - - // If the result is a Promise, we need to handle cleanup after it resolves - if (result && typeof result === 'object' && 'then' in result && 'catch' in result) { - const promise = result as unknown as Promise; - return promise.finally(() => { - this.store = prev; - }) as T; - } else { - // Synchronous result, clean up immediately - this.store = prev; - return result; - } - } catch (error) { - // Always clean up on error (for synchronous errors) - this.store = prev; - throw error; - } + // In browser, we don't need per-request isolation. + // Just execute the callback with the persistent context. + // This allows fire-and-forget operations to access context. + return callback(); } }