lakutata
Version:
An IoC-based universal application framework.
295 lines (290 loc) • 10.6 kB
TypeScript
import { AsyncConstructor } from './TypeDef.internal.140.js';
import { IBaseObjectConstructor } from './TypeDef.internal.118.js';
import { Module, DTO } from './TypeDef.internal.96.js';
import { ConstructorOptions, event, ListenerFn, EventAndListener } from './TypeDef.internal.19.js';
import { ActionPattern } from './TypeDef.internal.100.js';
/**
* Lakutata object base class
*/
declare class BaseObject extends AsyncConstructor {
#private;
/**
* Constructor
* @param cradleProxy
*/
constructor(cradleProxy: Record<string | symbol, any>);
/**
* Return class's name
*/
static get className(): string;
/**
* Get instance's class name
*/
get className(): string;
/**
* Initializer
* @protected
*/
protected init(): Promise<void>;
/**
* Destroyer
* @protected
*/
protected destroy(): Promise<void>;
/**
* Get registered object via constructor
* @param constructor
* @param configurableRecords
*/
protected getObject<T extends BaseObject>(constructor: IBaseObjectConstructor<T>, configurableRecords?: Record<string, any>): Promise<T>;
/**
* Get registered object via string
* @param name
* @param configurableRecords
*/
protected getObject<T extends BaseObject>(name: string, configurableRecords?: Record<string, any>): Promise<T>;
/**
* Get registered object via symbol
* @param name
* @param configurableRecords
*/
protected getObject<T extends BaseObject>(name: symbol, configurableRecords?: Record<string, any>): Promise<T>;
/**
* Get registered object via string or symbol
* @param name
* @param configurableRecords
* @protected
*/
protected getObject<T extends BaseObject>(name: string | symbol, configurableRecords?: Record<string, any>): Promise<T>;
/**
* Get registered object via string or symbol or constructor
* @param nameOrConstructor
* @param configurableRecords
* @protected
*/
protected getObject<T extends BaseObject>(nameOrConstructor: string | symbol | IBaseObjectConstructor<T>, configurableRecords?: Record<string, any>): Promise<T>;
/**
* Instantiate an instance of a base object class by injecting dependencies, but without registering it in the container
* @param constructor
* @param configurableRecords
* @protected
*/
protected buildObject<T extends BaseObject>(constructor: IBaseObjectConstructor<T>, configurableRecords?: Record<string, any>): Promise<T>;
/**
* Get current object's parent
* @protected
*/
protected getParent(): BaseObject | undefined;
/**
* Get current object's parent module
* @protected
*/
protected getModule(): Module;
/**
* Unique object uuid
*/
get $uuid(): string;
/**
* Object instance id which defined in runtime
*/
get $id(): string | symbol;
/**
* Object instance symbol
*/
get $symbol(): symbol;
/**
* Set object property
* @param propertyKey
* @param value
*/
setProperty(propertyKey: string, value: any): void;
/**
* Get object's property value
* @param propertyKey
* @param defaultValue
*/
getProperty<T = any>(propertyKey: string, defaultValue?: T): T;
/**
* Is object has property
* @param propertyKey
*/
hasProperty(propertyKey: string | symbol): boolean;
/**
* Get own property symbols
*/
propertySymbols(): symbol[];
/**
* Get own property names
*/
propertyNames(): string[];
/**
* Is object has method
* @param name
*/
hasMethod(name: string | symbol): boolean;
/**
* Get method from object
* @param name
* @param throwExceptionIfNotFound
*/
getMethod(name: string | symbol, throwExceptionIfNotFound?: boolean): (...args: any[]) => any | Promise<any>;
/**
* Dispose current object
* @description Call this function will invoke internal destroy method
*/
dispose(): Promise<void>;
}
type TEventEmitterOptions = ConstructorOptions;
declare class EventEmitter {
constructor(options?: TEventEmitterOptions);
/**
* emitter.emit(event | eventNS, [arg1], [arg2], [...])
* Execute each of the listeners that may be listening for the specified event name in order with the list of arguments.
* @param event
* @param values
*/
emit(event: string | symbol | event[], ...values: any[]): boolean;
/**
* emitter.emitRequest(event | eventNS, [arg1], [arg2], [...])
* Return the results of the listeners via Promise.all.
* @param event
* @param values
*/
emitRequest(event: string | symbol | event[], ...values: any[]): Promise<any[]>;
/**
* Adds a listener to the end of the listeners array for the specified event.
* @param event
* @param listener
*/
addListener(event: string | symbol | event[], listener: ListenerFn): this;
/**
* Adds a listener to the end of the listeners array for the specified event.
* @param event
* @param listener
*/
on(event: string | symbol | event[], listener: ListenerFn): this;
/**
* Adds a listener to the beginning of the listeners array for the specified event.
* @param event
* @param listener
*/
prependListener(event: string | symbol | event[], listener: ListenerFn): this;
/**
* Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed.
* @param event
* @param listener
*/
once(event: string | symbol | event[], listener: ListenerFn): this;
/**
* Adds a one time listener for the event. The listener is invoked only the first time the event is fired, after which it is removed. The listener is added to the beginning of the listeners array
* @param event
* @param listener
*/
prependOnceListener(event: string | symbol | event[], listener: ListenerFn): this;
/**
* Adds a listener that will execute n times for the event before being removed. The listener is invoked only the first n times the event is fired, after which it is removed.
* @param event
* @param timesToListen
* @param listener
*/
many(event: string | symbol | event[], timesToListen: number, listener: ListenerFn): this;
/**
* Adds a listener that will execute n times for the event before being removed. The listener is invoked only the first n times the event is fired, after which it is removed. The listener is added to the beginning of the listeners array.
* @param event
* @param timesToListen
* @param listener
*/
prependMany(event: string | symbol | event[], timesToListen: number, listener: ListenerFn): this;
/**
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback.
* @param listener
*/
onAny(listener: EventAndListener): this;
/**
* Adds a listener that will be fired when any event is emitted. The event name is passed as the first argument to the callback. The listener is added to the beginning of the listeners array
* @param listener
*/
prependAny(listener: EventAndListener): this;
/**
* Removes the listener that will be fired when any event is emitted.
* @param listener
*/
offAny(listener: ListenerFn): this;
/**
* Remove a specific listener from the listener array for the specified event.
* @param event
* @param listener
*/
removeListener(event: string | symbol | event[], listener: ListenerFn): this;
/**
* emitter.off(event | eventNS, listener)
* Remove a listener from the listener array for the specified event. Caution: Calling this method changes the array indices in the listener array behind the listener.
* @param event
* @param listener
*/
off(event: string | symbol | event[], listener: ListenerFn): this;
/**
* emitter.removeAllListeners([event | eventNS])
* Removes all listeners, or those of the specified event.
* @param event
*/
removeAllListeners(event?: string | symbol | event[] | undefined): this;
/**
* emitter.setMaxListeners(n)
* By default EventEmitters will print a warning if more than 10 listeners are added to it. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.
* @param n
*/
setMaxListeners(n: number): void;
/**
* emitter.getMaxListeners()
* Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n)
*/
getMaxListeners(): number;
/**
* emitter.eventNames(nsAsArray)
* Returns an array listing the events for which the emitter has registered listeners.
* Listeners order not guaranteed
* @param nsAsArray
*/
eventNames(nsAsArray?: boolean | undefined): (string | symbol | event[])[];
/**
* Returns listener count that are listening for specific event or events
* @param event
*/
listenerCount(event?: string | symbol | event[] | undefined): number;
/**
* emitter.listeners(event | eventNS)
* Returns an array of listeners for the specified event. This array can be manipulated, e.g. to remove listeners.
* @param event
*/
listeners(event?: string | symbol | event[] | undefined): ListenerFn[];
/**
* Returns an array of listeners that are listening for any event that is specified. This array can be manipulated, e.g. to remove listeners.
*/
listenersAny(): ListenerFn[];
/**
* hasListeners(event | eventNS?:String)
* Checks whether emitter has any listeners.
* @param event
*/
hasListeners(event?: String | undefined): boolean;
}
/**
* Context types
*/
declare enum ContextType {
CLI = "CLI",
HTTP = "HTTP",
SERVICE = "SERVICE"
}
type ContextParams<T extends {} = {}> = T & Record<string, any>;
/**
* Base context class
*/
declare class BaseContext<T extends Record<string, any> = {}> extends DTO {
readonly type: ContextType;
data: ActionPattern<T>;
constructor(params: ContextParams);
}
export { BaseContext, BaseObject, ContextType, EventEmitter };
export type { ContextParams };