-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventCombiner.ts
277 lines (255 loc) Β· 13 KB
/
EventCombiner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { DomainEvent } from './DomainEvent';
import { AllThen, Consume, Once, SomeThen } from './EventCombiner.type';
import { EventHandler } from './EventHandler';
/** ### Combines multiple events to let subscribers listen to any combination of the events
*
* There are 8 different combinations that can be achieved by the following chaining of the EventCombiners methods.
*
* | method | first call on | subsequent calls on | provided payloads of |
* |:----------------------------|:---------------|:-----------------------|:-------------------------------------|
* | `.all()` | all events | every event | every events last received |
* | `.once().all()` | all events | never again | every events last received |
* | `.once().all().first()` | all events | never again | every events first received |
* | `.consume().all()` | all events | again all events | every events freshly received |
* | `.consume().all().first()` | all events | again all events | every events first freshly received |
* | `.some()` | any event | every event | every events last (received or not) |
* | `.once().some()` | any event | never again | only the first received |
* | `.consume().some()` | any event | every event | only the last received |
* |_____________________________|________________|________________________|______________________________________|
*
* @example
* const A = new EventHandler<string>('HandlerA');
* const B = new EventHandler<number>('HandlerB');
*
* new EventCombiner('All').all(A, B).then((events) => {
* // events has the type [DomainEvent<string>, DomainEvent<number>]
* });
* new EventCombiner('Some').some(A, B).then(...);
* new EventCombiner('OnceAll').once().all(A, B).then(...);
* new EventCombiner('OnceAllFirst').once().all(A, B).first().then(...);
* new EventCombiner('OnceSome').once().some(A, B).then(...);
* new EventCombiner('ConsumeAll').consume().all(A, B).then(...);
* new EventCombiner('ConsumeAllFirst').consume().all(A, B).first().then(...);
* new EventCombiner('ConsumeSome').consume().some(A, B).then(...);
*
* A.dispatch('foo'); // (Consume)Some + OnceSome
* A.dispatch('bar'); // (Consume)Some
* B.dispatch(420); // (Consume)Some + All
* // + OnceAll[420, bar] + OnceAllFirst[420, foo]
* // + ConsumeAll[420, bar] + ConsumeAllFirst[420, foo]
* B.dispatch(69); // (Consume)Some + All
* B.dispatch(41); // (Consume)Some + All
* A.dispatch('baz'); // (Consume)Some + All + ConsumeAll[41, baz] + ConsumeAllFirst[69, baz]
*
* @description
* #### Detailed overview
*
* Consider having `new EventCombiner('myCombiner')` as a prefix for the following method chains:
*
* ##### `.all(HANDLERS).then(CALLBACK)`
* - Calls the given CALLBACK once **all** of the given HANDLERS have dispatched.
* - After that, every time one of the HANDLERS dispatches, the CALLBACK is called again.
* - *use this if you want to react to multiple events, that all* **must** *be dispatched once*
*
* ##### `.some(HANDLERS).then(CALLBACK)`
* - Calls the given CALLBACK once **one** of the given HANDLERS has dispatched.
* - After that, every time one of the HANDLERS dispatches, the CALLBACK is called again.
* - *use this to react to multiple events without any restriction*
*
* ##### `.once().all(HANDLERS).then(CALLBACK)`
* - Calls the given CALLBACK once **all** of the given HANDLERS have dispatched.
* - After that, the CALLBACK is never called again, when one of the HANDLERS is dispatched.
* - *use this if you want to react when a whole set of events has dispatched once*
*
* ##### `.once().all(HANDLERS).first().then(CALLBACK)`
* - Calls the given CALLBACK once **all** of the given HANDLERS have dispatched.
* - After that, the CALLBACK is never called again, when one of the HANDLERS is dispatched.
* - Different to once.all just the first occurred event for every handler is provided
* - *use this if you want to react when a whole set of events has dispatched once*
*
* ##### `.once().some(HANDLERS).then(CALLBACK)`
* - Calls the given CALLBACK once **one** of the given HANDLERS has dispatched.
* - After that, the CALLBACK is never called again, when one of the HANDLERS is dispatched.
* - *use this if you want to react if a random event dispatches once*
*
* ##### `.consume().all(HANDLERS).then(CALLBACK)`
* - Calls the given CALLBACK once **all** of the given HANDLERS have dispatched.
* - After that **all** of the given HANDLERS have to be dispatched once **again** for another CALLBACK call.
* - *Use this if you want to react to a synchronously repeated occurrence of a whole set of events*
*
* ##### `.consume().all(HANDLERS)first().then(CALLBACK)`
* - Calls the given CALLBACK once **all** of the given HANDLERS have dispatched.
* - After that **all** of the given HANDLERS have to be dispatched once **again** for another CALLBACK call.
* - Different to consume.all just the first occurred event for every handler is provided
* - *Use this if you want to react to a synchronously repeated occurrence of a whole set of events*
*
* ##### `.consume().some(HANDLERS).then(CALLBACK)`
* - Calls the given CALLBACK once **one** of the given HANDLERS has dispatched.
* - After that, every time one of the HANDLERS dispatches, the CALLBACK is called again.
* - *use this to react to multiple events with the restriction, that only the last occurred event
* has a payload in the CALLBACK*
*
* #### `consume()` behavior
* "consume" can be understood as a consumption of the dispatched events when the callback is called, so
* `consume()` resets the received events. This means that the payloads provided in the CALLBACK will
* only be the ones that have been delivered since the last call of the CALLBACK. Therefore `all()`
* will have all "*fresh*" payloads and `some()` will have only one (the last) payload.
*
* Without `consume()` the CALLBACKS will always receive the respectively last payload that has been
* delivered for every corresponding event - even if the event has dispatched in a previous iteration
* of the CALLBACK.
*
* #### HANDLERS
* HANDLERS is a list of `EventHandler`s that one would normally subscribe separately to.
*
* #### CALLBACK
* The CALLBACK is a function that provides the dispatched `DomainEvent`s from the given HANDLERS as an array. The order of the DomainEvents
* in the array correlates to the order of the given HANDLERS from the `all(HANDLERS)` or `some(HANDLERS)` method.
* The DomainEvents including their payloads are also correctly typed corresponding to their HANDLERS.
*
* ##### ATTENTION:
* Since `some()` means that not all of the HANDLERS have to dispatch, not all of the provided
* DomainEvents may exist. Therefore the type is `DomainEvent<any> | 'pending'`, where 'pending' means,
* that the EVENT has not yet been dispatched. This has to be catched when working with the `.some(HANDLERS)` payloads.
*/
export class EventCombiner {
protected _name: string;
protected _eventHandlers: EventHandler<any>[] = [];
protected _receivedEvents: Map<string, DomainEvent<any> | 'pending'> = new Map();
protected _destroyed = false;
private _all: boolean = false;
private _once: boolean = false;
private _consume: boolean = false;
private _first: boolean = false;
// CONSTRUCTION + SUBSCRIPTION ___________________________________________________________________
constructor(name: string) {
this._name = name;
}
/** π¬ unsubscribes from all listened {@link EventHandler}s and marks this combiner as destroyed.
* > Further chains that end with `.then(...)` will not subscribe to the given handlers and thus
* no listening will happen, but the extendable `onLog()` method will be called with
* `type = 'alreadyDestroyed'`
*/
destroy(): void {
this.unsubscribeEventHandlers();
this._destroyed = true;
}
private unsubscribeEventHandlers(): void {
this._eventHandlers.forEach((handler) => handler.unsubscribe(this._name));
this.onLog('destroy');
}
// MODIFIERS _____________________________________________________________________________________
/** π¬ `Just the first time ...`
* > (and never again)
* #### **Chains** {@link all} & {@link some}
*/
once = (): Once => {
this._once = true;
// @ts-ignore STATIC TYPE CONVERSION
return { all: this.all, some: this.some };
};
/** π¬ `Every time ...`
* > (`consume().all()` means: always every {@link EventHandler} must dispatch again)
* #### **Chains** {@link all} & {@link some}
*/
consume = (): Consume => {
this._consume = true;
// @ts-ignore STATIC TYPE CONVERSION
return { all: this.all, some: this.some };
};
/** π¬ `Always when one of the given handlers dispatches ...`
* > (just one of the given {@link EventHandler} has dispatched a {@link DomainEvent})
* #### **Chains** {@link then}
*/
some = <Handlers extends EventHandler<any>[]>(...handlers: [...Handlers]): SomeThen<Handlers> => {
this._eventHandlers = handlers;
// @ts-ignore STATIC TYPE CONVERSION
return { then: this.then };
};
/** π¬ `When all given handlers dispatch (and then on every event) ...`
* > (every given {@link EventHandler} has dispatched at least one {@link DomainEvent})
* #### **Chains** {@link then} & {@link first}
*/
all = <Handlers extends EventHandler<any>[]>(...handlers: [...Handlers]): AllThen<Handlers> => {
this._eventHandlers = handlers;
this._all = true;
// @ts-ignore STATIC TYPE CONVERSION
return { then: this.then, first: this.first };
};
/** π¬ `... Just take the first occurred events from each and ...`
* > (if an event already occurred, then it will not be overwritten by following events of the same
* type until the `then()` is called and the events are consumed)
* #### **Chains** {@link then}
*/
protected first = <Handlers extends EventHandler<any>[]>(): AllThen<Handlers> => {
this._first = this._consume || this._once; // otherwise first is not allowed and should not work
// TS users will not see this method, but JS users can force using this with combiner.all().first().then(...)
// @ts-ignore STATIC TYPE CONVERSION
return { then: this.then };
};
// THEN __________________________________________________________________________________________
/** π¬ `... Call the given callback.`
* > A list of all possible {@link DomainEvent}s is passed to the callback,
* which corresponds to the order of the defined {@link EventHandler}s. *
* #### Attention
* When this method is chained with a `some(...)` method, the {@link DomainEvent}s may also be `'pending'` for those {@link EventHandler}s which have not dispatched yet.
*/
protected then = (callback: (events: (DomainEvent<any> | 'pending')[]) => void): void => {
this._destroyed && this.onLog('alreadyDestroyed');
this._eventHandlers?.forEach((handler) => {
// set empty events
this._receivedEvents.set(handler.name, 'pending');
// set events when dispatched
handler.subscribe(
this._name,
(event) => {
if (!(this._first && this._receivedEvents.get(handler.name) !== 'pending')) {
this._receivedEvents.set(handler.name, event);
}
if (this.thenIsFulfilled()) {
this.onLog('then');
const events = [...this._receivedEvents.values()];
callback(events);
if (this._once) {
this.unsubscribeEventHandlers();
}
if (this._consume) {
this.resetReceivedEvents();
}
}
},
true
);
});
};
private thenIsFulfilled(): boolean {
for (const event of this._receivedEvents.values()) {
if (this._all && event === 'pending') {
return false;
}
if (!this._all && event !== 'pending') {
return true;
}
}
return this._all; // === all & no event pending || !all (some) & every event pending
}
private resetReceivedEvents(): void {
[...this._receivedEvents.keys()].forEach((handlerName) => {
this._receivedEvents.set(handlerName, 'pending');
});
}
/** π¬ Inject Logging (by extending this class)
* > This method is called when the `.then(...)` callback is called, the combiner is destroyed,
* or the callback is called, when already destroyed. The original method will do nothing.
* To utilize this method (eg. for a Logger integration) this class must be extended: *
* @example
* class MyEventCombiner extends EventCombiner {
* protected onLog(type: 'then' | 'destroy' | 'alreadyDestroyed'): void {
* console.log(type, this._name, this._receivedEvents, this._eventHandlers);
* }
* }
*/
protected onLog(type: 'then' | 'destroy' | 'alreadyDestroyed'): void {
// extend this class to implement this function in order to inject logging
}
}