@@ -16,17 +16,21 @@ const event_handler_types: Array<ArtifactType> = [
16
16
* @param timeout lease expiration time (in ms) when polling the store
17
17
* @param limit max number of events to drain in each try
18
18
* @param delay debounce delay (in ms) to drain
19
+ * @param subscribed to subscribe the broker to commit events - set to false when serverless
19
20
*/
20
- export const InMemoryBroker = ( {
21
- timeout,
22
- limit,
23
- delay
24
- } : {
25
- timeout : number ;
26
- limit : number ;
27
- delay : number ;
21
+ export const InMemoryBroker = ( options ?: {
22
+ timeout ?: number ;
23
+ limit ?: number ;
24
+ delay ?: number ;
25
+ subscribed ?: boolean ;
28
26
} ) : Broker => {
29
27
const name = "InMemoryBroker" ;
28
+ const {
29
+ timeout = 5000 ,
30
+ limit = 10 ,
31
+ delay = 500 ,
32
+ subscribed = true
33
+ } = options ?? { } ;
30
34
31
35
// connect private event handlers only
32
36
// NOTE: public consumers should be connected by an external broker service
@@ -58,34 +62,35 @@ export const InMemoryBroker = ({
58
62
const __drain = throttle ( drainAll , delay ) ;
59
63
60
64
// subscribe broker to commit events
61
- app ( ) . on ( "commit" , async ( { factory, snapshot } ) => {
62
- // commits STATE_EVENT - artifact must be configured in app builder
63
- if ( snapshot ) {
64
- const commit = app ( ) . commits . get ( factory . name ) ;
65
- if ( commit && commit ( snapshot ) ) {
66
- try {
67
- const { id, stream, name, metadata, version } = snapshot . event ! ;
68
- return await store ( ) . commit (
69
- stream ,
70
- [
65
+ subscribed &&
66
+ app ( ) . on ( "commit" , async ( { factory, snapshot } ) => {
67
+ // commits STATE_EVENT - artifact must be configured in app builder
68
+ if ( snapshot ) {
69
+ const commit = app ( ) . commits . get ( factory . name ) ;
70
+ if ( commit && commit ( snapshot ) ) {
71
+ try {
72
+ const { id, stream, name, metadata, version } = snapshot . event ! ;
73
+ return await store ( ) . commit (
74
+ stream ,
75
+ [
76
+ {
77
+ name : STATE_EVENT ,
78
+ data : snapshot . state
79
+ }
80
+ ] ,
71
81
{
72
- name : STATE_EVENT ,
73
- data : snapshot . state
74
- }
75
- ] ,
76
- {
77
- correlation : metadata . correlation ,
78
- causation : { event : { id, name, stream } }
79
- } ,
80
- version // IMPORTANT! - state events should be committed right after the snapshot's event
81
- ) ;
82
- } catch ( error ) {
83
- log ( ) . error ( error ) ;
82
+ correlation : metadata . correlation ,
83
+ causation : { event : { id, name, stream } }
84
+ } ,
85
+ version // IMPORTANT! - state events should be committed right after the snapshot's event
86
+ ) ;
87
+ } catch ( error ) {
88
+ log ( ) . error ( error ) ;
89
+ }
84
90
}
85
91
}
86
- }
87
- __drain ( ) ;
88
- } ) ;
92
+ __drain ( ) ;
93
+ } ) ;
89
94
90
95
return {
91
96
name,
0 commit comments