Move events and requests into messages

Merge the events and requests categories in lib/link into messages and
use the class name of the message to determine which handler to call.

Also swaps the order of the arguments for forwardInstanceRequest to be
consistent with the rest of the lib/link code and fixes up the docs for
requests and events.
This commit is contained in:
Hornwitser committed 2019-11-30 00:08:10 +01:00
1 parent 278dc9b2d2
commit 039b41d3e6
5 files changed
+68 -57

No files matched your search

+29 -31
View File
@@ -182,11 +182,15 @@ class InstanceRequest {
*/
attach(link, handler) {
this._masterRequest.attach(link, async (message) => {
return await link.forwardInstanceRequest(message, this);
});
this._slaveRequest.attach(link, async (message) => {
return await link.forwardInstanceRequest(message, handler);
return await link.forwardInstanceRequest(this, message);
});
if (handler) {
this._slaveRequest.attach(link, async (message) => {
return await link.forwardInstanceRequest(handler, message);
});
} else {
this._slaveRequest.attach(link);
}
}
/**
@@ -215,10 +219,10 @@ class InstanceRequest {
}
}
let requests = {}
let messages = {}
// Management requests
requests.listSlaves = new Request({
messages.listSlaves = new Request({
type: 'list_slaves',
sources: ['control'],
targets: ['master'],
@@ -240,7 +244,7 @@ requests.listSlaves = new Request({
},
});
requests.listInstances = new Request({
messages.listInstances = new Request({
type: 'list_instances',
sources: ['control'],
targets: ['master'],
@@ -260,7 +264,7 @@ requests.listInstances = new Request({
},
});
requests.setInstanceOutputSubscriptions = new Request({
messages.setInstanceOutputSubscriptions = new Request({
type: 'set_instance_output_subscriptions',
sources: ['control'],
targets: ['master'],
@@ -272,7 +276,7 @@ requests.setInstanceOutputSubscriptions = new Request({
},
});
requests.createInstanceCommand = new Request({
messages.createInstanceCommand = new Request({
type: 'create_instance',
sources: ['control'],
targets: ['master'],
@@ -282,7 +286,7 @@ requests.createInstanceCommand = new Request({
},
});
requests.createInstance = new Request({
messages.createInstance = new Request({
type: 'create_instance',
sources: ['master'],
targets: ['slave'],
@@ -310,27 +314,27 @@ requests.createInstance = new Request({
});
requests.createSave = new InstanceRequest({
messages.createSave = new InstanceRequest({
type: 'create_save',
sources: ['control'],
});
requests.startInstance = new InstanceRequest({
messages.startInstance = new InstanceRequest({
type: 'start_instance',
sources: ['control'],
});
requests.stopInstance = new InstanceRequest({
messages.stopInstance = new InstanceRequest({
type: 'stop_instance',
sources: ['control'],
});
requests.deleteInstance = new InstanceRequest({
messages.deleteInstance = new InstanceRequest({
type: 'delete_instance',
sources: ['control'],
});
requests.sendRcon = new InstanceRequest({
messages.sendRcon = new InstanceRequest({
type: 'send_rcon',
sources: ['control'],
requestProperties: {
@@ -388,8 +392,7 @@ class Event {
}
}
let events = {};
events.updateInstances = new Event({
messages.updateInstances = new Event({
type: 'update_instances',
sources: ['slave'],
targets: ['master'],
@@ -409,7 +412,7 @@ events.updateInstances = new Event({
},
});
events.instanceOutput = new Event({
messages.instanceOutput = new Event({
type: 'instance_output',
sources: ['slave', 'master'],
targets: ['master', 'control'],
@@ -438,22 +441,18 @@ events.instanceOutput = new Event({
/**
* Attaches all requests and events to the given link
*
* Loops through all requests and events defined and attaches all of
* them to the link. For requests the handler used is looked up on the
* link instance as the name of the request + 'RequestHandler'. For
* events the the handler used is looked up on the link instance as the
* name of the event + 'EventHandler'.
* Loops through all builtin messages defined and attaches all of them
* to the link. The handler used for the messageis looked up on the
* link instance as the concatenation of the name of the message, the
* name of message class, and 'Handler'.
*/
function attachAllMessages(link) {
for (let [name, request] of Object.entries(requests)) {
request.attach(link, link[name + 'RequestHandler']);
}
for (let [name, event] of Object.entries(events)) {
event.attach(link, link[name + 'EventHandler']);
for (let [name, message] of Object.entries(messages)) {
message.attach(link, link[name + message.constructor.name + 'Handler']);
}
}
module.exports = {
Request,
InstanceRequest,
@@ -461,6 +460,5 @@ module.exports = {
attachAllMessages,
requests,
events,
messages,
}