Skip to content

Commit

Permalink
reduce duplcation and remove unnecessary code
Browse files Browse the repository at this point in the history
Create an itnerface to repeatable type and use destructuring with deafult value to remove unnecessary code
  • Loading branch information
VictorAugDB committed May 17, 2022
1 parent 333e25a commit 3a5ddb4
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/_lib/Context.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
import { Application, HookFn, makeApp } from '@/_lib/Application';

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

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

type Module<T extends Record<string | symbol, any>, F extends BootFn<T> = BootFn<any>> = {
type BootFn<T extends RecordAny> = (arg: Context<T>) => Promise<void | HookFn>;

type Module<T extends RecordAny, F extends BootFn<T> = BootFn<any>> = {
name: string;
fn: F;
};

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

type ContextProvider<T extends Record<string | symbol, any>> = {
type ContextProvider<T extends RecordAny> = {
makeModule: <F extends BootFn<T>, M extends Module<F>>(name: string, fn: F) => M;
withContext: <F extends EntrypointFn<T>>(fn: F) => () => Promise<void>;
};

type ContextOptions = {
shutdownTimeout: number;
logger: Pick<Console, 'info' | 'error' | 'warn'>;
};

const defaultOptions: ContextOptions = {
shutdownTimeout: 5000,
logger: console,
shutdownTimeout?: number;
logger?: Pick<Console, 'info' | 'error' | 'warn'>;
};

const makeContext = <T extends Record<string | symbol, any>>(
const makeContext = <T extends RecordAny>(
localContext: T,
opts: Partial<ContextOptions> = {}
{ logger = console, shutdownTimeout = 5000 }: ContextOptions
): ContextProvider<T> => {
const { shutdownTimeout, logger } = { ...defaultOptions, ...opts };
const moduleKey = Symbol();

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

0 comments on commit 3a5ddb4

Please sign in to comment.