fix(core): Improve scope handling in async contexts.

This commit is contained in:
2025-01-19 00:27:06 +01:00
parent 58b189c1c7
commit 9c0b53f373
6 changed files with 227 additions and 286 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartcontext',
version: '2.1.1',
version: '2.1.2',
description: 'A module providing advanced asynchronous context management to enrich logs with context and manage scope effectively in Node.js applications.'
}

View File

@ -2,12 +2,18 @@ import * as plugins from './logcontext.plugins.js';
import { AsyncStore } from './logcontext.classes.asyncstore.js';
export class AsyncContext {
private context = new plugins.simpleAsyncContext.AsyncContext.Variable<AsyncStore>();
public store = new AsyncStore();
public runScoped(functionArg: (storeArg: AsyncStore) => Promise<void>) {
this.context.run(this.store, async () => {
const childStore = new AsyncStore(this.store);
functionArg(childStore)
private _context = new plugins.simpleAsyncContext.AsyncContext.Variable<AsyncStore>();
private _store = new AsyncStore();
get store() {
return this._context.get() || this._store;
}
set store(value: AsyncStore) {
this._store = value;
}
public async runScoped(functionArg: () => Promise<void>) {
const childStore = new AsyncStore(this.store);
await this._context.run(childStore, async () => {
await functionArg()
});
}
}