UNPKG

ts-fp-di

Version:

Tiny TypeScript functional dependency injection, based on AsyncLocalStorage. Supports Node.js, Deno

94 lines (93 loc) 8.65 kB
import { AsyncLocalStorage } from 'node:async_hooks'; type AlsContext = { deps: Map<unknown, unknown>; once: Map<unknown, unknown>; state: Map<unknown, unknown>; derived: Map<unknown, unknown>; [k: string]: unknown; }; export declare const als: AsyncLocalStorage<AlsContext>; export declare const clearGlobalState: () => void; export declare const di: <T extends Function>(fn: T) => T; export declare const dis: <P, S>(fn: (state: S, payload: P) => S, defaultState: S, isGlobal?: boolean) => (this: unknown, payload?: P) => S; export declare const div: <T>() => (x?: T) => T; export declare const diDep: <T>(dep: T | string) => T; export declare const diSet: <T>(dep: T, value: T extends string ? unknown : T) => void; export declare const diHas: <T>(dep: T | string) => boolean; export declare const diInit: <T>(cb: () => T, ctx?: AlsContext) => T; export declare const diOnce: <T extends (...args: any) => any>(fn: T) => T; export declare const diOnceSet: <T>(fn: (...args: any[]) => T, value: T) => void; export declare const diExists: () => boolean; export declare const diContext: () => AlsContext; export declare const diScope: <T extends { [key: string]: any; }>(scope: T, init?: () => void) => T; export declare const dic: <T>() => (x?: T | undefined) => T; export declare function diMap<T1, R>(pred: (x1: T1) => R, fn1: (...args: any[]) => T1): (() => R) & { raw: (x1: T1) => R; }; export declare function diMap<T1, T2, R>(pred: (x1: T1, x2: T2) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2): (() => R) & { raw: (x1: T1, x2: T2) => R; }; export declare function diMap<T1, T2, T3, R>(pred: (x1: T1, x2: T2, x3: T3) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2, fn3: (...args: any[]) => T3): (() => R) & { raw: (x1: T1, x2: T2, x3: T3) => R; }; export declare function diMap<T1, T2, T3, T4, R>(pred: (x1: T1, x2: T2, x3: T3, x4: T4) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2, fn3: (...args: any[]) => T3, fn4: (...args: any[]) => T4): (() => R) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4) => R; }; export declare function diMap<T1, T2, T3, T4, T5, R>(pred: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2, fn3: (...args: any[]) => T3, fn4: (...args: any[]) => T4, fn5: (...args: any[]) => T5): (() => R) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5) => R; }; export declare function diMap<T1, T2, T3, T4, T5, T6, R>(pred: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2, fn3: (...args: any[]) => T3, fn4: (...args: any[]) => T4, fn5: (...args: any[]) => T5, fn6: (...args: any[]) => T6): (() => R) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6) => R; }; export declare function diMap<T1, T2, T3, T4, T5, T6, T7, R>(pred: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2, fn3: (...args: any[]) => T3, fn4: (...args: any[]) => T4, fn5: (...args: any[]) => T5, fn6: (...args: any[]) => T6, fn7: (...args: any[]) => T7): (() => R) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7) => R; }; export declare function diMap<T1, T2, T3, T4, T5, T6, T7, T8, R>(pred: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2, fn3: (...args: any[]) => T3, fn4: (...args: any[]) => T4, fn5: (...args: any[]) => T5, fn6: (...args: any[]) => T6, fn7: (...args: any[]) => T7, fn8: (...args: any[]) => T8): (() => R) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8) => R; }; export declare function diMap<T1, T2, T3, T4, T5, T6, T7, T8, T9, R>(pred: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8, x9: T9) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2, fn3: (...args: any[]) => T3, fn4: (...args: any[]) => T4, fn5: (...args: any[]) => T5, fn6: (...args: any[]) => T6, fn7: (...args: any[]) => T7, fn8: (...args: any[]) => T8, fn9: (...args: any[]) => T9): (() => R) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8, x9: T9) => R; }; export declare function diMap<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R>(pred: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8, x9: T9, x10: T10) => R, fn1: (...args: any[]) => T1, fn2: (...args: any[]) => T2, fn3: (...args: any[]) => T3, fn4: (...args: any[]) => T4, fn5: (...args: any[]) => T5, fn6: (...args: any[]) => T6, fn7: (...args: any[]) => T7, fn8: (...args: any[]) => T8, fn9: (...args: any[]) => T9, fn10: (...args: any[]) => T10): (() => R) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8, x9: T9, x10: T10) => R; }; export declare const diMapOnce: typeof diMap; export declare function dise<R>(effect: () => Promise<R>, dicOutput: (x: R) => R): (() => Promise<R>) & { raw: () => Promise<R>; }; export declare function dise<R, T1>(effect: (x1: T1) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1): (() => Promise<R>) & { raw: (x1: T1) => Promise<R>; }; export declare function dise<R, I, T1, T2>(effect: (x1: T1, x2: T2) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2): (() => Promise<R>) & { raw: (x1: T1, x2: T2) => Promise<R>; }; export declare function dise<R, T1, T2, T3>(effect: (x1: T1, x2: T2, x3: T3) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2, dicInput3: () => T3): (() => Promise<R>) & { raw: (x1: T1, x2: T2, x3: T3) => Promise<R>; }; export declare function dise<R, T1, T2, T3, T4>(effect: (x1: T1, x2: T2, x3: T3, x4: T4) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2, dicInput3: () => T3, dicInput4: () => T4): (() => Promise<R>) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4) => Promise<R>; }; export declare function dise<R, T1, T2, T3, T4, T5>(effect: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2, dicInput3: () => T3, dicInput4: () => T4, dicInput5: () => T5): (() => Promise<R>) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5) => Promise<R>; }; export declare function dise<R, T1, T2, T3, T4, T5, T6>(effect: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2, dicInput3: () => T3, dicInput4: () => T4, dicInput5: () => T5, dicInput6: () => T6): (() => Promise<R>) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6) => Promise<R>; }; export declare function dise<R, T1, T2, T3, T4, T5, T6, T7>(effect: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2, dicInput3: () => T3, dicInput4: () => T4, dicInput5: () => T5, dicInput6: () => T6, dicInput7: () => T7): (() => Promise<R>) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7) => Promise<R>; }; export declare function dise<R, T1, T2, T3, T4, T5, T6, T7, T8>(effect: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2, dicInput3: () => T3, dicInput4: () => T4, dicInput5: () => T5, dicInput6: () => T6, dicInput7: () => T7, dicInput8: () => T8): (() => Promise<R>) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8) => Promise<R>; }; export declare function dise<R, T1, T2, T3, T4, T5, T6, T7, T8, T9>(effect: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8, x9: T9) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2, dicInput3: () => T3, dicInput4: () => T4, dicInput5: () => T5, dicInput6: () => T6, dicInput7: () => T7, dicInput8: () => T8, dicInput9: () => T9): (() => Promise<R>) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8, x9: T9) => Promise<R>; }; export declare function dise<R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(effect: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8, x9: T9, x10: T10) => Promise<R>, dicOutput: (x: R) => R, dicInput1: () => T1, dicInput2: () => T2, dicInput3: () => T3, dicInput4: () => T4, dicInput5: () => T5, dicInput6: () => T6, dicInput7: () => T7, dicInput8: () => T8, dicInput9: () => T9, dicInput10: () => T10): (() => Promise<R>) & { raw: (x1: T1, x2: T2, x3: T3, x4: T4, x5: T5, x6: T6, x7: T7, x8: T8, x9: T9, x10: T10) => Promise<R>; }; export declare const diseSet: <T>(fun: { raw: T; }, replacement: T) => void; export {};