fix(client/lightweight): CLS not available in routes

This commit is contained in:
Elian Doran 2026-01-07 12:37:29 +02:00
parent 3371a31c70
commit a84e804fc3
No known key found for this signature in database
2 changed files with 19 additions and 4 deletions

View File

@ -3,6 +3,8 @@
* Supports path parameters (e.g., /api/notes/:noteId) and query strings.
*/
import { getContext } from "@triliumnext/core";
export interface BrowserRequest {
method: string;
url: string;
@ -166,7 +168,7 @@ export class BrowserRouter {
};
try {
const result = await route.handler(request);
const result = await getContext().init(async () => await route.handler(request));
return this.formatResult(result);
} catch (error) {
return this.formatError(error);

View File

@ -24,10 +24,23 @@ export default class BrowserExecutionContext implements ExecutionContext {
this.store = new Map();
try {
return callback();
} finally {
// Always clean up
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<any>;
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;
}
}
}