@openhps/core
Version:
Open Hybrid Positioning System - Core component
186 lines • 6.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GraphShape = void 0;
const tslib_1 = require("tslib");
const GraphNode_1 = require("../GraphNode");
const BroadcastNode_1 = require("../../../nodes/shapes/BroadcastNode");
const Edge_1 = require("../../Edge");
const Node_1 = require("../../../Node");
const decorators_1 = require("../../../data/decorators");
const data_1 = require("../../../data");
/**
* @category Graph
*/
let GraphShape = class GraphShape extends Node_1.Node {
constructor() {
super();
this._nodes = new Map();
this._edges = new Map();
this.internalSource = new BroadcastNode_1.BroadcastNode();
this.internalSink = new BroadcastNode_1.BroadcastNode();
/**
* Graph logger
* @returns {(level: string, message: string, data?: any) => void} logger function
*/
this.logger = () => undefined;
// Internal input and output nodes
this.addNode(this.internalSource);
this.addNode(this.internalSink);
// Graph building and destroying
this.once('build', this._onBuild.bind(this));
this.once('destroy', this._onDestroy.bind(this));
// Error handling
this.removeAllListeners('error');
this.internalSource.on('error', this.onError.bind(this));
this.internalSink.on('error', this.onError.bind(this));
// Completed event
this.removeAllListeners('completed');
this.internalSource.on('completed', this.onCompleted.bind(this));
this.internalSink.on('completed', this.onCompleted.bind(this));
}
_onDestroy() {
this.nodes.forEach((node) => {
node.emit('destroy');
});
}
_onBuild(_) {
return new Promise((resolve, reject) => {
Promise.all(this.nodes.map((node) => node.emitAsync('build', _)))
.then(() => {
this.emit('ready');
resolve();
})
.catch((ex) => {
reject(ex);
});
});
}
get edges() {
return this._edges ? Array.from(this._edges.values()) : [];
}
set edges(edges) {
edges.forEach(this.addEdge);
}
get nodes() {
return this._nodes ? Array.from(this._nodes.values()) : [];
}
set nodes(nodes) {
nodes.forEach(this.addNode);
}
findNodeByUID(uid) {
return this._nodes.get(uid);
}
findNodeByName(name) {
let result;
this._nodes.forEach((node) => {
if (node.name === name) {
result = node;
return;
}
});
return result;
}
addNode(node) {
node.graph = this.graph === undefined ? this : this.model;
this._nodes.set(node.uid, node);
}
addEdge(edge) {
this._edges.set(edge.inputNode.uid + edge.outputNode.uid, edge);
}
deleteEdge(edge) {
this._edges.delete(edge.inputNode.uid + edge.outputNode.uid);
}
deleteNode(node) {
this._nodes.delete(node.uid);
}
/**
* Find an edge by the identifiers of its inlet and outlet
* @param {string} inlet Node uid of inlet
* @param {string} outlet Node uid of outlet
* @returns {Edge<any>} Edge
*/
findEdge(inlet, outlet) {
return this._edges.get(inlet + outlet);
}
/**
* Send a pull request to the graph
* @param {PullOptions} [options] Pull options
* @returns {PullPromise<void>} Pull promise
*/
pull(options) {
return this.internalSink.pull(options);
}
/**
* Push data to the graph
* @param {DataFrame | DataFrame[]} frame Data frame to push
* @param {PushOptions} [options] Push options
* @returns {PushPromise<void>} Push promise
*/
push(frame, options) {
return this.internalSource.push(frame, options);
}
onError(event) {
// Do not emit if no listeners attached
// Event emitter will throw an uncaught exception
if (this.listenerCount('error') > 0)
this.emit('error', event);
}
onCompleted(event) {
this.emit('completed', event);
}
};
exports.GraphShape = GraphShape;
tslib_1.__decorate([
(0, decorators_1.SerializableMapMember)(String, GraphNode_1.GraphNode, {
name: 'nodes',
}),
tslib_1.__metadata("design:type", Map)
], GraphShape.prototype, "_nodes", void 0);
tslib_1.__decorate([
(0, decorators_1.SerializableMapMember)(String, Edge_1.Edge, {
serializer: (edges) => {
if (!edges) {
return [];
}
return Array.from(edges.values()).map((edge) => ({
input: edge.inputNode.uid,
output: edge.outputNode.uid,
}));
},
name: 'edges',
}),
tslib_1.__metadata("design:type", Map)
], GraphShape.prototype, "_edges", void 0);
tslib_1.__decorate([
(0, decorators_1.SerializableMember)({
serializer: (node) => (node ? node.uid : undefined),
deserializer: () => {
return undefined;
},
}),
tslib_1.__metadata("design:type", GraphNode_1.GraphNode)
], GraphShape.prototype, "internalSource", void 0);
tslib_1.__decorate([
(0, decorators_1.SerializableMember)({
serializer: (node) => (node ? node.uid : undefined),
deserializer: () => {
return undefined;
},
}),
tslib_1.__metadata("design:type", GraphNode_1.GraphNode)
], GraphShape.prototype, "internalSink", void 0);
exports.GraphShape = GraphShape = tslib_1.__decorate([
(0, decorators_1.SerializableObject)({
initializer: (sourceObject, raw) => {
const expectedType = data_1.DataSerializer.findTypeByName(raw.__type);
const targetObject = new expectedType();
Object.assign(targetObject, sourceObject);
raw.edges.forEach((edge) => {
targetObject._edges.set(edge.input + edge.output, new Edge_1.Edge(sourceObject._nodes.get(edge.input), sourceObject._nodes.get(edge.output)));
});
return targetObject;
},
}),
tslib_1.__metadata("design:paramtypes", [])
], GraphShape);
//# sourceMappingURL=GraphShape.js.map