UNPKG

fluoro

Version:
167 lines (166 loc) 5.45 kB
import Tokens from './tokens'; import { Events, type EventsMapping } from './events'; import Modules from './modules'; interface obj { [propName: string | number | symbol]: any; } /** Context keys */ export type CommonKeys = keyof obj; /** Context keys */ export type ContextKeys = Exclude<keyof Context, keyof ContextOrigin>; /** Context identity */ export type IdentityType = string | symbol; /** Context origin */ export interface ContextOrigin { readonly [Tokens.container]: Map<CommonKeys, obj>; readonly [Tokens.tracker]: Map<CommonKeys, CommonKeys | undefined>; readonly [Tokens.record]: Set<this>; readonly identity?: IdentityType; readonly root: this; readonly parent?: this; get(prop: CommonKeys): obj | undefined; inject<T extends ContextKeys>(prop: T, force?: boolean): boolean; inject(prop: CommonKeys, force?: boolean): boolean; provide<T extends obj>(prop: CommonKeys, value: T): boolean; mixin<K extends ContextKeys>(prop: CommonKeys, keys: K[], force?: boolean): boolean; mixin(prop: CommonKeys, keys: CommonKeys[], force?: boolean): boolean; extends(identity?: IdentityType): this; extends(_: Exclude<unknown, IdentityType>, identity?: IdentityType): this; } export interface Context<E = EventsMapping> { /** * Emits an event with the given type and data. * * @template T - The type of the event * @param type - The event type to emit * @param data - The data to pass to the event listeners */ emit: Events<E>['emit']; /** * Emits an event and waits for all listeners to complete asynchronously. * * @template T - The type of the event * @param type - The event type to emit * @param data - The data to pass to the event listeners * @returns A promise that resolves when all listeners have completed */ parallel: Events<E>['parallel']; /** * Adds an event listener for the specified event type. * * @template T - The type of the event * @param type - The event type to listen for * @param callback - The callback function to be called when the event is emitted */ on: Events<E>['on']; /** * Adds a one-time event listener for the specified event type. * * @template T - The type of the event * @param type - The event type to listen for * @param callback - The callback function to be called when the event is emitted */ once: Events<E>['once']; /** * Removes an event listener for the specified event type. * * @template T - The type of the event * @param type - The event type to remove the listener from * @param callback - The callback function to be removed */ off: Events<E>['off']; /** * Removes all event listeners for the specified event type. * * @template - The type of the event * @param type - The event type to remove all listeners from */ offAll: Events<E>['offAll']; /** * Loads a module. * * @param instance - The module to load */ load: Modules<this>['load']; /** * Unloads a module. * * @param instance - The module to unload */ unload: Modules<this>['unload']; /** * Loads a service. * * @param instance - The service to load */ service: Modules<this>['service']; } /** * Context. */ export declare class Context<E = EventsMapping> implements ContextOrigin { /** Context container */ readonly [Tokens.container]: Map<CommonKeys, obj>; /** Context container */ readonly [Tokens.tracker]: Map<CommonKeys, CommonKeys | undefined>; /** Context record */ readonly [Tokens.record]: Set<this>; /** Context identity */ readonly identity?: IdentityType; /** Context root */ readonly root: this; /** Context parent */ readonly parent?: this; constructor(); /** * Context parent. * * @param parent - Context parent * @param identity - Context identity */ constructor(parent: Context, identity: IdentityType); /** * Get context property. * * @param prop - Context property * @returns Context property */ get<T = obj | undefined>(prop: CommonKeys): T; /** * Inject context. * * @param prop - Context property * @param force - Force inject * @returns boolean */ inject<T extends ContextKeys>(prop: T, force?: boolean): boolean; inject(prop: CommonKeys, force?: boolean): boolean; provide<T extends obj>(prop: CommonKeys, value: T): boolean; /** * Mixin context. * * @param prop - Context property * @param keys - Context keys * @param force - Force mixin * @returns boolean */ mixin<T extends ContextKeys>(prop: CommonKeys, keys: T[], force?: boolean): boolean; mixin(prop: CommonKeys, keys: CommonKeys[], force?: boolean): boolean; /** * Extends context. * * @param identity - Context identity * @returns Context */ extends(identity?: IdentityType): this; extends(_: Exclude<unknown, IdentityType>, identity?: IdentityType): this; /** * Find context by identity. * * @param identity - Context identity * @param mode - Search mode * @returns Context */ find(identity: IdentityType, mode?: 'up' | 'down' | 'both'): Context | undefined; } export default Context;