@openhps/core
Version:
Open Hybrid Positioning System - Core component
164 lines • 5.18 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { GraphNode } from '../GraphNode';
import { BroadcastNode } from '../../../nodes/shapes/BroadcastNode';
import { Edge } from '../../Edge';
import { Node } from '../../../Node';
import { SerializableMapMember, SerializableMember, SerializableObject } from '../../../data/decorators';
import { DataSerializer } from '../../../data';
/**
* @category Graph
*/
let GraphShape = class GraphShape extends Node {
constructor() {
super();
this._nodes = new Map();
this._edges = new Map();
this.internalSource = new BroadcastNode();
this.internalSink = new 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);
}
};
__decorate([SerializableMapMember(String, GraphNode, {
name: 'nodes'
}), __metadata("design:type", Map)], GraphShape.prototype, "_nodes", void 0);
__decorate([SerializableMapMember(String, Edge, {
serializer: edges => {
if (!edges) {
return [];
}
return Array.from(edges.values()).map(edge => ({
input: edge.inputNode.uid,
output: edge.outputNode.uid
}));
},
name: 'edges'
}), __metadata("design:type", Map)], GraphShape.prototype, "_edges", void 0);
__decorate([SerializableMember({
serializer: node => node ? node.uid : undefined,
deserializer: () => {
return undefined;
}
}), __metadata("design:type", GraphNode)], GraphShape.prototype, "internalSource", void 0);
__decorate([SerializableMember({
serializer: node => node ? node.uid : undefined,
deserializer: () => {
return undefined;
}
}), __metadata("design:type", GraphNode)], GraphShape.prototype, "internalSink", void 0);
GraphShape = __decorate([SerializableObject({
initializer: (sourceObject, raw) => {
const expectedType = 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(sourceObject._nodes.get(edge.input), sourceObject._nodes.get(edge.output)));
});
return targetObject;
}
}), __metadata("design:paramtypes", [])], GraphShape);
export { GraphShape };