import "./_dnt.polyfills.js"; import { PureEventWithApply } from "./pure_event.js"; /** * 函数转化,一个新函数:它的第一个参数 将作为 原函数的 this */ export declare const uncurryThisFn: (func: (this: T, ...args: ARGS) => R) => ((self: T, ...restArgs: ARGS) => R); export type UncurryThisFn = T extends Func ? ReturnType, Func.Args, Func.Return>> : never; /** * 函数转化,一个新函数,它的this 是原函数的第一个参数 */ export declare const curryThisFn: (func: (self: T, ...args: ARGS) => R) => ((this: T, ...args: ARGS) => R); export type CurryThisFn = T extends Func ? ReturnType, Func.Args, Func.Return>> : never; /** * 类型安全的函数定义 */ export type Func = Arguments["length"] extends 0 ? (this: This) => Return : (this: This, ...args: Arguments) => Return; export declare namespace Func { type Return = F extends Func ? ReturnType : never; type AwaitedResult = { type: "resolved"; result: Awaited>; } | { type: "rejected"; error: unknown; }; type Args = F extends Func ? Parameters : never; type This = F extends Func ? T : never; type SetReturn = Func, Args, R>; } type KeyFun = Func, Parameters>; export type FuncRemember | void = void> = F & { readonly source: F; readonly key: Func.Return | undefined; readonly runned: boolean; readonly returnValue: Func.Return | undefined; readonly awaitedReturnValue: Func.AwaitedResult | undefined; reset(): void; rerun(...args: Parameters): Func.Return; }; export declare namespace func_remember { type Return | void = void> = FuncRemember; } /** * 让一个函数的返回结果是缓存的 * @param key 自定义缓存key生成器,如果生成的key不一样,那么缓存失效 * @returns */ export declare const func_remember: , Parameters> | void | void>(func: F, key?: K, cacheAwaited?: boolean) => FuncRemember; /** * 包裹一个“目标函数”,将它的执行权交给“包裹函数”。 * 包裹函数可以在目标函数执行之前或者执行之后做一些工作,比如参数检查,比如返回值修改 * @param func 目标函数 * @param wrapper 包裹函数,第一个参数是 context,可以获得详细的上下文;第二个参数是 next,可以用于快速执行“目标函数” */ export declare const func_wrap: (func: F, wrapper: (context: { target: F; this: ThisParameterType; arguments: Parameters; }, next: () => ReturnType) => R) => ((this: ThisParameterType, ...args: Parameters) => R); type PrototypeToThis = T extends String ? string : T extends Number ? number : T extends Boolean ? boolean : T extends BigInt ? bigint : T extends Symbol ? symbol : T; /** * 向某一个对象配置函数属性 */ export declare const extendsMethod: (target: T, prop: PropertyKey, method: Func>) => void; /** * 向某一个对象配置getter属性 */ export declare const extendsGetter: (target: T, prop: PropertyKey, getter: Func, []>) => void; export interface FuncCatch { (fn: F, errorParser?: (err: unknown) => E): FuncCatch.Wrapper; wrapSuccess: (resule: R) => FuncCatch.SuccessReturn; wrapError: (err: E, errorParser?: (err: unknown) => E) => FuncCatch.ErrorReturn; } export declare namespace FuncCatch { type Wrapper = Func, Parameters, Return>> & { catchType(errorParser?: (err: unknown) => E): Wrapper; }; type SuccessReturn = readonly [undefined, R] & { readonly success: true; readonly result: R; readonly error: void; }; type ErrorReturn = readonly [E, undefined] & { readonly success: false; readonly result: void; readonly error: E; }; type Return = [R] extends [never] ? ErrorReturn : R extends PromiseLike ? PromiseLike | ErrorReturn> : SuccessReturn | ErrorReturn; } /** * 包裹一个函数,并对其进行错误捕捉并返回 */ export declare const func_catch: FuncCatch; /** * 一个能延迟执行的函数包裹 * 和 func_remember 不同,`func_lazy(...args)` 等同于 `func_remember()(...args)` * @param factory * @returns */ export declare const func_lazy: (factory: Func.SetReturn) => T; declare const effect_symbol: unique symbol; type WithEffect = { [effect_symbol]: Func; }; /** * 让一个对象附带副作用 * @param source * @param effect * @returns */ export declare const withEffect: (source: T, effect: Func) => T & WithEffect; /** * 执行副作用 * @param sources */ export declare const applyEffect: (...sources: (WithEffect | object)[]) => void; /** * 并行执行一组函数,并限制并发数量。 * * @template T - 函数的类型,这些函数不接受参数 * @param {Iterable} funcs - 一个可迭代的函数集合,这些函数将被并行执行。 * @param {number} limit - 并发执行函数的最大数量。 * @returns {{ emit: (data: { source: T; result: FuncCatch.Return> }) => void, on: (cb: (data: { source: T; result: FuncCatch.Return> }) => void) => () => void, off: (cb: (data: { source: T; result: FuncCatch.Return> }) => void) => void, once: (cb: (data: { source: T; result: FuncCatch.Return> }) => void) => () => void, then: Promise['then'] }} 返回一个对象,包含一个事件发射器 `returns` 用于监听每个函数完成时的结果,以及一个 `then` 方法,该方法在所有函数都执行完毕后 resolve。 * 每个函数的结果会通过 `returns` 事件的 `emit` 方法发出,数据格式为 `{ source: T; result: FuncCatch.Return> }`。 * `result` 是一个元组,成功时为 `[undefined, R]`,失败时为 `[E, undefined]`,并附带 `success`、`result` 和 `error` 属性。 */ export declare const func_parallel_limit: >(funcs: Iterable | AsyncIterable, limit: number) => PureEventWithApply<{ source: T; result: FuncCatch.Return>; }> & PromiseLike; export {}; //# sourceMappingURL=func.d.ts.map