<commit_before>// round-robin.js

var _ = require('underscore');
var noflo = require('noflo');

var basenode = require('./base-node');

exports.getComponent = function() {
    return _.extend(new noflo.Component({
        outPorts: {
            out: {
                description: "Only one socket will receive each group of data packets",
                datatype: 'all'
            }
        },
        inPorts: {
            'in': {
                description: "Each input group is send to exactly one output socket",
                datatype: 'all',
                required: true,
                process: basenode.on({
                    begingroup: begingroup,
                    data: send,
                    endgroup: endgroup
                })
            }
        }
    }), {
        description: "Every group of data packet will be sent to at most one out-port socket",
        icon: 'refresh'
    });
};

function begingroup(group){
    this.groupPort = nextSocket(this);
    onceConnected(this.groupPort, function(socket){
        socket.beginGroup(group);
    });
}

function send(data) {
    var disconnect = !this.groupPort;
    onceConnected(this.groupPort || nextSocket(this), function(socket){
        socket.send(data);
        if (disconnect) {
            socket.disconnect();
        }
    });
}

function endgroup() {
    onceConnected(this.groupPort, function(socket){
        socket.endGroup();
        socket.disconnect();
    });
    this.groupPort = null;
}

function onceConnected(socket, callback) {
    if (!socket) return;
    else if (socket.isConnected()) return callback(socket);
    socket.once('connect', callback.bind(this, socket));
    socket.connect();
}

function nextSocket(self) {
    var sockets = self.outPorts.out.sockets;
    self.socketId = (_.isFinite(self.socketId) ? 1 + self.socketId : 0) % sockets.length;
    return sockets[self.socketId];
}<commit_msg>Remove group support in round-robin (parallel and nested groups weren't supported anyway)<commit_after>// round-robin.js

var _ = require('underscore');
var noflo = require('noflo');

var basenode = require('./base-node');

exports.getComponent = function() {
    return _.extend(new noflo.Component({
        outPorts: {
            out: {
                description: "Only one socket will receive each data packet",
                datatype: 'all'
            }
        },
        inPorts: {
            'in': {
                description: "Each input group is send to exactly one output socket",
                addressable: true,
                datatype: 'all',
                required: true,
                process: basenode.on({data: send})
            }
        }
    }), {
        description: "Every data packet will be sent to at most one out-port socket",
        icon: 'refresh'
    });
};

function send(data, socketIndex) {
    nextSocket(this).then(function(socket){
        socket.send(data);
        socket.disconnect();
    });
}

function nextSocket(self) {
    var sockets = self.outPorts.out.sockets;
    self.socketId = (_.isFinite(self.socketId) ? ++self.socketId : 0) % sockets.length;
    var socket = sockets[self.socketId];
    if (socket) return Promise.resolve(socket);
    else return Promise.reject(Error("No attached output sockets"));
}
{'fraction_non_alphanumeric': 0.1227217496962333, 'fraction_numerical': 0.0009113001215066828, 'mean_word_length': 2.145176695319962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4520819', 'n_tokens_mistral': 949, 'n_tokens_neox': 911, 'n_words': 232}