vue-hooks-plus
Version:
Vue hooks library
57 lines (56 loc) • 1.86 kB
TypeScript
type EventKey = string | number;
type EventParams = any;
type Subscription<T = EventParams, K extends EventKey = EventKey> = (params: {
params: T;
event: K;
}) => void;
type SubscriptionParams<T = EventParams, K extends EventKey = EventKey> = {
params: T;
event: K;
};
interface EventEmitterOptions {
global?: boolean;
}
declare class EventEmitter<T = EventParams, K extends EventKey = string> {
private subscriptions;
private emitEffectCache;
private readonly isGlobal;
private static instance;
constructor(options?: EventEmitterOptions);
/**
* 获取全局单例
*/
static getInstance<T = EventParams, K extends EventKey = string>(): EventEmitter<T, K>;
/**
* 订阅事件
* @param event 事件名
* @param listener 订阅回调
* @returns 取消订阅函数
*/
subscribe<E extends K = K>(event: E, listener: Subscription<T, E>): () => void;
/**
* 发送事件通知
* @param event 事件名
* @param args 事件参数
*/
emit<E extends K = K>(event: E, ...args: T extends any[] ? T : [T]): void;
/**
* 回放缓存事件(仅非全局实例)
*/
emitEffect<E extends K = K>(event: E): void;
/**
* 移除监听
*/
removeListener<E extends K = K>(event: E, listener?: Subscription<T, E>): void;
/**
* 清空所有事件
*/
clear(): void;
}
/**
* Vue hook: 订阅事件,自动解绑
*/
declare function useEventEmitterSubscription<T = EventParams, K extends EventKey = string, E extends K = K>(event: E, listener: Subscription<T, E>, emitter?: EventEmitter<T, K>): void;
declare const eventEmitterOverall: EventEmitter<any, string>;
export { EventEmitter, eventEmitterOverall, useEventEmitterSubscription, };
export type { EventKey, EventParams, Subscription, SubscriptionParams };