TeaSpeak-Client/imports/shared-app/events.d.ts
2021-02-07 20:01:37 +01:00

97 lines
4.8 KiB
TypeScript

import * as React from "react";
export declare type EventPayloadObject = any;
export declare type EventMap<P> = {
[K in keyof P]: EventPayloadObject & {
type?: never;
};
};
export declare type Event<P extends EventMap<P>, T extends keyof P> = {
readonly type: T;
as<S extends T>(target: S): Event<P, S>;
asUnchecked<S extends T>(target: S): Event<P, S>;
asAnyUnchecked<S extends keyof P>(target: S): Event<P, S>;
/**
* Return an object containing only the event payload specific key value pairs.
*/
extractPayload(): P[T];
} & P[T];
export interface EventSender<Events extends EventMap<Events> = EventMap<any>> {
fire<T extends keyof Events>(event_type: T, data?: Events[T], overrideTypeKey?: boolean): any;
/**
* Fire an event later by using setTimeout(..)
* @param event_type The target event to be fired
* @param data The payload of the event
* @param callback The callback will be called after the event has been successfully dispatched
*/
fire_later<T extends keyof Events>(event_type: T, data?: Events[T], callback?: () => void): any;
/**
* Fire an event, which will be delayed until the next animation frame.
* This ensures that all react components have been successfully mounted/unmounted.
* @param event_type The target event to be fired
* @param data The payload of the event
* @param callback The callback will be called after the event has been successfully dispatched
*/
fire_react<T extends keyof Events>(event_type: T, data?: Events[T], callback?: () => void): any;
}
export declare type EventDispatchType = "sync" | "later" | "react";
export interface EventConsumer {
handleEvent(mode: EventDispatchType, type: string, data: any): any;
}
export declare class Registry<Events extends EventMap<Events> = EventMap<any>> implements EventSender<Events> {
protected readonly registryUniqueId: any;
protected persistentEventHandler: {
[key: string]: ((event: any) => void)[];
};
protected oneShotEventHandler: {
[key: string]: ((event: any) => void)[];
};
protected genericEventHandler: ((event: any) => void)[];
protected consumer: EventConsumer[];
private ipcConsumer;
private debugPrefix;
private warnUnhandledEvents;
private pendingAsyncCallbacks;
private pendingAsyncCallbacksTimeout;
private pendingReactCallbacks;
private pendingReactCallbacksFrame;
static fromIpcDescription<Events extends EventMap<Events> = EventMap<any>>(description: IpcRegistryDescription<Events>): Registry<Events>;
constructor();
destroy(): void;
enableDebug(prefix: string): void;
disableDebug(): void;
enableWarnUnhandledEvents(): void;
disableWarnUnhandledEvents(): void;
fire<T extends keyof Events>(eventType: T, data?: Events[T], overrideTypeKey?: boolean): void;
fire_later<T extends keyof Events>(eventType: T, data?: Events[T], callback?: () => void): void;
fire_react<T extends keyof Events>(eventType: T, data?: Events[T], callback?: () => void): void;
on<T extends keyof Events>(event: T | T[], handler: (event: Event<Events, T>) => void): () => void;
one<T extends keyof Events>(event: T | T[], handler: (event: Event<Events, T>) => void): () => void;
off(handler: (event: Event<Events, keyof Events>) => void): any;
off<T extends keyof Events>(events: T | T[], handler: (event: Event<Events, T>) => void): any;
onAll(handler: (event: Event<Events, keyof Events>) => void): () => void;
offAll(handler: (event: Event<Events, keyof Events>) => void): void;
/**
* @param event
* @param handler
* @param condition If a boolean the event handler will only be registered if the condition is true
* @param reactEffectDependencies
*/
reactUse<T extends keyof Events>(event: T | T[], handler: (event: Event<Events, T>) => void, condition?: boolean, reactEffectDependencies?: any[]): any;
private doInvokeEvent;
private invokeAsyncCallbacks;
private invokeReactCallbacks;
registerHandler(handler: any, parentClasses?: boolean): void;
unregisterHandler(handler: any): void;
registerConsumer(consumer: EventConsumer): () => void;
unregisterConsumer(consumer: EventConsumer): void;
generateIpcDescription(): IpcRegistryDescription<Events>;
}
export declare type RegistryMap = {
[key: string]: any;
};
export declare function EventHandler<EventTypes>(events: (keyof EventTypes) | (keyof EventTypes)[]): (target: any, propertyKey: string, _descriptor: PropertyDescriptor) => void;
export declare function ReactEventHandler<ObjectClass = React.Component<any, any>, Events = any>(registry_callback: (object: ObjectClass) => Registry<Events>): (constructor: Function) => void;
export declare type IpcRegistryDescription<Events extends EventMap<Events> = EventMap<any>> = {
ipcChannelId: string;
};