@openhps/core
Version:
Open Hybrid Positioning System - Core component
74 lines • 2.81 kB
JavaScript
import { DataFrame } from '../data/DataFrame';
import { Node } from '../Node';
import { RemoteService } from '../service/RemoteService';
import { DataSerializer } from '../data/DataSerializer';
/**
* A remote node connects to a service in order to provide a remote connection.
* @category Node
*/
export class RemoteNode extends Node {
constructor(options, node) {
var _a, _b;
super(options);
this.proxyNode = node;
this.options.service = this.options.service || RemoteService;
this.options.serialize = (_a = this.options.serialize) !== null && _a !== void 0 ? _a : object => DataSerializer.serialize(object);
this.options.deserialize = (_b = this.options.deserialize) !== null && _b !== void 0 ? _b : object => 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 ? 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);
}
}