@openhps/core
Version:
Open Hybrid Positioning System - Core component
68 lines • 2.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BalanceNode = void 0;
const PushPromise_1 = require("../../graph/PushPromise");
const Node_1 = require("../../Node");
/**
* @category Flow shape
*/
class BalanceNode extends Node_1.Node {
constructor() {
super(...arguments);
this._busyNodes = [];
this._queue = [];
}
push(frame) {
return new PushPromise_1.PushPromise((resolve, reject) => {
this.logger('debug', `Received a data frame in the balance node ${this.uid}`, frame);
let assigned = false;
for (const outlet of this.outlets) {
if (this._busyNodes.indexOf(outlet) === -1) {
// Node is not busy - perform push
this._busyNodes.push(outlet);
assigned = true;
outlet
.push(frame)
.then(() => {
this._busyNodes.splice(this._busyNodes.indexOf(outlet), 1);
this._updateQueue();
resolve();
})
.catch((ex) => {
this._updateQueue();
reject(ex);
});
break; // Stop Assigning
}
}
if (!assigned) {
// Add to queue
this._queue.push({ frame, resolve, reject });
}
});
}
_updateQueue() {
if (this._queue.length !== 0) {
for (const outlet of this.outlets) {
if (this._busyNodes.indexOf(outlet) === -1) {
// Node is not busy - perform push
const queue = this._queue.pop();
outlet
.push(queue.frame)
.then(() => {
this._busyNodes.splice(this._busyNodes.indexOf(outlet), 1);
this._updateQueue();
queue.resolve();
})
.catch((ex) => {
this._updateQueue();
queue.reject(ex);
});
break; // Stop Assigning
}
}
}
}
}
exports.BalanceNode = BalanceNode;
//# sourceMappingURL=BalanceNode.js.map