mirror of
https://github.com/sern-handler/handler
synced 2026-06-28 02:32:15 +00:00
style: pretty
This commit is contained in:
@@ -3,8 +3,7 @@ import { composeRoot, useContainer } from './dependency-injection';
|
||||
import { Dependencies, DependencyConfiguration } from './types';
|
||||
import { CoreContainer } from '../structures/container';
|
||||
|
||||
|
||||
//SIDE EFFECT: GLOBAL DI
|
||||
//SIDE EFFECT: GLOBAL DI
|
||||
let containerSubject: CoreContainer<Partial<Dependencies>>;
|
||||
|
||||
/**
|
||||
@@ -14,7 +13,7 @@ let containerSubject: CoreContainer<Partial<Dependencies>>;
|
||||
export function useContainerRaw() {
|
||||
assert.ok(
|
||||
containerSubject && containerSubject.isReady(),
|
||||
"Could not find container or container wasn't ready. Did you call makeDependencies?"
|
||||
"Could not find container or container wasn't ready. Did you call makeDependencies?",
|
||||
);
|
||||
return containerSubject;
|
||||
}
|
||||
@@ -33,4 +32,3 @@ export async function makeDependencies<const T extends Dependencies>(
|
||||
|
||||
return useContainer<T>();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
import type { DependencyConfiguration, MapDeps, IntoDependencies, Dependencies, CoreDependencies } from './types';
|
||||
import type {
|
||||
DependencyConfiguration,
|
||||
MapDeps,
|
||||
IntoDependencies,
|
||||
Dependencies,
|
||||
CoreDependencies,
|
||||
} from './types';
|
||||
import { DefaultLogging } from '../structures';
|
||||
import { SernError } from '../structures/errors';
|
||||
import { useContainerRaw } from './base';
|
||||
import { CoreContainer } from '../structures/container';
|
||||
|
||||
|
||||
/**
|
||||
* @__PURE__
|
||||
* @since 2.0.0.
|
||||
@@ -18,7 +23,7 @@ export function single<T>(cb: () => T) {
|
||||
/**
|
||||
* @__PURE__
|
||||
* @since 2.0.0
|
||||
* Creates a transient object
|
||||
* Creates a transient object
|
||||
* @param cb
|
||||
*/
|
||||
export function transient<T>(cb: () => () => T) {
|
||||
@@ -42,7 +47,7 @@ export function Services<const T extends (keyof Dependencies)[]>(...keys: [...T]
|
||||
*/
|
||||
export async function composeRoot(
|
||||
container: CoreContainer<Partial<Dependencies>>,
|
||||
conf: DependencyConfiguration
|
||||
conf: DependencyConfiguration,
|
||||
) {
|
||||
//container should have no client or logger yet.
|
||||
const hasLogger = conf.exclude?.has('@sern/logger');
|
||||
@@ -71,11 +76,7 @@ export function useContainer<const T extends Dependencies>() {
|
||||
Warning: using a container hook (useContainer) is not recommended.
|
||||
Could lead to many unwanted side effects.
|
||||
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>;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ 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>;
|
||||
@@ -15,10 +14,10 @@ export interface CoreDependencies {
|
||||
export interface Dependencies extends CoreDependencies {
|
||||
'@sern/client': Singleton<import('node:events').EventEmitter>;
|
||||
}
|
||||
export type DependencyFromKey<T extends keyof Dependencies> = Dependencies[T];
|
||||
export type DependencyFromKey<T extends keyof Dependencies> = Dependencies[T];
|
||||
|
||||
export type IntoDependencies<Tuple extends [...any[]]> = {
|
||||
[Index in keyof Tuple]: UnpackFunction<DependencyFromKey<Tuple[Index]>&{}>; //Unpack and make NonNullable
|
||||
[Index in keyof Tuple]: UnpackFunction<DependencyFromKey<Tuple[Index]> & {}>; //Unpack and make NonNullable
|
||||
} & { length: Tuple['length'] };
|
||||
|
||||
export interface DependencyConfiguration {
|
||||
@@ -38,4 +37,3 @@ export type MapDeps<Deps extends Dependencies, T extends readonly unknown[]> = T
|
||||
...(MapDeps<Deps, Rest> extends [never] ? [] : MapDeps<Deps, Rest>),
|
||||
]
|
||||
: [never];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user