Skip to content

reduce duplication and remove unnecessary code #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for curiosity but this is a kind of thing we should place as a shared type? cc/ @talyssonoc


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