@solid-primitives/event-bus
Version:
A collection of SolidJS primitives providing various features of a pubsub/event-emitter/event-bus.
85 lines (84 loc) • 2.93 kB
TypeScript
import { EventBusCore, type Listener } from "./eventBus.js";
export declare class EmitterCore<M extends Record<PropertyKey, any>> extends Map<keyof M, EventBusCore<M[any]>> {
on<K extends keyof M>(event: K, listener: Listener<M[K]>): void;
off<K extends keyof M>(event: K, listener: Listener<M[K]>): void;
emit<K extends keyof M>(event: K, ..._: void extends M[K] ? [payload?: M[K]] : [payload: M[K]]): void;
}
export type EmitterOn<M extends Record<PropertyKey, any>> = <K extends keyof M>(event: K, listener: Listener<M[K]>) => VoidFunction;
export type EmitterEmit<M extends Record<PropertyKey, any>> = EmitterCore<M>["emit"];
export type Emitter<M extends Record<PropertyKey, any>> = {
readonly on: EmitterOn<M>;
readonly emit: EmitterEmit<M>;
readonly clear: VoidFunction;
};
/**
* Creates an emitter with which you can listen to and emit various events.
*
* @returns emitter mathods: `{on, emit, clear}`
*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/event-bus#createEmitter
*
* @example
* const emitter = createEmitter<{
* foo: number;
* bar: string;
* }>();
* // can be destructured
* const { on, emit, clear } = emitter;
*
* emitter.on("foo", e => {});
* emitter.on("bar", e => {});
*
* emitter.emit("foo", 0);
* emitter.emit("bar", "hello");
*/
export declare function createEmitter<M extends Record<PropertyKey, any>>(): Emitter<M>;
export type EmitterPayload<M extends Record<PropertyKey, any>> = {
[K in keyof M]: {
readonly name: K;
readonly details: M[K];
};
}[keyof M];
export type EmitterListener<M extends Record<PropertyKey, any>> = (payload: EmitterPayload<M>) => void;
export type EmitterListen<M extends Record<PropertyKey, any>> = (listener: EmitterListener<M>) => VoidFunction;
export type GlobalEmitter<M extends Record<PropertyKey, any>> = {
readonly on: EmitterOn<M>;
readonly listen: EmitterListen<M>;
readonly emit: EmitterEmit<M>;
readonly clear: VoidFunction;
};
/**
* Creates an emitter with which you can listen to and emit various events. With this emitter you can also listen to all events.
*
* @returns emitter mathods: `{on, listen, emit, clear}`
*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/event-bus#createGlobalEmitter
*
* @example
* const emitter = createGlobalEmitter<{
* foo: number;
* bar: string;
* }>();
* // can be destructured
* const { on, emit, clear, listen } = emitter;
*
* emitter.on("foo", e => {});
* emitter.on("bar", e => {});
*
* emitter.emit("foo", 0);
* emitter.emit("bar", "hello");
*
* emitter.listen(e => {
* switch (e.name) {
* case "foo": {
* e.details;
* break;
* }
* case "bar": {
* e.details;
* break;
* }
* }
* })
*/
export declare function createGlobalEmitter<M extends Record<PropertyKey, any>>(): GlobalEmitter<M>;