fix(core): Updated dependencies and improved AsyncStore debugging and cleanup

This commit is contained in:
2025-01-19 20:06:18 +01:00
parent 8548ad9684
commit 98f6afec7e
8 changed files with 164 additions and 80 deletions

View File

@ -2,6 +2,8 @@ import { tap, expect } from '@push.rocks/tapbundle';
import { AsyncContext } from '../ts/logcontext.classes.asynccontext.js';
import { AsyncStore } from '../ts/logcontext.classes.asyncstore.js';
process.env.DEBUG = 'true';
/**
* This test file demonstrates how to use the AsyncContext and ensures
* that runScoped() properly creates child AsyncStore contexts and merges parent data.
@ -35,15 +37,28 @@ tap.test('should not contaminate the parent store with child-only data', async (
expect(asyncContext.store.get('temporaryKey')).toBeUndefined();
});
tap.test('should allow adding data in multiple scopes independently', async () => {
tap.test('should allow adding data in multiple scopes independently', async (toolsArg) => {
const done = toolsArg.cumulativeDefer();
// add data in first scope
await asyncContext.runScoped(async () => {
asyncContext.store.add('childKey1', 'childValue1');
expect(asyncContext.store.get('childKey1')).toEqual('childValue1');
asyncContext.runScoped(async () => {
const subDone = done.subDefer();
asyncContext.store.add('childKey1', 'childValue1-v1');
await toolsArg.delayFor(2000);
expect(asyncContext.store.get('childKey1')).toEqual('childValue1-v1');
subDone.resolve();
});
asyncContext.runScoped(async () => {
const subDone = done.subDefer();
asyncContext.store.add('childKey1', 'childValue1-v2');
await toolsArg.delayFor(1000);
expect(asyncContext.store.get('childKey1')).toEqual('childValue1-v2');
subDone.resolve();
});
// add data in second scope
await asyncContext.runScoped(async () => {
asyncContext.runScoped(async () => {
asyncContext.store.add('childKey2', 'childValue2');
expect(asyncContext.store.get('childKey2')).toEqual('childValue2');
});
@ -51,23 +66,27 @@ tap.test('should allow adding data in multiple scopes independently', async () =
// neither childKey1 nor childKey2 should exist in the parent store
expect(asyncContext.store.get('childKey1')).toBeUndefined();
expect(asyncContext.store.get('childKey2')).toBeUndefined();
await done.promise;
});
tap.test('should allow deleting data in a child store without removing it from the parent store', async () => {
// ensure parent has some data
asyncContext.store.add('deletableKey', 'iShouldStayInParent');
tap.test(
'should allow deleting data in a child store without removing it from the parent store',
async (toolsArg) => {
// ensure parent has some data
asyncContext.store.add('deletableKey', 'iShouldStayInParent');
await asyncContext.runScoped(async () => {
// child sees the parent's data
await asyncContext.runScoped(async () => {
// child sees the parent's data
expect(asyncContext.store.get('deletableKey')).toEqual('iShouldStayInParent');
// attempt to delete it in the child
asyncContext.store.delete('deletableKey');
// child no longer sees it
expect(asyncContext.store.get('deletableKey')).toBeUndefined();
// but parent still has it
});
expect(asyncContext.store.get('deletableKey')).toEqual('iShouldStayInParent');
// attempt to delete it in the child
asyncContext.store.delete('deletableKey');
// child no longer sees it
expect(asyncContext.store.get('deletableKey')).toBeUndefined();
// but parent still has it
});
expect(asyncContext.store.get('deletableKey')).toEqual('iShouldStayInParent');
});
}
);
tap.test('should allow multiple child scopes to share the same parent store data', async () => {
// add a key to the parent store
@ -85,4 +104,4 @@ tap.test('should allow multiple child scopes to share the same parent store data
});
});
export default tap.start();
export default tap.start();