handlor
Version:
Handles and stores eventsListeners, timeouts/intervals and animationFrames and stores in one place for convenient cancelling and unsubscribing of events
55 lines (54 loc) • 1.31 kB
TypeScript
/**
* Enum for listener types
*/
export declare enum Type {
Timeout = "timeout",
Interval = "interval",
RequestAnimationFrame = "requestAnimationFrame"
}
/**
* TODO: Allow dynamic types based on listener type
* e.g. "interval" will give you only timeOutInMs for options
*/
interface TempListenerOptions {
[key: string]: any;
listenerNode?: HTMLElement | Window;
timeoutInMs?: number;
}
/**
* Stored Listener Interface
*/
interface StoredListener {
type: Type | string;
callback?: () => void;
id?: number;
options?: TempListenerOptions;
addEventListenerOptions?: boolean | AddEventListenerOptions;
}
declare type StoredListeners = {
[key: number]: StoredListener;
};
/**
* Interface for adding a listener
*/
export interface Handle {
type: Type | string;
callback: () => void;
options?: TempListenerOptions;
addEventListenerOptions?: boolean | AddEventListenerOptions;
}
/**
* Main class to handle listeners
*/
export declare class Handlor {
readonly listeners: StoredListeners;
/**
* Registers listeners and returns their IDs
*/
registerHandles(data: Handle | Handle[]): number[];
private addEvent;
cleanAll(): void;
cleanItems(keys: string[]): void;
cleanItem(key: string): Error | void;
}
export {};