1 | export type Event<EventName extends string = string, Payload extends unknown = unknown> = { [Key in EventName]: Payload; };
|
2 |
|
3 |
|
4 |
|
5 | export type Handler<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (news: T[Key]) => void;
|
6 |
|
7 |
|
8 |
|
9 | export 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 |
|
15 |
|
16 | export 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 |
|
19 |
|
20 | export type CreateSubscribeOptions = {
|
21 | |
22 |
|
23 |
|
24 | caseInsensitive: boolean;
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 | export type CreateSubscribe<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (stack: any, options?: Partial<CreateSubscribeOptions>) => Subscribe<T, Key>;
|
30 |
|
31 |
|
32 |
|
33 | export 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 |
|
36 |
|
37 | export type CreateUnsubscribeOptions = {
|
38 | |
39 |
|
40 |
|
41 | caseInsensitive: boolean;
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 | export type CreateUnsubscribe<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (stack: any, options?: Partial<CreateUnsubscribeOptions>) => Unsubscribe<T, Key>;
|
47 |
|
48 |
|
49 |
|
50 | export type Inform<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = (listeners: (Handler<T, Key>)[], news: T[Key]) => void;
|
51 |
|
52 |
|
53 |
|
54 | export type PublishOptions = {
|
55 | |
56 |
|
57 |
|
58 | isNoGlobalBroadcast: boolean;
|
59 | |
60 |
|
61 |
|
62 | async: boolean;
|
63 | };
|
64 |
|
65 |
|
66 |
|
67 | export 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 |
|
70 |
|
71 | export type CreatePublishOptions = {
|
72 | |
73 |
|
74 |
|
75 | isGlobal: boolean;
|
76 | |
77 |
|
78 |
|
79 | caseInsensitive: boolean;
|
80 | |
81 |
|
82 |
|
83 | async: boolean;
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 | export type CreatePublish<T extends Event<string, unknown> = Event<string, unknown>> = (stack: any, options?: Partial<CreatePublishOptions>) => any;
|
89 |
|
90 |
|
91 |
|
92 | export type Clear = () => void;
|
93 |
|
94 |
|
95 |
|
96 | export type CreateClear<T extends Event<string, unknown> = Event<string, unknown>> = (stack: any) => Clear;
|
97 |
|
98 |
|
99 |
|
100 | export type Stack<T extends Event<string, unknown> = Event<string, unknown>, Key extends keyof T> = {
|
101 | __times__: {};
|
102 | };
|
103 |
|
104 |
|
105 |
|
106 | export type PubSubOptions = {
|
107 | |
108 |
|
109 |
|
110 | async: boolean;
|
111 | |
112 |
|
113 |
|
114 | caseInsensitive: boolean;
|
115 | |
116 |
|
117 |
|
118 | stack: any;
|
119 | };
|
120 |
|
121 |
|
122 |
|
123 | export 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 |
|
132 |
|
133 | export type CreateStack<T extends Event<string, unknown> = Event<string, unknown>> = () => Stack<T>;
|
134 |
|
135 |
|
136 |
|
137 | export type CreatePubSub<T extends Event<string, unknown> = Event<string, unknown>> = (options?: Partial<PubSubOptions>) => PubSub<T>;
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | export const createPubSub: CreatePubSub;
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | export const globalPubSub: PubSub;
|
152 | export { createPubSub as default };
|