Refactor lib/flows code to include initial router component

This commit is contained in:
Nick O'Leary
2020-07-20 16:48:47 +01:00
parent 952c9d8bdb
commit d57ec0cd53
18 changed files with 147 additions and 115 deletions

View File

@@ -0,0 +1,50 @@
var settings;
const LocalRouter = require("./localRouter");
var defaultRouter;
class Router {
constructor(stack) {
this.stack = stack || [];
}
send(source,destinationId,msg) {
var pos = 0;
var next = () => {
var router = this.stack[pos++];
if (router) {
router.send(source,destinationId,msg,next);
}
}
next();
}
}
function init(runtime) {
settings = runtime.settings;
defaultRouter = new Router([
new LocalRouter(),
new PostMessageLogger()
])
}
function send(source,destinationId,msg) {
defaultRouter.send(source,destinationId,msg);
}
module.exports = {
init:init,
send: send
}
class PostMessageLogger {
constructor() {}
send(source,destinationId,msg,next) {
console.log(source.id.padEnd(16),"->",destinationId.padEnd(16),JSON.stringify(msg));
}
}

View File

@@ -0,0 +1,17 @@
class LocalRouter {
constructor() {}
send(source,destinationId,msg,next) {
var node = source._flow.getNode(destinationId);
if (node) {
setImmediate(function() {
node.receive(msg);
next();
});
} else if (next) {
next()
}
}
}
module.exports = LocalRouter