Implement forwarding to instances for events

Implement forward to instance for Event messages.
This commit is contained in:
Hornwitser committed 2019-12-14 17:31:02 +01:00
1 parent e5115df28b
commit 513651393b
3 files changed
+49 -6

No files matched your search

+15 -6
View File
@@ -311,8 +311,9 @@ class Event {
* Links this event is valid on. Takes an array of strings
* containing "<source>-<target>" specifications.
* @param {?string} forwardTo -
* Optional target to forward this request to. Only 'master' is
* currently supported.
* Optional target to forward this request to. Supported values are
* 'master' and 'instance'. If set to 'instance' an instance_id
* property is implicity added to eventProperties.
* @param {?string} broadcastTo -
* Optional target to broadcast this event to. Only 'instance' is
* currently supported. An event will only be broadcast towards
@@ -329,7 +330,12 @@ class Event {
this.forwardTo = forwardTo;
this.broadcastTo = broadcastTo;
if (forwardTo !== 'master' && forwardTo !== null) {
if (forwardTo === 'instance') {
eventProperties = Object.assign(
{"instance_id": { type: "integer" }}, eventProperties
);
} else if (forwardTo !== 'master' && forwardTo !== null) {
throw new Error(`Invalid forwardTo value ${forwardTo}`);
}
@@ -358,10 +364,11 @@ class Event {
* for the event.
*
* If forwardTo was set to 'master' the `forwardEventToMaster` method on
* the link is used as the default handler.
* the link is used as the default handler. If it was set to 'instance'
* the `forwardEventToInstance` method on the link is used by default.
*
* If broadcastTo was set to 'instance' the `broadcastEventToInstance`
* method on the link is invoked before the handler. BUT HOW?
* method on the link is invoked before the handler.
*
* Does nothing if the link is not a target for this event.
*
@@ -376,7 +383,9 @@ class Event {
if (this.links.includes(`${link.target}-${link.source}`)) {
// Use forwarder if handler is not present.
if (!handler) {
if (this.forwardTo === 'master') {
if (this.forwardTo === 'instance') {
handler = link.forwardEventToInstance;
} else if (this.forwardTo === 'master') {
handler = link.forwardEventToMaster;
}
}