UNPKG

@akala/core

Version:
154 lines (153 loc) 6.64 kB
import { SimpleInjector } from './injectors/simple-injector.js'; import { type Listener } from './events/shared.js'; import { AsyncEvent } from './events/async.js'; import type { Injectable, InjectableAsyncWithTypedThis, InjectableWithTypedThis, Resolvable } from './injectors/shared.js'; /** * Extended event class that supports async completion tracking * @template T - Type of event arguments * @extends AsyncEvent<[ExtendableEvent<T>]> */ export declare class ExtendableEvent<T = void> extends AsyncEvent<[ExtendableEvent<T>]> { private readonly once; private markAsDone; private _triggered; /** * Create an ExtendableEvent * @param once - Whether the event should only trigger once */ constructor(once: boolean); private promises; /** * Add a promise to wait for before marking event as complete * @template T - Type of promise result * @param p - Promise to wait for */ waitUntil<T>(p: PromiseLike<T>): void; /** * Reset event state for reuse (if configured with once=false) * @throws Error if reset during incomplete event or after single use */ reset(): void; /** Event arguments passed during triggering */ eventArgs: T; /** * Trigger the event with given arguments * @param value - Arguments to pass to event handlers */ trigger(value: T): Promise<void>; /** * Add event listener * @param handler - Handler function to add * @returns Self for chaining */ addListener(handler: Listener<[this], void | PromiseLike<void>>): import("./teardown-manager.js").Subscription; /** Whether the event has been triggered */ get triggered(): boolean; private _whenDone; /** Promise that resolves when event completes */ get whenDone(): Promise<T>; /** Complete all pending promises and mark event done */ complete(): Promise<void>; /** Whether the event has completed */ get done(): boolean; private _done; } /** * Core module management class handling dependency injection and lifecycle events * @extends SimpleInjector */ export declare class Module extends SimpleInjector { name: string; dep?: Module[]; /** * Create a new Module * @param name - Unique module name * @param dep - Optional array of module dependencies */ constructor(name: string, dep?: Module[]); private static readonly o; /** Event triggered when module activates */ readonly activateEvent: ExtendableEvent<void>; /** Event triggered when module is fully ready */ readonly readyEvent: ExtendableEvent<void>; /** * Add a module dependency * @param m - Module to add as dependency */ addDependency(m: Module): void; /** * Register a module with the orchestrator * @param m - Module to register */ static registerModule(m: Module): void; /** * Register ready handler with dependency injection * @template TArgs - Argument types * @param toInject - Names of dependencies to inject * @param f - Handler function * @returns Self for chaining */ ready<TArgs extends unknown[]>(toInject: Resolvable[], f: InjectableWithTypedThis<void | Promise<void>, ExtendableEvent, TArgs>): any; ready<TArgs extends unknown[]>(toInject: Resolvable[]): (f: InjectableWithTypedThis<void | Promise<void>, ExtendableEvent, TArgs>) => this; /** * Register async ready handler with dependency injection * @template TArgs - Argument types * @param toInject - Names of dependencies to inject * @param f - Async handler function * @returns Self for chaining */ readyAsync<TArgs extends unknown[]>(toInject: Resolvable[], f: InjectableAsyncWithTypedThis<void, ExtendableEvent, TArgs>): any; readyAsync<TArgs extends unknown[]>(toInject: Resolvable[]): (f: InjectableWithTypedThis<void, ExtendableEvent, TArgs>) => this; /** * Register activation handler with dependency injection * @template TArgs - Argument types * @param toInject - Names of dependencies to inject * @param f - Handler function * @returns Self for chaining */ activate<TArgs extends unknown[]>(toInject: Resolvable[], f: InjectableWithTypedThis<void | Promise<void>, ExtendableEvent, TArgs>): this; activate<TArgs extends unknown[]>(toInject: Resolvable[]): (f: InjectableWithTypedThis<void | Promise<void>, ExtendableEvent, TArgs>) => this; /** * Register async activation handler with dependency injection * @template TArgs - Argument types * @param toInject - Names of dependencies to inject * @param f - Async handler function * @returns Self for chaining */ activateAsync<TArgs extends unknown[]>(toInject: Resolvable[], f: InjectableAsyncWithTypedThis<void, ExtendableEvent, TArgs>): any; activateAsync<TArgs extends unknown[]>(toInject: Resolvable[]): (f: InjectableWithTypedThis<void, ExtendableEvent, TArgs>) => this; /** * Create activation handler for class instantiation * @param toInject - Names of dependencies to inject * @returns Decorator function for class constructor */ activateNew(...toInject: Resolvable[]): <T>(ctor: new (...args: unknown[]) => T) => void; /** * Create async activation handler for class instantiation * @param toInject - Names of dependencies to inject * @returns Decorator function for class constructor */ activateNewAsync(...toInject: Resolvable[]): <T>(ctor: new (...args: unknown[]) => T) => void; /** * Create ready handler for class instantiation * @param toInject - Names of dependencies to inject * @returns Decorator function for class constructor */ readyNew(...toInject: Resolvable[]): <T>(ctor: new (...args: unknown[]) => T) => void; /** * Create async ready handler for class instantiation * @param toInject - Names of dependencies to inject * @returns Decorator function for class constructor */ readyNewAsync(...toInject: Resolvable[]): <T>(ctor: new (...args: unknown[]) => T) => void; /** * Start the module lifecycle * @template TArgs - Argument types * @param toInject - Names of dependencies to inject * @param f - Optional handler function * @returns Promise that resolves when module stops */ start<TArgs extends unknown[]>(toInject?: string[], f?: Injectable<unknown, TArgs>): Promise<void>; } export declare function module(name: string, ...dependencies: string[]): Module; export declare function module(name: string, ...dependencies: Module[]): Module;