Which of these approaches should be used to wait for all ports to enter before sending packets?

I am still learning a lot about how FBP and NoFlo work in terms of the process, so although I can hack something together that does this work, I'm not sure if I'm asking problems elsewhere.

In this case, I create a component that waits for input on each of its 2 ports before sending data to the output port. What for? I need data from both inputs to create an output package (group name and input package).

I managed to achieve this using 2 approaches, and I would like to know which approach is better or at least the upper / lower level for each approach.

The component I'm talking about is vizicities / Group:

enter image description here

Approach 1: Queue packets from input1 internally until input 2 receives a packet

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 }; 

Approach 2: Using the WirePattern Helper

 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; }; 

Both approaches seem to work, although obviously there should be a reason to use one over the other. Can anyone clarify this for me?

+6
source share
1 answer

WirePattern is the recommended method in modern NoFlo, as it gives you extra control over packet synchronization when you need it. You can run when all the necessary input receives data, or you can require strict group matching (or even a packet payload).

Many common NoFlo components are still waiting for WirePattern-ization, but for something new, much less for something asynchronous, this is the recommended way.

+3
source

Source: https://habr.com/ru/post/981599/


All Articles