add more tests, polish up ioc

This commit is contained in:
Jacob Nguyen
2024-05-15 01:44:23 -05:00
parent ec45f80be6
commit 16a84e85d1
13 changed files with 62 additions and 29 deletions

View File

@@ -17,6 +17,7 @@ export interface Disposable {
dispose(): unknown;
}
export interface Emitter {
addListener(eventName: string | symbol, listener: AnyFunction): this;
removeListener(eventName: string | symbol, listener: AnyFunction): this;

View File

@@ -13,7 +13,7 @@ export function disposeAll(logger: Logging|undefined) {
type Insertable =
| ((container: UnpackedDependencies) => unknown)
| ((container: UnpackedDependencies) => object)
| object
const dependencyBuilder = (container: Container, excluded: string[] ) => {
return {
@@ -25,9 +25,7 @@ const dependencyBuilder = (container: Container, excluded: string[] ) => {
if(typeof v !== 'function') {
container.addSingleton(key, v)
} else {
//TODO fixme
//@ts-ignore
container.addWiredSingleton(key, (cntr: UnpackedDependencies) => v(cntr))
container.addWiredSingleton(key, (cntr) => v(cntr as UnpackedDependencies))
}
},
/**

View File

@@ -1,4 +1,4 @@
import * as __Services from '../structures/default-services';
import type { UnpackedDependencies } from '../../types/utility';
/**
* A semi-generic container that provides error handling, emitter, and module store.
@@ -46,8 +46,8 @@ export class Container {
return false;
}
addWiredSingleton(key: string, fn: (c: Container) => object) {
const insert = fn(this);
addWiredSingleton(key: string, fn: (c: Record<string,unknown>) => object) {
const insert = fn(this.deps());
return this.addSingleton(key, insert);
}

View File

@@ -40,7 +40,7 @@ export async function importModule<T>(absPath: string) {
let commandModule: Module = fileModule.default;
assert(commandModule , `No export @ ${absPath}. Forgot to ignore with "!"? (!${path.basename(absPath)})?`);
assert(commandModule , `No default export @ ${absPath}`);
if ('default' in commandModule) {
commandModule = commandModule.default as Module;
}
@@ -56,10 +56,15 @@ export async function importModule<T>(absPath: string) {
export async function* readRecursive(dir: string): AsyncGenerator<string> {
const files = await readdir(dir, { recursive: true, withFileTypes: true });
const files = await readdir(dir, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(file.parentPath, file.name);
if(!file.name.startsWith('!') && !file.isDirectory()) {
const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
if (!file.name.startsWith('!')) {
yield* readRecursive(fullPath);
}
} else if (!file.name.startsWith('!')) {
yield fullPath;
}
}