我仍然从流程的角度学习很多关于FBP和NoFlo是如何工作的,所以虽然我可以在一起工作,但我不确定我是否在其他地方引入了问题。
在这种情况下,我正在创建一个组件,在将数据发送到out端口之前等待其2个端口中的每个端口上的输入。为什么?我需要来自两个输入的数据来构造输出数据包(组名和输入数据包)。
我已经设法使用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;
};
这两种方法似乎都有效,但显然必须有一种方法可以使用其中一种方法。有人可以为我澄清一下吗?
答案 0 :(得分:3)
WirePattern是现代NoFlo中推荐的方式,因为它在需要时为您提供了对数据包同步的额外控制。您可以在所有必需的输入获取数据时触发,或者您可以要求严格的组(甚至数据包有效负载)匹配。
许多常见的NoFlo组件仍在等待WirePattern化,但对于任何新的东西,甚至更多的异步,它都是推荐的方式。