mirror of
https://github.com/sern-handler/tools
synced 2026-06-06 01:16:59 +00:00
moving around
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
import assert from "node:assert";
|
||||
import assert from "assert";
|
||||
import { hasCallableMethod } from "./hooks";
|
||||
import { } from 'node:fs/promises'
|
||||
|
||||
/**
|
||||
* 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
|
||||
* A Depedency injection container capable of adding singletons, firing hooks, and managing IOC within an application
|
||||
*/
|
||||
export class CoreContainer {
|
||||
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) {
|
||||
|
||||
}
|
||||
if(options.autowire) { /* noop */ }
|
||||
}
|
||||
|
||||
addHook(name: string, callback: Function) {
|
||||
@@ -23,10 +20,10 @@ export class CoreContainer {
|
||||
this.hooks.get(name)!.push(callback);
|
||||
}
|
||||
private registerHooks(hookname: string, insert: object) {
|
||||
if(hasCallableMethod(insert, hookname)) {
|
||||
//@ts-ignore
|
||||
this.addHook('init', async () => await insert[hookname]())
|
||||
}
|
||||
if(hasCallableMethod(insert, hookname)) {
|
||||
//@ts-ignore
|
||||
this.addHook('init', async () => await insert[hookname]())
|
||||
}
|
||||
}
|
||||
addSingleton(key: string, insert: object) {
|
||||
assert(typeof insert === 'object')
|
||||
@@ -39,7 +36,7 @@ export class CoreContainer {
|
||||
return false;
|
||||
}
|
||||
|
||||
addWiredSingleton(key: string, fn: (c: CoreContainer) => object) {
|
||||
addWiredSingleton(key: string, fn: (c: Container) => object) {
|
||||
const insert = fn(this);
|
||||
assert(typeof insert === 'object')
|
||||
if(!this.__singletons.has(key)){
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
import assert from 'node:assert';
|
||||
import { useContainerRaw } from './base';
|
||||
|
||||
|
||||
/**
|
||||
* The Service api, retrieve from the globally init'ed container
|
||||
* Note: this method only works AFTER your container has been initiated
|
||||
* @since 3.0.0
|
||||
* @example
|
||||
* ```ts
|
||||
* const client = Service('@sern/client');
|
||||
* ```
|
||||
* @param key a key that corresponds to a dependency registered.
|
||||
*
|
||||
*/
|
||||
export function Service<const T>(key: PropertyKey) {
|
||||
const dep = useContainerRaw().get<T>(key)!;
|
||||
assert(dep, "Requested key " + String(key) + " returned undefined");
|
||||
return dep;
|
||||
}
|
||||
/**
|
||||
* @since 3.0.0
|
||||
* The plural version of {@link Service}
|
||||
* @returns array of dependencies, in the same order of keys provided
|
||||
*/
|
||||
export function Services<const T extends string[], V>(...keys: [...T]) {
|
||||
const container = useContainerRaw();
|
||||
return keys.map(k => container.get(k)!) as V;
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
import * as assert from 'assert';
|
||||
import assert from 'assert';
|
||||
import { CoreContainer } from './container';
|
||||
|
||||
//SIDE EFFECT: GLOBAL DI
|
||||
let containerSubject: CoreContainer;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
|
||||
* then it will swap
|
||||
*/
|
||||
@@ -17,7 +16,6 @@ export async function __swap_container(c: CoreContainer) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* Don't use this unless you know what you're doing. Destroys old containerSubject if it exists and disposes everything
|
||||
* then it will swap
|
||||
*/
|
||||
@@ -25,12 +23,17 @@ export function __add_container(key: string, v: object) {
|
||||
containerSubject.addSingleton(key, v);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates the global api.
|
||||
* Once this is finished, the Service api and the other global api is available
|
||||
*/
|
||||
export function __init_container(options: {
|
||||
autowire: boolean;
|
||||
path?: string | undefined;
|
||||
}) {
|
||||
containerSubject = new CoreContainer(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the underlying data structure holding all dependencies.
|
||||
* Exposes methods from iti
|
||||
@@ -44,4 +47,28 @@ export function useContainerRaw() {
|
||||
return containerSubject;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The Service api, retrieve from the globally init'ed container
|
||||
* Note: this method only works AFTER your container has been initiated
|
||||
* @since 3.0.0
|
||||
* @example
|
||||
* ```ts
|
||||
* const client = Service('@sern/client');
|
||||
* ```
|
||||
* @param key a key that corresponds to a dependency registered.
|
||||
*
|
||||
*/
|
||||
export function Service<const T>(key: PropertyKey) {
|
||||
const dep = useContainerRaw().get<T>(key)!;
|
||||
assert(dep, "Requested key " + String(key) + " returned undefined");
|
||||
return dep;
|
||||
}
|
||||
/**
|
||||
* @since 3.0.0
|
||||
* The plural version of {@link Service}
|
||||
* @returns array of dependencies, in the same order of keys provided
|
||||
*/
|
||||
export function Services<const T extends string[], V>(...keys: [...T]) {
|
||||
const container = useContainerRaw();
|
||||
return keys.map(k => container.get(k)!) as V;
|
||||
}
|
||||
@@ -1,2 +1,2 @@
|
||||
export { Service, Services } from './dependency-injection';
|
||||
export { CoreContainer } from './container'
|
||||
export { Service, Services, __init_container, __swap_container, __add_container } from './global';
|
||||
export { Container } from './container'
|
||||
|
||||
Reference in New Issue
Block a user