|
1 | 1 | import { Application, HookFn, makeApp } from '@/_lib/Application';
|
2 | 2 |
|
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> |
4 | 4 |
|
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>; |
6 | 6 |
|
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>> = { |
8 | 10 | name: string;
|
9 | 11 | fn: F;
|
10 | 12 | };
|
11 | 13 |
|
12 |
| -type Context<T extends Record<string | symbol, any>> = { |
| 14 | +type Context<T extends RecordAny> = { |
13 | 15 | app: Omit<Application, 'start' | 'onBooting'>;
|
14 | 16 | bootstrap: <M extends Module<T>[]>(...modules: M) => Promise<void>;
|
15 | 17 | } & T;
|
16 | 18 |
|
17 |
| -type ContextProvider<T extends Record<string | symbol, any>> = { |
| 19 | +type ContextProvider<T extends RecordAny> = { |
18 | 20 | makeModule: <F extends BootFn<T>, M extends Module<F>>(name: string, fn: F) => M;
|
19 | 21 | withContext: <F extends EntrypointFn<T>>(fn: F) => () => Promise<void>;
|
20 | 22 | };
|
21 | 23 |
|
22 | 24 | 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'>; |
30 | 27 | };
|
31 | 28 |
|
32 |
| -const makeContext = <T extends Record<string | symbol, any>>( |
| 29 | +const makeContext = <T extends RecordAny>( |
33 | 30 | localContext: T,
|
34 |
| - opts: Partial<ContextOptions> = {} |
| 31 | + { logger = console, shutdownTimeout = 5000 }: ContextOptions |
35 | 32 | ): ContextProvider<T> => {
|
36 |
| - const { shutdownTimeout, logger } = { ...defaultOptions, ...opts }; |
37 | 33 | const moduleKey = Symbol();
|
38 | 34 |
|
39 | 35 | const app = makeApp({ shutdownTimeout, logger });
|
|
0 commit comments