@stacksjs/events 0.58.73 → 0.59.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +25 -0
- package/package.json +1 -1
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type EventType = string | symbol;
|
|
2
|
+
export type Handler<T = unknown> = (event: T) => void;
|
|
3
|
+
export type WildcardHandler<T = Record<string, unknown>> = (type: keyof T, event: T[keyof T]) => void;
|
|
4
|
+
export type EventHandlerList<T = unknown> = Array<Handler<T>>;
|
|
5
|
+
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<WildcardHandler<T>>;
|
|
6
|
+
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map<keyof Events | '*', EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events>>;
|
|
7
|
+
export interface Emitter<Events extends Record<EventType, unknown>> {
|
|
8
|
+
all: EventHandlerMap<Events>;
|
|
9
|
+
on: (<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>) => void) & ((type: '*', handler: WildcardHandler<Events>) => void);
|
|
10
|
+
off: (<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>) => void) & ((type: '*', handler: WildcardHandler<Events>) => void);
|
|
11
|
+
emit: (<Key extends keyof Events>(type: Key, event: Events[Key]) => void) & (<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never) => void);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A tiny (~200b) functional event emitter / pubsub.
|
|
15
|
+
*/
|
|
16
|
+
export default function mitt<Events extends Record<EventType, unknown>>(all?: EventHandlerMap<Events>): Emitter<Events>;
|
|
17
|
+
declare const events: typeof mitt;
|
|
18
|
+
declare const emitter: Emitter<Record<EventType, unknown>>;
|
|
19
|
+
declare const useEvent: typeof emitter.emit;
|
|
20
|
+
declare const useEvents: Emitter<Record<EventType, unknown>>;
|
|
21
|
+
declare const dispatch: typeof emitter.emit;
|
|
22
|
+
declare const listen: typeof emitter.on;
|
|
23
|
+
declare const off: typeof emitter.off;
|
|
24
|
+
declare const all: typeof emitter.all;
|
|
25
|
+
export { all, dispatch, events, listen, mitt, off, useEvent, useEvents };
|