Refactor to a connector based concept in lib/link

Remove connection and client subclasses of Link and put the connection
logic into a "connector" class.  The connector deals with sending
and receiving messages, handling the connection and reconnection of lost
connections and giving sequence numbers to messages.

This move the logic of the actual connection away from the link
sub-classes, which in turn means the connection should be a lot easier
to mock in tests.
This commit is contained in:
Hornwitser committed 2019-11-30 00:08:10 +01:00
1 parent a3eb39712c
commit 3bccbac496
12 files changed
+492 -494

No files matched your search

+21 -15
View File
@@ -253,20 +253,9 @@ commands = new Map([...commands.map(command => [command.name, command])]);
/**
* Handles running the control
*
* Connects to the master server over the socket.io connection and sends
* commands to it.
* Connector for control connection to master server
*/
class Control extends link.Client {
// I don't like God classes, but the alternative of putting all this state
// into global variables is not much better.
constructor(controlConfig) {
super('control', controlConfig.url, controlConfig.token);
link.attachAllMessages(this);
}
class ControlConnector extends link.SocketIOClientConnector {
register() {
console.log("SOCKET | registering control");
this.send('register_control', {
@@ -274,6 +263,22 @@ class Control extends link.Client {
version: version,
});
}
}
/**
* Handles running the control
*
* Connects to the master server over the socket.io connection and sends
* commands to it.
*/
class Control extends link.Link {
// I don't like God classes, but the alternative of putting all this state
// into global variables is not much better.
constructor(connector) {
super('control', 'master', connector);
link.attachAllMessages(this);
}
async instanceOutputEventHandler(message) {
let { instance_id, output } = message.data;
@@ -395,8 +400,9 @@ async function startControl() {
}
}
let control = new Control(controlConfig);
await control.connect();
let controlConnector = new ControlConnector(controlConfig.url, controlConfig.token);
let control = new Control(controlConnector);
await controlConnector.connect();
if (commands.has(commandName)) {
command = commands.get(commandName);