From 3a5ddb47918b80b73da86f5ce59e2a2db0456f3e Mon Sep 17 00:00:00 2001 From: VictorAugDB Date: Tue, 17 May 2022 16:39:56 -0300 Subject: [PATCH] reduce duplcation and remove unnecessary code Create an itnerface to repeatable type and use destructuring with deafult value to remove unnecessary code --- src/_lib/Context.ts | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/_lib/Context.ts b/src/_lib/Context.ts index f532e65..c0aa080 100644 --- a/src/_lib/Context.ts +++ b/src/_lib/Context.ts @@ -1,39 +1,35 @@ import { Application, HookFn, makeApp } from '@/_lib/Application'; -type EntrypointFn> = (arg: Context) => Promise; +type RecordAny = Record -type BootFn> = (arg: Context) => Promise; +type EntrypointFn = (arg: Context) => Promise; -type Module, F extends BootFn = BootFn> = { +type BootFn = (arg: Context) => Promise; + +type Module = BootFn> = { name: string; fn: F; }; -type Context> = { +type Context = { app: Omit; bootstrap: []>(...modules: M) => Promise; } & T; -type ContextProvider> = { +type ContextProvider = { makeModule: , M extends Module>(name: string, fn: F) => M; withContext: >(fn: F) => () => Promise; }; type ContextOptions = { - shutdownTimeout: number; - logger: Pick; -}; - -const defaultOptions: ContextOptions = { - shutdownTimeout: 5000, - logger: console, + shutdownTimeout?: number; + logger?: Pick; }; -const makeContext = >( +const makeContext = ( localContext: T, - opts: Partial = {} + { logger = console, shutdownTimeout = 5000 }: ContextOptions ): ContextProvider => { - const { shutdownTimeout, logger } = { ...defaultOptions, ...opts }; const moduleKey = Symbol(); const app = makeApp({ shutdownTimeout, logger });