UNPKG

4.99 kBTypeScriptView Raw
1export type Event<EventName extends string = string, Payload extends unknown = unknown> = { [Key in EventName]: Payload; };
2/**
3 * - Event handler function
4 */
5export type Handler<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (news: T[Key]) => void;
6/**
7 * - Event subscription object
8 */
9export type Subscription<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = {
10 event: Key;
11 handler: Handler<T, Key>;
12};
13/**
14 * - Function to subscribe to an event
15 */
16export type Subscribe<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (event: Key, handler: Handler<T, Key>, times?: number) => Subscription<T, Key>;
17/**
18 * - Options for customizing the subscriber factory
19 */
20export type CreateSubscribeOptions = {
21 /**
22 * - If `true` the event names are case insenseitive
23 */
24 caseInsensitive: boolean;
25};
26/**
27 * - Factory function for `subscribe()`
28 */
29export type CreateSubscribe<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (stack: any, options?: Partial<CreateSubscribeOptions>) => Subscribe<T, Key>;
30/**
31 * - Function to unsubscribe from an event
32 */
33export type Unsubscribe<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (event: Key | Subscription<T, Key>, handler?: Handler<T, Key>) => void;
34/**
35 * - Options for customizing the unsubscriber factory
36 */
37export type CreateUnsubscribeOptions = {
38 /**
39 * - If `true` the event names are case insenseitive
40 */
41 caseInsensitive: boolean;
42};
43/**
44 * - Factory function for `unsubscribe()`
45 */
46export type CreateUnsubscribe<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (stack: any, options?: Partial<CreateUnsubscribeOptions>) => Unsubscribe<T, Key>;
47/**
48 * - Inform listeners about some news
49 */
50export type Inform<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (listeners: (Handler<T, Key>)[], news: T[Key]) => void;
51/**
52 * - Options for how to publish an event
53 */
54export type PublishOptions = {
55 /**
56 * - If `true` event will *not* be broadcasted gloablly even if `isGlobal` is `true`.
57 */
58 isNoGlobalBroadcast: boolean;
59 /**
60 * - If `true` event will *not* be broadcasted synchronously even if `async` is `false` globally.
61 */
62 async: boolean;
63};
64/**
65 * - Function to publish an event
66 */
67export type Publish<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (event: Key, news: T[Key], options?: Partial<PublishOptions>) => void;
68/**
69 * - Factory function for `publish()`
70 */
71export type CreatePublishOptions = {
72 /**
73 * - If `true` event will be published globally.
74 */
75 isGlobal: boolean;
76 /**
77 * - If `true` the event names are case insenseitive
78 */
79 caseInsensitive: boolean;
80 /**
81 * - If `true` the pub-sub instance publishes events asynchronously (recommended)
82 */
83 async: boolean;
84};
85/**
86 * - Factory function for `publish()`
87 */
88export type CreatePublish<T extends Event<string, unknown> = Event<string, unknown>> = (stack: any, options?: Partial<CreatePublishOptions>) => any;
89/**
90 * - Remove all event listeners
91 */
92export type Clear = () => void;
93/**
94 * - Factory function for `clear()`
95 */
96export type CreateClear<T extends Event<string, unknown> = Event<string, unknown>> = (stack: any) => Clear;
97/**
98 * - Event stack object that stores the events handers and notification times
99 */
100export type Stack<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = {
101 __times__: {};
102};
103/**
104 * - Options for customizing the pub-sub instance
105 */
106export type PubSubOptions = {
107 /**
108 * - If `true` the pub-sub instance publishes events asynchronously (recommended)
109 */
110 async: boolean;
111 /**
112 * - If `true` the event names are case insenseitive
113 */
114 caseInsensitive: boolean;
115 /**
116 * A custom event subscriber stack
117 */
118 stack: any;
119};
120/**
121 * - The pub-sub instance
122 */
123export type PubSub<T extends Event<string, unknown> = Event<string, unknown>> = {
124 publish: Publish<Event>;
125 subscribe: Subscribe<Event>;
126 unsubscribe: Unsubscribe<Event>;
127 clear: Clear;
128 stack: Stack;
129};
130/**
131 * - Create a new empty stack object
132 */
133export type CreateStack<T extends Event<string, unknown> = Event<string, unknown>> = () => Stack<T>;
134/**
135 * - Create a new pub-sub instance
136 */
137export type CreatePubSub<T extends Event<string, unknown> = Event<string, unknown>> = (options?: Partial<PubSubOptions>) => PubSub<T>;
138/**
139 * @typedef {(options?: Partial<PubSubOptions>) => PubSub<T>} CreatePubSub - Create a new pub-sub instance
140 * @template {Event} [T=Event]
141 */
142/**
143 * Create a new pub-sub instance
144 * @type {CreatePubSub}
145 */
146export const createPubSub: CreatePubSub;
147/**
148 * Global pub-sub instance
149 * @type {PubSub}
150 */
151export const globalPubSub: PubSub;
152export { createPubSub as default };