import { Func, Primitive } from './types'; import { IEvent, TEventTarget } from './events'; import { ICreateObsDispAPIParams, TGetObserversMap } from './api'; import { TObserverByIdMap } from './obsMap'; export declare const ON_CREATE_OBS_FIELD = "obs"; export declare const __removeAllObservers: (API: ICreateObsDispAPIParams, getObserversMap: TGetObserversMap) => () => void; export interface IObserverTreeNode extends IObserverOptions { children: IObserverTreeNode[]; } export interface IObserverOptions { /** Optional id (must be unique); if not passed, it is autogenerated */ id?: string; /** * Optional name; it's an arbitrary key, not used by the library internally; can be * useful for debugging / display purposes or when dispatching events (target filter); * does not need to be unique * */ name?: string; /** * Arbitrary meta data for the given observer; can be * useful for debugging / display purposes or when dispatching events (target filter); * does not need to be unique */ meta?: Record; } export interface IObserver { handleEvent: (event: IEvent) => void; options?: IObserverOptions; parentId?: string; forChildren: (mapper: (obs: IObserver, childrenMap: TObserverByIdMap) => void) => void; /** Adds a child observer */ addOD: (obs: IObserver) => IObserver; dispatchEvent: (...args: Parameters>) => ReturnType>; /** * Removes the child observer that is passed * @param obs * @param skipChildren If passed as `true`, this will not automatically remove child observers */ removeOD: (obs: IObserver, skipChildren: boolean) => void; } export type TEventDispatchOptions = { payload?: T extends IEvent ? T['payload'] : {}; target?: TEventTarget | TEventTarget[]; }; /** * Dispatches an obs-disp event. * Note: do not use self-referencing objects (expect exceptions) for payload * */ export declare const __dispatchEvent: (API: ICreateObsDispAPIParams, getObserversMap: TGetObserversMap) => (event: string | Partial, options?: TEventDispatchOptions) => void; export type TDispatchEventType = (event: Partial | IEvent['name'], options?: TEventDispatchOptions) => void; /** * Same as dispatchEvent but can be throttled via throttleMs * @param ev The default event (can be overriden when calling the function) * @param throttleMs default 200 * @returns A throttled function that dispatches an event, e.g. as a generic func `dispatchThrottledEv = createThrottledDispatch(undefined, 200)` * ... later in the code ... `dispatchThrottledEv({ name: 'SOME_EVENT', payload: {}})` * * Or as a more specific throttled event func: * `dispatchSomeThrottledEv = createThrottledDispatch({ name: 'SOME_EVENT', 200)` * ... later in the code ... or overriding the payload if needed * `dispatchSomeThrottledEv({ payload: { somePayloadProp: 1 }})` */ export declare const __createThrottledDispatch: (API: ICreateObsDispAPIParams, getObserversMap: TGetObserversMap) => (ev: IEvent, opts?: { throttleMs?: number; debugLog?: string; }) => () => any; export interface IObserverEventHandlers extends Record> { } /** * Adds (creates) a new global Observer (Observable Dispatcher or obs-disp) * which starts tracking events. * * Will generate a unique ID or use the provided ID (only one OD can exists with the same id) * * When an observer with the same id already exists (and replaceExisting is not passed or is `false`) * then a warning is thrown (can be handled via the `onWarn` handler on the API) and the existing observer is returned * @param eventHandlersOrFunc Either a map of event-name-to-handler or a function that returns such () => ({ ... }) * @param options Common options available to each observer (like `id`) * @param props Initial properties which would be passed (convenient when not using this directly but * when using obsDispCreator((props: Props) => {...})) * @returns */ export declare const __addObsDisp: (API: ICreateObsDispAPIParams, getObserversMap: TGetObserversMap) => (eventHandlersOrFunc: IObserverEventHandlers | Func, options?: IObserver['options'], props?: T) => IObserver; declare const __getSampleAddObsDispFn: () => (eventHandlersOrFunc: IObserverEventHandlers | Func, options?: IObserver['options'], props?: T) => IObserver; export interface IAddObsDispType extends ReturnType> { } export interface IAddObsDispParameters extends Parameters> { } export interface IAddObsDispReturnType extends ReturnType> { } /** * The initial parameters (props) when creating an obs-disp - a plain object or nothing (void) * */ export type TObsCreateParams = Record | void; export declare const __obsDispCreator: (API: ICreateObsDispAPIParams, getObserversMap: TGetObserversMap) => (...args: [IAddObsDispParameters[0], IAddObsDispParameters[1]?, IAddObsDispParameters[2]?]) => (props: T) => IObserver; /** * Removes the observer (along with all of its children). * You can disable child removal by passing skipChildren as false * @param obs * @param skipChildren If passed as `true`, this will not automatically remove child observers */ export declare const __removeObs: (API: ICreateObsDispAPIParams, getObserversMap: TGetObserversMap) => (observer: IObserver, skipChildren?: boolean) => void; export declare const __removeObsById: (API: ICreateObsDispAPIParams, getObserversMap: TGetObserversMap) => (id: string) => void; export declare const __checkObsExists: (getObserversMap: TGetObserversMap) => (obs: IObserver) => boolean; export declare const __checkObsExistsById: (getObserversMap: TGetObserversMap) => (id: string) => boolean; export declare const addMultipleObs: (getObserversMap: TGetObserversMap) => (evHandlersOrFuncs: (Func | IObserverEventHandlers)[]) => IObserver[]; export {};