fix(core): Improve scope handling in async contexts.
This commit is contained in:
@ -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.'
|
||||
}
|
||||
|
@ -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()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user