从流程的角度来看,我仍在学习很多关于 FBP 和 NoFlo 如何工作的知识,所以虽然我可以一起破解一些可以完成这项工作的东西,但我不确定我是否会在其他地方引入问题。
在这种情况下,我正在创建一个组件,该组件在将数据发送到输出端口之前等待其 2 个端口中的每个端口的输入。为什么?我需要来自两个输入的数据来构造输出数据包(组名和输入数据包)。
我已经设法使用两种方法来实现这一点,我想知道哪种方法更好,或者至少每种方法的优缺点。
我在这里谈论的组件是 vizicities/Group:
方法 1:在内部对来自 input1 的数据包进行排队,直到 input2 收到数据包
var noflo = require("noflo");
exports.getComponent = function() {
var AddGroup = new noflo.Component();
AddGroup.description = "This component adds a group to the data packets";
var packets = [];
var groupName = "";
// Register ports and event handlers
AddGroup.inPorts.add("in", { datatype: "all" }, function(event, payload) {
switch (event) {
case "begingroup":
AddGroup.outPorts.out.beginGroup(payload);
break;
case "endgroup":
AddGroup.outPorts.out.endGroup();
break;
case "data":
// Queue packet
packets.push(payload);
break;
case "disconnect":
// Only send output if group has been set
if (groupName) {
for (var i = 0; i < packets.length; i++) {
AddGroup.outPorts.out.beginGroup(groupName);
AddGroup.outPorts.out.send(packets);
AddGroup.outPorts.out.endGroup();
}
// Disconnect output port when input port disconnects
AddGroup.outPorts.out.disconnect();
}
break;
}
});
AddGroup.inPorts.add("group", { datatype: "string" }, function(event, payload) {
switch (event) {
case "begingroup":
break;
case "endgroup":
break;
case "data":
groupName = payload;
break;
case "disconnect":
// TODO: Does this dupe anything with the same logic on the in port?
// Only send output if group has been set
if (groupName) {
for (var i = 0; i < packets.length; i++) {
AddGroup.outPorts.out.beginGroup(groupName);
AddGroup.outPorts.out.send(packets);
AddGroup.outPorts.out.endGroup();
}
// Disconnect output port when input port disconnects
AddGroup.outPorts.out.disconnect();
}
break;
}
});
AddGroup.outPorts.add("out", { datatype: "all" });
return AddGroup; // Return new instance
};
方法 2:使用 WirePattern 助手
var noflo = require("noflo");
exports.getComponent = function() {
var AddGroup = new noflo.Component();
AddGroup.description = "This component adds a group to the data packets";
var config = {
in: ["in", "group"],
out: "out"
};
AddGroup.inPorts = new noflo.InPorts({
in: {
datatype: "string",
required: true
},
group: {
datatype: "string",
required: true
}
});
AddGroup.outPorts = new noflo.OutPorts({
out: {
datatype: "all"
}
});
noflo.helpers.WirePattern(AddGroup, config, function(data, groups, outPort) {
outPort.beginGroup(data.group);
outPort.send(data.in);
outPort.endGroup();
});
// Return new instance
return AddGroup;
};
这两种方法似乎都有效,但显然必须有理由使用其中一种。有人可以为我澄清一下吗?