Skip to content

Commit c87c042

Browse files
committed
reduce duplication and remove unnecessary code
Create an interface to repeatable type and use destructuring with deafult value to remove unnecessary code
1 parent 333e25a commit c87c042

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/_lib/Context.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
import { Application, HookFn, makeApp } from '@/_lib/Application';
22

3-
type EntrypointFn<T extends Record<string | symbol, any>> = (arg: Context<T>) => Promise<void>;
3+
type RecordAny<T extends string | symbol = string | symbol> = Record<T, any>
44

5-
type BootFn<T extends Record<string | symbol, any>> = (arg: Context<T>) => Promise<void | HookFn>;
5+
type EntrypointFn<T extends RecordAny> = (arg: Context<T>) => Promise<void>;
66

7-
type Module<T extends Record<string | symbol, any>, F extends BootFn<T> = BootFn<any>> = {
7+
type BootFn<T extends RecordAny> = (arg: Context<T>) => Promise<void | HookFn>;
8+
9+
type Module<T extends RecordAny, F extends BootFn<T> = BootFn<any>> = {
810
name: string;
911
fn: F;
1012
};
1113

12-
type Context<T extends Record<string | symbol, any>> = {
14+
type Context<T extends RecordAny> = {
1315
app: Omit<Application, 'start' | 'onBooting'>;
1416
bootstrap: <M extends Module<T>[]>(...modules: M) => Promise<void>;
1517
} & T;
1618

17-
type ContextProvider<T extends Record<string | symbol, any>> = {
19+
type ContextProvider<T extends RecordAny> = {
1820
makeModule: <F extends BootFn<T>, M extends Module<F>>(name: string, fn: F) => M;
1921
withContext: <F extends EntrypointFn<T>>(fn: F) => () => Promise<void>;
2022
};
2123

2224
type ContextOptions = {
23-
shutdownTimeout: number;
24-
logger: Pick<Console, 'info' | 'error' | 'warn'>;
25-
};
26-
27-
const defaultOptions: ContextOptions = {
28-
shutdownTimeout: 5000,
29-
logger: console,
25+
shutdownTimeout?: number;
26+
logger?: Pick<Console, 'info' | 'error' | 'warn'>;
3027
};
3128

32-
const makeContext = <T extends Record<string | symbol, any>>(
29+
const makeContext = <T extends RecordAny>(
3330
localContext: T,
34-
opts: Partial<ContextOptions> = {}
31+
{ logger = console, shutdownTimeout = 5000 }: ContextOptions
3532
): ContextProvider<T> => {
36-
const { shutdownTimeout, logger } = { ...defaultOptions, ...opts };
3733
const moduleKey = Symbol();
3834

3935
const app = makeApp({ shutdownTimeout, logger });

0 commit comments

Comments
 (0)