evently-react
Version:
A Hook-Based Framework for Event-Driven React Apps
26 lines (25 loc) • 1.1 kB
TypeScript
import { Events } from '../types';
import { EventBus } from '../core';
interface EventHook {
emitEvent: <E extends keyof Events>(eventName: E, payload: Events[E]) => void;
subscribeEvent: <E extends keyof Events>(eventName: E, callback: (payload: Events[E]) => void, priority?: number) => () => void;
eventBus: EventBus;
}
/**
* useEvent hook to emit and subscribe to events.
* @returns An object containing `emitEvent` and `subscribeEvent` functions.
*
* `emitEvent` - Emits an event with the given name and payload.
* @param eventName The name of the event to emit.
* @param payload The payload to send with the event.
*
* `subscribeEvent` - Subscribes to an event with the given name and callback.
* @param eventName The name of the event to subscribe to.
* @param callback The callback function to handle the event.
* @param priority The priority of the subscription.
* @returns A function to unsubscribe from the event.
*
* `eventBus` - The full EventBus instance for advanced use cases (e.g., middleware).
*/
export declare const useEvent: () => EventHook;
export {};