UNPKG

@openhps/core

Version:

Open Hybrid Positioning System - Core component

81 lines 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RemoteNode = void 0; const DataFrame_1 = require("../data/DataFrame"); const Node_1 = require("../Node"); const RemoteService_1 = require("../service/RemoteService"); const DataSerializer_1 = require("../data/DataSerializer"); /** * A remote node connects to a service in order to provide a remote connection. * @category Node */ class RemoteNode extends Node_1.Node { constructor(options, node) { var _a, _b; super(options); this.proxyNode = node; this.options.service = this.options.service || RemoteService_1.RemoteService; this.options.serialize = (_a = this.options.serialize) !== null && _a !== void 0 ? _a : ((object) => DataSerializer_1.DataSerializer.serialize(object)); this.options.deserialize = (_b = this.options.deserialize) !== null && _b !== void 0 ? _b : ((object) => DataSerializer_1.DataSerializer.deserialize(object)); this.on('push', this._onPush.bind(this)); this.on('pull', this._onPull.bind(this)); this.on('error', this._onDownstreamError.bind(this)); this.on('completed', this._onDownstreamCompleted.bind(this)); this.on('localpush', this._onLocalPush.bind(this)); this.on('localpull', this._onLocalPull.bind(this)); this.on('localevent', this._onLocalEvent.bind(this)); this.once('build', this._onBuild.bind(this)); } _onBuild() { return new Promise((resolve, reject) => { this.service = this.graph.findService(this.options.service); if (this.service === undefined || this.service === null) { return reject(new Error(`Remote service was not added to model!`)); } this.service.registerNode(this).then(resolve).catch(reject); }); } _onPush(frame, options) { return new Promise((resolve) => { // Send push to clients this.service.remotePush(this.uid, frame, Object.assign(Object.assign({}, options), this.options)); resolve(); }); } _onPull(options) { return new Promise((resolve) => { // Send pull to clients this.service.remotePull(this.uid, options); resolve(); }); } _onLocalPush(frame, options) { return new Promise((resolve) => { const frameDeserialized = frame instanceof DataFrame_1.DataFrame ? frame : this.options.deserialize(frame, options); this.outlets.forEach((outlet) => outlet.push(frameDeserialized, options)); resolve(); }); } _onLocalPull(options) { return new Promise((resolve, reject) => { Promise.all(this.inlets.map((inlet) => inlet.pull(options))) .then(() => { resolve(); }) .catch(reject); }); } _onLocalEvent(event, arg) { this.inlets.forEach((inlet) => inlet.emit(event, arg)); } _onDownstreamCompleted(event) { // Send completed event to client this.service.remoteEvent(this.uid, 'completed', event); } _onDownstreamError(error) { // Send error to clients this.service.remoteEvent(this.uid, 'error', error); } } exports.RemoteNode = RemoteNode; //# sourceMappingURL=RemoteNode.js.map