1
1
'use strict'
2
2
3
- import { workerData , BroadcastChannel } from 'worker_threads'
3
+ import { workerData } from 'worker_threads'
4
4
import domainEvents from './domain-events'
5
+
5
6
const { portEvent } = domainEvents
6
7
7
8
/**
@@ -18,8 +19,10 @@ export class PortEventRouter {
18
19
this . models = models
19
20
this . broker = broker
20
21
this . _localSpec = this . models . getModelSpec ( workerData . poolName )
22
+
21
23
if ( ! this . _localSpec )
22
24
this . models . getModelSpecs ( ) . find ( s => s . domain === workerData . poolName )
25
+
23
26
this . _threadRemotePorts = this . threadRemotePorts ( )
24
27
this . _threadLocalPorts = this . threadLocalPorts ( )
25
28
}
@@ -106,35 +109,17 @@ export class PortEventRouter {
106
109
)
107
110
}
108
111
109
- handleBroadcastEvent ( msg ) {
110
- if ( msg ?. data ?. eventName )
111
- this . broker . notify ( this . internalizeEvent ( msg . data . eventName ) , msg . data )
112
- else {
113
- console . log ( 'missing eventName' , msg . data )
114
- this . broker . notify ( 'missingEventName' , msg . data )
115
- }
116
- }
117
-
118
- externalizeEvent ( event ) {
112
+ externalizeEvent ( event , route = 'broadcast' ) {
119
113
if ( typeof event === 'object' ) {
120
114
return {
121
115
...event ,
122
- eventName : portEvent ( event . eventName )
116
+ eventName : portEvent ( event . eventName ) ,
117
+ route
123
118
}
124
119
}
125
120
if ( typeof event === 'string' ) return portEvent ( event )
126
121
}
127
122
128
- internalizeEvent ( event ) {
129
- if ( typeof event === 'object' ) {
130
- return {
131
- ...event ,
132
- eventName : event . eventName . replace ( portEvent ( ) , '' )
133
- }
134
- }
135
- if ( typeof event === 'string' ) return event . replace ( portEvent ( ) , '' )
136
- }
137
-
138
123
/**
139
124
* Listen for producer events coming from other thread pools and
140
125
* fire the events in the local pool for ports to consume them.
@@ -143,56 +128,33 @@ export class PortEventRouter {
143
128
* dispatch them to pools that consume them.
144
129
*/
145
130
listen ( ) {
146
- const services = new Set ( )
147
- const channels = new Map ( )
148
131
const subscriberPorts = this . subscriberPorts ( )
149
132
const publisherPorts = this . publisherPorts ( )
150
133
const unhandledPorts = this . unhandledPorts ( )
151
134
152
- publisherPorts . forEach ( port => services . add ( port . domain ) )
153
- subscriberPorts . forEach ( port => services . add ( port . domain ) )
154
-
155
- services . forEach ( service =>
156
- channels . set ( service , new BroadcastChannel ( service ) )
157
- )
158
-
159
135
console . log ( 'publisherPorts' , publisherPorts )
160
136
console . log ( 'subscriberPorts' , subscriberPorts )
161
137
console . log ( 'unhandledPorts' , unhandledPorts )
162
- console . log ( 'channels' , channels )
163
138
164
139
// listen for producer events and dispatch them to local pools
165
140
publisherPorts . forEach ( port =>
166
141
this . broker . on ( port . producesEvent , event =>
167
- channels
168
- . get ( port . domain )
169
- . postMessage ( JSON . parse ( JSON . stringify ( this . externalizeEvent ( event ) ) ) )
142
+ this . broker . notify ( 'broadcast' , this . externalizeEvent ( event ) )
170
143
)
171
144
)
172
145
173
146
// listen for incoming consumer events from local pools
174
- subscriberPorts . forEach ( port => {
175
- channels . get ( port . domain ) . onmessage = msg =>
176
- this . handleBroadcastEvent ( msg )
177
- } )
147
+ subscriberPorts . forEach ( port =>
148
+ this . broker . on ( this . externalizeEvent ( port . consumesEvent ) , event =>
149
+ this . notify ( port . consumesEvent , event )
150
+ )
151
+ )
178
152
179
153
// dispatch events not handled by local pools to the service mesh
180
- unhandledPorts . forEach ( port => {
181
- this . broker . on ( port . producesEvent , event => {
182
- this . broker . notify ( 'to_main' , {
183
- ...event ,
184
- eventName : this . externalizeEvent ( port . producesEvent ) ,
185
- route : 'balanceEventConsumer' // mesh routing algo
186
- } )
187
- } )
188
- } )
189
-
190
- if ( ! channels . has ( this . localSpec . domain ) ) {
191
- // listen to this model's channel
192
- new BroadcastChannel ( this . localSpec . domain ) . onmessage = msg => {
193
- console . log ( 'onmessage' , msg . data )
194
- this . handleBroadcastEvent ( msg )
195
- }
196
- }
154
+ unhandledPorts . forEach ( port =>
155
+ this . broker . on ( port . producesEvent , event =>
156
+ this . broker . notify ( 'to_main' , this . externalizeEvent ( event ) )
157
+ )
158
+ )
197
159
}
198
160
}
0 commit comments