polishing up

This commit is contained in:
jacob
2024-02-24 12:55:41 -06:00
parent cd2a670ce9
commit c907a3baa8
5 changed files with 13 additions and 34 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@sern/ioc",
"version": "1.1.0",
"version": "1.0.0",
"description": "Dependency Injection system",
"main": "dist/index.js",
"scripts": {

View File

@@ -1,7 +1,8 @@
import assert from "assert";
import { hasCallableMethod } from "./hooks";
import { } from 'node:fs/promises'
function hasCallableMethod(obj: object, name: PropertyKey) {
//@ts-ignore
return Object.hasOwn(obj, name) && typeof obj[name] == 'function';
}
/**
* A Depedency injection container capable of adding singletons, firing hooks, and managing IOC within an application
*/
@@ -21,13 +22,14 @@ export class Container {
}
private registerHooks(hookname: string, insert: object) {
if(hasCallableMethod(insert, hookname)) {
console.log(insert)
//@ts-ignore
this.addHook(hookname, () => insert[hookname]())
}
}
addSingleton(key: string, insert: object) {
assert(typeof insert === 'object')
if(typeof insert !== 'object') {
throw Error("Inserted object must be an object");
}
if(!this.__singletons.has(key)){
this.registerHooks('init', insert)
this.registerHooks('dispose', insert)
@@ -39,14 +41,7 @@ export class Container {
addWiredSingleton(key: string, fn: (c: Container) => object) {
const insert = fn(this);
assert(typeof insert === 'object')
if(!this.__singletons.has(key)){
this.registerHooks('init', insert)
this.registerHooks('dispose', insert)
this.__singletons.set(key, insert);
return true;
}
return false;
return this.addSingleton(key, insert);
}
async disposeAll() {
@@ -66,7 +61,6 @@ 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,19 +1,9 @@
import assert from 'assert';
import { CoreContainer } from './container';
import { Container } from './container';
//SIDE EFFECT: GLOBAL DI
let containerSubject: CoreContainer;
let containerSubject: Container;
/**
* 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: CoreContainer) {
if(containerSubject) {
await containerSubject.disposeAll()
}
containerSubject = c;
}
/**
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
@@ -31,7 +21,7 @@ export function __init_container(options: {
autowire: boolean;
path?: string | undefined;
}) {
containerSubject = new CoreContainer(options);
containerSubject = new Container(options);
}
/**

View File

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

View File

@@ -1,2 +1,2 @@
export { Service, Services, __init_container, __swap_container, __add_container } from './global';
export { Service, Services, __init_container, __add_container } from './global';
export { Container } from './container'