ioc proto

This commit is contained in:
jacob
2024-02-23 20:28:33 -06:00
parent 5e339f594a
commit 6231d9078c
7 changed files with 11 additions and 21 deletions

View File

@@ -1,8 +0,0 @@
{
"hash": "577958af",
"configHash": "94f40ccb",
"lockfileHash": "9b10afd2",
"browserHash": "79b250d2",
"optimized": {},
"chunks": {}
}

View File

@@ -1,3 +0,0 @@
{
"type": "module"
}

View File

@@ -1 +0,0 @@
{"version":"1.2.2","results":[[":test/index.test.ts",{"duration":15,"failed":true}]]}

View File

@@ -21,8 +21,9 @@ export class Container {
}
private registerHooks(hookname: string, insert: object) {
if(hasCallableMethod(insert, hookname)) {
console.log(insert)
//@ts-ignore
this.addHook('init', async () => await insert[hookname]())
this.addHook(hookname, () => insert[hookname]())
}
}
addSingleton(key: string, insert: object) {
@@ -65,6 +66,7 @@ export class Container {
async executeHooks(name: string) {
const hookFunctions = this.hooks.get(name) || [];
console.log(hookFunctions)
for (const hookFunction of hookFunctions) {
await hookFunction();
}

View File

@@ -1,5 +1,5 @@
export function hasCallableMethod(obj: object, name: PropertyKey) {
//@ts-ignore
return Object.hasOwn(obj, name) && typeof obj.init == 'function';
return Object.hasOwn(obj, name) && typeof obj[name] == 'function';
}

View File

@@ -1,12 +1,12 @@
import { CoreContainer } from '../src/container';
import { Container } from '../src/container';
import { describe, it, expect, beforeEach, vi } from 'vitest';
describe('CoreContainer Tests', () => {
let coreContainer: CoreContainer;
let coreContainer: Container;
beforeEach(() => {
coreContainer = new CoreContainer({ autowire: false });
coreContainer = new Container({ autowire: false });
});
it('Adding and getting singletons', () => {
@@ -76,7 +76,7 @@ describe('CoreContainer Tests', () => {
it('wired singleton', async () => {
let fn = vi.fn()
const wiredSingletonFn = (container: CoreContainer) => {
const wiredSingletonFn = (container: Container) => {
return { value: 'wiredSingletonValue', init: fn };
};
const added = coreContainer.addWiredSingleton('wiredSingletonKey', wiredSingletonFn);
@@ -90,10 +90,9 @@ describe('CoreContainer Tests', () => {
})
it('dispose', async () => {
let dfn = vi.fn()
const wiredSingletonFn = { value: 'wiredSingletonValue', dispose: vi.fn() };
let dfn = vi.fn();
const wiredSingletonFn = { value: 'wiredSingletonValue', dispose: dfn };
coreContainer.addSingleton('sk', wiredSingletonFn);
//@ts-ignore
await coreContainer.disposeAll();