@superhero/eventflow-spoke 4.5.3 → 4.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/consume.js +26 -13
- package/package.json +1 -1
package/consume.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import Log from '@superhero/log'
|
|
2
|
+
|
|
1
3
|
export function locate(locator)
|
|
2
4
|
{
|
|
3
5
|
const
|
|
@@ -32,6 +34,7 @@ export default class ConsumeService
|
|
|
32
34
|
#locator
|
|
33
35
|
#spoke
|
|
34
36
|
#lookupConsumerMap = new Map
|
|
37
|
+
log = new Log({ label: '[EVENTFLOW:SPOKE:CONSUME]' })
|
|
35
38
|
|
|
36
39
|
constructor(locator, spoke)
|
|
37
40
|
{
|
|
@@ -59,18 +62,19 @@ export default class ConsumeService
|
|
|
59
62
|
|
|
60
63
|
async #consumer(service, event)
|
|
61
64
|
{
|
|
62
|
-
const consumer = this.#
|
|
65
|
+
const consumer = this.#lazyloadConsumer(service, event)
|
|
63
66
|
|
|
64
67
|
try
|
|
65
68
|
{
|
|
66
|
-
await
|
|
69
|
+
await consumer(event)
|
|
67
70
|
}
|
|
68
71
|
catch(reason)
|
|
69
72
|
{
|
|
70
|
-
const error
|
|
71
|
-
error.code
|
|
72
|
-
error.cause
|
|
73
|
-
error.
|
|
73
|
+
const error = new Error('consumer failed to handle event')
|
|
74
|
+
error.code = 'E_EVENTFLOW_SPOKE_CONSUMER_FAILED'
|
|
75
|
+
error.cause = reason
|
|
76
|
+
error.event = { ...event }
|
|
77
|
+
delete error.event.data
|
|
74
78
|
|
|
75
79
|
if('function' === typeof service.onError)
|
|
76
80
|
{
|
|
@@ -93,16 +97,20 @@ export default class ConsumeService
|
|
|
93
97
|
}
|
|
94
98
|
}
|
|
95
99
|
|
|
96
|
-
#
|
|
100
|
+
#lazyloadConsumer(service, event)
|
|
97
101
|
{
|
|
98
|
-
|
|
102
|
+
const lookupKey = event.domain + '.' + event.name
|
|
103
|
+
|
|
104
|
+
if(false === this.#lookupConsumerMap.has(lookupKey))
|
|
99
105
|
{
|
|
100
|
-
|
|
106
|
+
const consumerName = this.#composeConsumerName(event.name)
|
|
107
|
+
this.#lookupConsumerMap.set(lookupKey,
|
|
108
|
+
consumerName in service
|
|
109
|
+
? service[consumerName].bind(service)
|
|
110
|
+
: this.#fallbackConsumerCallback.bind(this, service, consumerName))
|
|
101
111
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
this.#lookupConsumerMap.set(eventName, consumer)
|
|
105
|
-
return consumer
|
|
112
|
+
|
|
113
|
+
return this.#lookupConsumerMap.get(lookupKey)
|
|
106
114
|
}
|
|
107
115
|
|
|
108
116
|
#composeConsumerName(eventName)
|
|
@@ -116,4 +124,9 @@ export default class ConsumeService
|
|
|
116
124
|
|
|
117
125
|
return observerName
|
|
118
126
|
}
|
|
127
|
+
|
|
128
|
+
#fallbackConsumerCallback(service, consumerName, event)
|
|
129
|
+
{
|
|
130
|
+
this.log.warn`consumer missing ${event.domain} › ${event.name} not found in service ${service.constructor.name} › ${consumerName}`
|
|
131
|
+
}
|
|
119
132
|
}
|