@stevenleep/eventemitter
v0.0.2
Published
## 安装 ```bash pnpm add @stevenleep/eventemitter ```
Readme
@stevenleep/eventemitter
安装
pnpm add @stevenleep/eventemitter使用
import { EventEmitter } from "@stevenleep/eventemitter";
const eventEmitter = new EventEmitter();
eventEmitter.on("event", (data) => {
console.log(data); // hello world
});
eventEmitter.emit("event", "hello world");Define
import { BaseStoreMapProperties, StoreMaps, BaseStoreMap, CollectionStoreMap } from '@stevenleep/mapstore';
export { StoreMaps } from '@stevenleep/mapstore';
type EventListener$1<V = void> = (...args: any[]) => V;
interface EventListeners<Item = EventListener$1> {
on?: (event: BaseStoreMapProperties, listener: Item) => Function;
emit?: (event: BaseStoreMapProperties, ...args: any[]) => void;
off?: (event: BaseStoreMapProperties) => void;
once?: (event: BaseStoreMapProperties, listener: Item) => Function;
}
interface EventEmitterOptions {
maxListeners?: number;
storeMode?: StoreMaps | string;
store?: BaseStoreMap<any>;
}
declare abstract class AbstractEventEmitter<Item = EventListener> {
options: EventEmitterOptions;
listenerStore: CollectionStoreMap<Item> | BaseStoreMap<Item> | null;
onceEventStore: Set<BaseStoreMapProperties>;
protected constructor(options?: EventEmitterOptions);
/**
* @description: prepare store mode
*/
private prepare;
/**
* @title: init store
*
* @description: Out of the process of initialized Store will involve certain business logic,
* and the specific implementation is submitted to the subclass
*
* @param initStoreMode SupportedStoreEnum
* @param options EventEmitterOptions
*/
protected abstract initStore(initStoreMode: StoreMaps | string, options: EventEmitterOptions): void;
getListeners(key: BaseStoreMapProperties): Item[];
/**
* @description: Event belonging to only can only trigger once
*
* @param eventName BaseStoreMapProperties
* @returns boolean
*/
hasOnceEvent(eventName: BaseStoreMapProperties): boolean;
/**
* @description: Judgment is Collection type store
*/
isCollectionMode(): boolean;
protected requestCancelCallback(eventName: BaseStoreMapProperties, handler: Item): () => void;
}
/**
* @title: EventEmitter
* @description: EventEmitter is a class that can be used to register and trigger custom events.
*/
declare class EventEmitter<Item = EventListener> extends AbstractEventEmitter<Item> implements EventListeners<Item> {
constructor(options?: EventEmitterOptions);
/**
* @description: override abstract initStore method
* @param initializedStoreMode
* @param options EventEmitterOptions
* @returns void
*/
initStore(initializedStoreMode: StoreMaps, options: EventEmitterOptions): void;
on(eventName: BaseStoreMapProperties, event: Item): () => void;
emit(eventName: BaseStoreMapProperties, ...args: any[]): void;
emitWithFlow(eventName: BaseStoreMapProperties, ...args: any[]): any[];
once(eventName: BaseStoreMapProperties, event: any): () => void;
off(eventName: BaseStoreMapProperties): void;
}
export { EventEmitter, EventEmitterOptions, EventListener$1 as EventListener, EventListeners };
