mirror of
https://github.com/sern-handler/handler
synced 2026-06-24 08:42:17 +00:00
finish ioc transition
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Container } from './container';
|
||||
import { Container } from '@sern/ioc';
|
||||
import * as __Services from '../structures/default-services';
|
||||
import type { Logging } from '../interfaces';
|
||||
import { __add_container, __add_wiredcontainer, __init_container, __swap_container, useContainerRaw } from './global';
|
||||
@@ -34,8 +34,7 @@ const dependencyBuilder = (container: Container) => {
|
||||
* Swap out a preexisting dependency.
|
||||
*/
|
||||
swap(key: keyof Dependencies, v: Insertable) {
|
||||
//todo in container
|
||||
this.add(key, v);
|
||||
this.swap(key, v);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
/**
|
||||
* A semi-generic container that provides error handling, emitter, and module store.
|
||||
* For the handler to operate correctly, The only user provided dependency needs to be @sern/client
|
||||
*/
|
||||
function hasCallableMethod(obj: object, name: PropertyKey) {
|
||||
//@ts-ignore
|
||||
return typeof obj[name] == 'function';
|
||||
}
|
||||
/**
|
||||
* A Depedency injection container capable of adding singletons, firing hooks, and managing IOC within an application
|
||||
*/
|
||||
export class Container {
|
||||
private __singletons = new Map<PropertyKey, any>();
|
||||
private hooks= new Map<string, Function[]>();
|
||||
private finished_init = false;
|
||||
constructor(options: { autowire: boolean; path?: string }) {
|
||||
if(options.autowire) { /* noop */ }
|
||||
}
|
||||
|
||||
addHook(name: string, callback: Function) {
|
||||
if (!this.hooks.has(name)) {
|
||||
this.hooks.set(name, []);
|
||||
}
|
||||
this.hooks.get(name)!.push(callback);
|
||||
}
|
||||
private registerHooks(hookname: string, insert: object) {
|
||||
|
||||
if(hasCallableMethod(insert, hookname)) {
|
||||
//@ts-ignore
|
||||
this.addHook(hookname, () => insert[hookname]())
|
||||
}
|
||||
}
|
||||
|
||||
addSingleton(key: string, 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)
|
||||
this.__singletons.set(key, insert);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
addWiredSingleton(key: string, fn: (c: Record<string,unknown>) => object) {
|
||||
const insert = fn(this.deps());
|
||||
return this.addSingleton(key, insert);
|
||||
}
|
||||
|
||||
async disposeAll() {
|
||||
await this.executeHooks('dispose');
|
||||
this.hooks.delete('dispose');
|
||||
}
|
||||
|
||||
isReady() { return this.finished_init; }
|
||||
hasKey(key: string) { return this.__singletons.has(key); }
|
||||
get<T>(key: PropertyKey) : T|undefined { return this.__singletons.get(key); }
|
||||
|
||||
async ready() {
|
||||
await this.executeHooks('init');
|
||||
this.hooks.delete('init');
|
||||
this.finished_init = true;
|
||||
}
|
||||
|
||||
deps<T extends Record<string,any>>(): T {
|
||||
return Object.fromEntries(this.__singletons) as T
|
||||
}
|
||||
|
||||
async executeHooks(name: string) {
|
||||
const hookFunctions = this.hooks.get(name) || [];
|
||||
for (const hookFunction of hookFunctions) {
|
||||
await hookFunction();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Container } from '@sern/ioc';
|
||||
import { UnpackedDependencies } from '../../types/utility';
|
||||
import { Container } from './container';
|
||||
|
||||
//SIDE EFFECT: GLOBAL DI
|
||||
let containerSubject: Container;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IntoDependencies } from '../../types/ioc';
|
||||
import type { IntoDependencies } from '../../types/ioc';
|
||||
import { Service as __Service, Services as __Services } from './global'
|
||||
export { makeDependencies } from './base';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user