-
Notifications
You must be signed in to change notification settings - Fork 0
EventCombiner
Combines multiple events to let Subscribers listen to any combination of the events. There are 6 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 |
.some() | any event | every event | every events last (received or not) |
.once().all() | all events | never again | every events last received |
.once().some() | any event | never again | only the first received |
.consume().all() | all events | again all events | every events freshly received |
.consume().some() | any event | every event | only the last received |
Consider having new EventCombiner('myCombiner')
as a prefix for the following method chains:
- Calls the given CALLBACK once all of the given HANDLERS have fired.
- After that, every time one of the HANDLERS fires, the CALLBACK is called again.
- use this if you want to react to multiple events, that all must be fired once
- Calls the given CALLBACK once one of the given HANDLERS has fired.
- After that, every time one of the HANDLERS fires, the CALLBACK is called again.
- use this to react to multiple events without any restriction
- Calls the given CALLBACK once all of the given HANDLERS have fired.
- After that, the CALLBACK is never called again, when one of the HANDLERS is fired.
- use this if you want to react when a whole set of events has fired once
- Calls the given CALLBACK once one of the given HANDLERS has fired.
- After that, the CALLBACK is never called again, when one of the HANDLERS is fired.
- use this if you want to react if a random event fires once
- Calls the given CALLBACK once all of the given HANDLERS have fired.
- After that all of the given HANDLERS have to be fired once again for another CALLBACK call.
- Use this if you want to react to a synchronously repeated occurrence of a whole set of events
- Calls the given CALLBACK once one of the given HANDLERS has fired.
- After that, every time one of the HANDLERS fires, 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" can be understood as a consumption of the fired 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 fired in a previous iteration
of the CALLBACK.
HANDLERS is a list of EventHandler
s that one would normally subscribe separately to.
The CALLBACK is a function that provides the fired 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.
Since some()
means that not all of the HANDLERS have to fire, 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 fired. This has to be catched when working with the .some(HANDLERS)
payloads.
• new EventCombiner(name
)
Name | Type |
---|---|
name |
string |
• Private
_all: Partial
<boolean
> = true
• Private
_consume: boolean
= false
• Protected
_eventHandlers: EventHandler
<any
>[] = []
• Protected
_name: string
• Private
_once: boolean
= false
• Protected
_receivedEvents: Map
<string
, DomainEvent
<any
> | "pending"
>
▸ all<EH
>(...eventHandler
): AllThenType
<EH
>
When all given events occur ...
Name | Type |
---|---|
EH |
extends EventHandler <any >[] |
Name | Type |
---|---|
...eventHandler |
[...EH[]] |
AllThenType
<EH
>
▸ consume(): Object
Every time ...
Object
Name | Type |
---|---|
all |
<EH>(...eventHandler : [...EH[]]) => AllThenType <EH > |
some |
<EH>(...eventHandler : [...EH[]]) => SomeThenType <EH > |
▸ Protected
logDispatch(): void
void
▸ once(): Object
Just the first time ...
Object
Name | Type |
---|---|
all |
<EH>(...eventHandler : [...EH[]]) => AllThenType <EH > |
some |
<EH>(...eventHandler : [...EH[]]) => SomeThenType <EH > |
▸ Private
resetReceivedEvents(): void
void
▸ some<EH
>(...eventHandler
): SomeThenType
<EH
>
When one of the given events occurs ...
Name | Type |
---|---|
EH |
extends EventHandler <any >[] |
Name | Type |
---|---|
...eventHandler |
[...EH[]] |
SomeThenType
<EH
>
▸ Private
then(callback
): void
Name | Type |
---|---|
callback |
(events : (DomainEvent <any > | "pending" )[]) => void
|
void
▸ Private
thenIsFulfilled(all
): boolean
Name | Type |
---|---|
all |
boolean |
boolean
▸ Private
unsubscribeEventHandlers(): void
void