feat: globalize dependencies type

This commit is contained in:
Jacob Nguyen
2023-05-18 18:54:03 -05:00
parent e4a9c8d551
commit b8619df838
11 changed files with 32 additions and 40 deletions

View File

@@ -1,6 +1,6 @@
import * as assert from 'assert';
import { composeRoot, useContainer } from './dependency-injection';
import { Dependencies, DependencyConfiguration } from './types';
import { DependencyConfiguration } from './types';
import { CoreContainer } from '../structures/container';
//SIDE EFFECT: GLOBAL DI

10
src/core/ioc/dependencies.d.ts vendored Normal file
View File

@@ -0,0 +1,10 @@
import { CoreDependencies } from './types'
declare global {
interface Dependencies extends CoreDependencies {}
}

View File

@@ -1,9 +1,7 @@
import type {
DependencyConfiguration,
MapDeps,
IntoDependencies,
Dependencies,
CoreDependencies,
DependencyConfiguration,
IntoDependencies,
} from './types';
import { DefaultLogging } from '../structures';
import { SernError } from '../structures/errors';
@@ -57,7 +55,7 @@ export async function composeRoot(
});
}
//Build the container based on the callback provided by the user
conf.build(container as CoreContainer<CoreDependencies>);
conf.build(container as CoreContainer<Omit<CoreDependencies, '@sern/client'>>);
try {
container.get('@sern/client');
} catch {
@@ -78,5 +76,5 @@ export function useContainer<const T extends Dependencies>() {
Use the new Service(s) api function instead.
`);
return <V extends (keyof T)[]>(...keys: [...V]) =>
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as MapDeps<T, V>;
keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies<V>;
}

View File

@@ -1,3 +1,3 @@
export { useContainerRaw, makeDependencies } from './base';
export { Service, Services, single, transient } from './dependency-injection';
export type { Singleton, Transient } from './types';
export type { Singleton, Transient, CoreDependencies } from './types';

View File

@@ -1,19 +1,17 @@
import { EventEmitter } from 'node:events';
import { Container, UnpackFunction } from 'iti';
import * as Contract from '../contracts';
export type Singleton<T> = () => T;
export type Transient<T> = () => () => T;
export interface CoreDependencies {
'@sern/logger'?: Singleton<Contract.Logging>;
'@sern/emitter': Singleton<import('../structures/sern-emitter').SernEmitter>;
'@sern/store': Singleton<Contract.CoreModuleStore>;
'@sern/modules': Singleton<Contract.ModuleManager>;
'@sern/errors': Singleton<Contract.ErrorHandling>;
'@sern/client': () => EventEmitter
'@sern/logger'?: () => import('../contracts').Logging;
'@sern/emitter': () => import('../structures/sern-emitter').SernEmitter;
'@sern/store': () => import('../contracts').CoreModuleStore;
'@sern/modules': () => import('../contracts').ModuleManager;
'@sern/errors': () => import('../contracts').ErrorHandling;
}
export interface Dependencies extends CoreDependencies {
'@sern/client': Singleton<import('node:events').EventEmitter>;
}
export type DependencyFromKey<T extends keyof Dependencies> = Dependencies[T];
export type IntoDependencies<Tuple extends [...any[]]> = {
@@ -23,17 +21,6 @@ export type IntoDependencies<Tuple extends [...any[]]> = {
export interface DependencyConfiguration {
//@deprecated. Loggers will always be included in the future
exclude?: Set<'@sern/logger'>;
build: (root: Container<CoreDependencies, {}>) => Container<Dependencies, {}>;
build: (root: Container<Omit<CoreDependencies, '@sern/client'>, {}>) => Container<Dependencies, {}>;
}
//To be removed in future
//prettier-ignore
export type MapDeps<Deps extends Dependencies, T extends readonly unknown[]> = T extends [
infer First extends keyof Deps,
...infer Rest extends readonly unknown[],
]
? [
UnpackFunction<Deps[First]>,
...(MapDeps<Deps, Rest> extends [never] ? [] : MapDeps<Deps, Rest>),
]
: [never];

View File

@@ -4,7 +4,6 @@ import { isAsyncFunction } from 'node:util/types';
import * as assert from 'node:assert';
import { Subject } from 'rxjs';
import { ModuleStore } from './module-store';
import { Dependencies } from '../ioc/types';
/**
* Provides all the defaults for sern to function properly.
@@ -41,7 +40,8 @@ export class CoreContainer<T extends Partial<Dependencies>> extends Container<T,
}
private async callInitHooks(e: { key: keyof T; newContainer: T[keyof T] | null }) {
const dep = e.newContainer;
const dep = e.newContainer ;
assert.ok(dep);
//Ignore any dependencies that are not objects or array
if (typeof dep !== 'object' || Array.isArray(dep)) {

View File

@@ -61,7 +61,7 @@ export function discordEvent<T extends keyof ClientEvents>(mod: {
});
}
function prepareClassPlugins(c: Module) {
function preparePlugins(c: Module) {
const [onEvent, initPlugins] = partitionPlugins(c.plugins);
c.plugins = initPlugins as InitPlugin[];
c.onEvent = onEvent as ControlPlugin[];
@@ -83,7 +83,7 @@ export abstract class CommandExecutable<const Type extends CommandType = Command
if (!CommandExecutable._instance) {
//@ts-ignore
CommandExecutable._instance = new this();
prepareClassPlugins(CommandExecutable._instance);
preparePlugins(CommandExecutable._instance);
}
return CommandExecutable._instance;
}

View File

@@ -35,11 +35,9 @@ export function init(wrapper: Wrapper) {
}
startReadyEvent(dependencies, getFullPathTree(wrapper.commands, mode)).add(() => {
const endTime = performance.now();
const time = ((performance.now() - startTime) / 1000).toFixed(2);
logger?.info({
message: `sern: registered all modules in ${((endTime - startTime) / 1000).toFixed(
2,
)} s`,
message: `sern: registered all modules in ${time} s`,
});
});

View File

@@ -1,5 +1,5 @@
import { ErrorHandling, Logging, ModuleManager, SernEmitter } from '../core';
import EventEmitter from 'node:events';
import { EventEmitter } from 'node:events';
import { Module } from '../core/types/modules';
export type Processed<T> = T & { name: string; description: string };

View File

@@ -5,7 +5,6 @@ import type {
} from 'discord.js';
import { PayloadType } from './core';
import { AnyModule } from './core/types/modules';
import { Dependencies } from './core/ioc/types';
export type ReplyOptions =
| string

View File

@@ -15,5 +15,5 @@
"isolatedModules": true
},
"exclude": ["node_modules", "dist"],
"include": ["src", "**/*.d.ts"]
"include": ["./src", "./src/**/*.d.ts"]
}