@solid-primitives/event-bus
Version:
A collection of SolidJS primitives providing various features of a pubsub/event-emitter/event-bus.
60 lines (59 loc) • 2.48 kB
TypeScript
import { type AnyFunction } from "@solid-primitives/utils";
import type { Listen, Listener, Emit } from "./eventBus.js";
/**
* Turns a stream-like listen function, into a promise resolving when the first event is captured.
* @param subscribe listen function from any EventBus/Emitter
* @returns a promise resulting in the captured event value
*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/event-bus#toPromise
*
* @example
* const emitter = createEventBus<string>();
* const event = await toPromise(emitter.listen);
*/
export declare function toPromise<T>(subscribe: Listen<T>): Promise<T>;
/**
* Listen to any EventBus/Emitter, but the listener will automatically unsubscribe on the first captured event. So the callback will run only **once**.
*
* @param subscribe Emitter's `listen` function
* @param listener callback called when an event is emitted
*
* @returns unsubscribe function
*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/event-bus#once
*
* @example
* const { listen, emit } = createEventBus<string>();
* const unsub = once(listen, event => console.log(event));
*
* emit("foo") // will log "foo" and unsub
*
* emit("bar") // won't log
*/
export declare function once<T>(subscribe: Listen<T>, listener: Listener<T>): VoidFunction;
/**
* Wraps `emit` calls inside a `createEffect`. It causes that listeners execute having an reactive owner available. It allows for usage of effects, memos and other primitives inside listeners, without having to create a synthetic root.
*
* @param emit the emit function of any emitter/event-bus
* @returns modified emit function
*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/event-bus#toEffect
*
* @example
* const { listen, emit } = createEventBus();
* const emitInEffect = toEffect(emit);
* listen(() => console.log(getOwner()))
*
* // ...sometime later (after root initiation):
* emit() // listener will log `null`
* emitInEffect() // listener will log an owner object
*/
export declare function toEffect<T>(emit: Emit<T>): Emit<T>;
/**
* Wraps `emit` calls inside a `batch` call. It causes that listeners execute in a single batch, so they are not executed in sepatate queue ticks.
*
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/event-bus#batchEmits
*/
export declare function batchEmits<T extends {
emit: AnyFunction;
}>(bus: T): T;