This commit is contained in:
Jacob Nguyen
2024-04-03 00:37:56 -05:00
parent 5af94b6e9e
commit 74024355e2
4 changed files with 46 additions and 8 deletions

View File

@@ -22,8 +22,9 @@ export class Container {
}
private registerHooks(hookname: string, insert: object) {
if(hasCallableMethod(insert, hookname)) {
console.log(hookname)
//@ts-ignore
this.addHook(hookname, () => insert[hookname]())
this.addHook(hookname, async () => await insert[hookname]())
}
}
addSingleton(key: string, insert: object) {

View File

@@ -1,8 +1,25 @@
<<<<<<< HEAD
=======
import assert from 'assert';
>>>>>>> 82054aa (sss)
import { Container } from './container';
//SIDE EFFECT: GLOBAL DI
let containerSubject: Container;
<<<<<<< HEAD
=======
/**
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
* then it will swap
*/
export async function __swap_container(c: Container) {
if(containerSubject) {
await containerSubject.disposeAll()
}
containerSubject = c;
}
>>>>>>> 82054aa (sss)
/**
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
@@ -20,11 +37,7 @@ export function __init_container(options: {
autowire: boolean;
path?: string | undefined;
}) {
if(containerSubject) {
return false;
}
containerSubject = new Container(options);
return true;
}
/**

View File

@@ -84,13 +84,37 @@ describe('CoreContainer Tests', () => {
})
it('dispose', async () => {
let dfn = vi.fn();
let dfn = vi.fn()
const wiredSingletonFn = { value: 'wiredSingletonValue', dispose: dfn };
coreContainer.addSingleton('sk', wiredSingletonFn);
//@ts-ignore
await coreContainer.disposeAll();
expect(dfn).toHaveBeenCalledOnce()
})
it('Checking if container is ready', async () => {
expect(coreContainer.isReady()).toBe(false);
await coreContainer.ready();
expect(coreContainer.isReady()).toBe(true);
});
it('Registering and executing hooks - init should be called once after ready', async () => {
let initCount = 0;
const singletonWithInit = {
value: 'singletonValueWithInit',
init: async () => {
initCount++;
}
};
coreContainer.addSingleton('singletonKeyWithInit', singletonWithInit);
// Call ready twice to ensure hooks are executed only once
await coreContainer.ready();
await coreContainer.ready();
expect(initCount).toBe(1);
});
})

View File

@@ -2837,7 +2837,7 @@ __metadata:
"typescript@patch:typescript@^5.0.0#~builtin<compat/typescript>, typescript@patch:typescript@^5.0.4#~builtin<compat/typescript>":
version: 5.3.3
resolution: "typescript@patch:typescript@npm%3A5.3.3#~builtin<compat/typescript>::version=5.3.3&hash=f3b441"
resolution: "typescript@patch:typescript@npm%3A5.3.3#~builtin<compat/typescript>::version=5.3.3&hash=14eedb"
bin:
tsc: bin/tsc
tsserver: bin/tsserver