UNPKG

@studiometa/js-toolkit

Version:

A set of useful little bits of JavaScript to boost your project! 🚀

99 lines (98 loc) • 2.65 kB
export interface ServiceInterface<T> { /** * Remove a function from the resize service by its key. */ remove(key: string | symbol): void; /** * Add a callback to the service. The callback will receive the current service props as parameter. */ add(key: string | symbol, callback: (props: T) => void): void; /** * Test if the service has alreaydy a callback for the given key. */ has(key: string | symbol): boolean; /** * Get the service's props. */ props(): T; } /** * Service configuration of events to be attached to targets. */ export type ServiceConfig = [ (instance: AbstractService) => EventTarget, [ string, AddEventListenerOptions? ][] ][]; /** * AbstractService class. */ export declare class AbstractService<PropsType = any> { /** * Used to type `this.constructor` correctly * @link https://github.com/microsoft/TypeScript/issues/3841#issuecomment-2381594311 */ ['constructor']: typeof AbstractService; /** * Service configuration. */ static config: ServiceConfig; /** * Cache for the created instances. */ static __instances: Map<any, any>; /** * Get a service instance as a singleton based on the given key. */ static getInstance<T extends ServiceInterface<any>>(keys?: any, ...args: any[]): T; /** * Is the service active or not? */ __isInit: boolean; /** * Props for the service. */ props: {}; /** * Holds all the callbacks that will be triggered. */ callbacks: Map<string | symbol, (props: PropsType) => unknown>; /** * Does the service has the given key? */ has(key: string | symbol): boolean; /** * Get a service callback by its key. */ get(key: string | symbol): (props: PropsType) => unknown; /** * Add a callback to the service. */ add(key: string | symbol, callback: (props: PropsType) => unknown): void; /** * Remove a callback from the service by its key. */ remove(key: string | symbol): void; /** * Trigger all service callbacks with service props. */ trigger(props: PropsType): void; /** * Implements the EventListenerObject interface. */ handleEvent(event: Event): void; /** * Add or remove event listeners based on the static `config` property. */ __manageEvents(mode: 'add' | 'remove'): void; /** * Triggered when the service is initialized. */ init(): void; /** * Triggered when the service is killed. */ kill(): void; }