UNPKG

diffusion

Version:

Diffusion JavaScript client

115 lines (114 loc) 4.48 kB
"use strict"; /** * @module ValueStream */ Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkerValueStreamAdapter = void 0; var errors_1 = require("./../../../errors/errors"); var datatypes_1 = require("./../../data/datatypes"); var topic_type_1 = require("../../../topics/topic-type"); /** * Adapter for transforming the type of the emitted value to the datatype of * the ValueStream. */ var WorkerValueStreamAdapter = /** @class */ (function () { /** * Create a new ValueStreamAdapter instance * * @param stream the stream that is wrapped * @param datatype the data type of the value stream * @param topictype the topic type * @param subscription the subscription receiving the data * @param emitter the emitter of the underlying stream adapter */ function WorkerValueStreamAdapter(stream, datatype, topicSpec) { this.stream = stream; this.datatype = datatype; if (topicSpec.type !== topic_type_1.TopicType.TIME_SERIES) { this.sourceDatatype = datatypes_1.DataTypes.getByValue(topicSpec.type); } else { this.sourceDatatype = datatypes_1.DataTypes.timeseries(datatypes_1.DataTypes.getByName(topicSpec.properties.TIME_SERIES_EVENT_VALUE_TYPE)); } } /** * @inheritdoc */ WorkerValueStreamAdapter.prototype.selects = function (specification) { return this.stream.selects(specification); }; /** * @inheritdoc */ WorkerValueStreamAdapter.prototype.onOpen = function () { return this.stream.onOpen(); }; /** * @inheritdoc */ WorkerValueStreamAdapter.prototype.onDelta = function (path, specification, oldContent, content, delta, oldValue, newValue) { // AnyDataType needs special treatment if (this.datatype.name() === 'any') { return this.stream.onDelta(path, specification, oldContent, content, delta, oldValue, newValue); } var datatype = this.datatype; var oldValueDest = (oldContent !== null) ? this.sourceDatatype.readAs(datatype.valueClass, oldContent) : null; var newValueDest = this.sourceDatatype.readAs(datatype.valueClass, content); var oldContentDest; var newContentDest; try { oldContentDest = datatype.writeValueToArray(oldValueDest); newContentDest = datatype.writeValueToArray(newValueDest); } catch (e) { throw new errors_1.InvalidDataError(e.message); } return this.stream.onDelta(path, specification, oldContentDest, newContentDest, delta, oldValueDest, newValueDest); }; /** * @inheritdoc */ WorkerValueStreamAdapter.prototype.onValue = function (path, specification, oldContent, content, oldValue, newValue) { // AnyDataType needs special treatment if (this.datatype.name() === 'any') { return this.stream.onValue(path, specification, oldContent, content, oldValue, newValue); } var datatype = this.datatype; var oldValueDest = (oldContent !== null) ? this.sourceDatatype.readAs(datatype.valueClass, oldContent) : null; var newValueDest = this.sourceDatatype.readAs(datatype.valueClass, content); var oldContentDest; var newContentDest; try { oldContentDest = datatype.writeValueToArray(oldValueDest); newContentDest = datatype.writeValueToArray(newValueDest); } catch (e) { throw new errors_1.InvalidDataError(e.message); } return this.stream.onValue(path, specification, oldContentDest, newContentDest, oldValueDest, newValueDest); }; /** * @inheritdoc */ WorkerValueStreamAdapter.prototype.onSubscription = function (path, specification) { return this.stream.onSubscription(path, specification); }; /** * @inheritdoc */ WorkerValueStreamAdapter.prototype.onUnsubscription = function (path, specification, reason) { return this.stream.onUnsubscription(path, specification, reason); }; /** * @inheritdoc */ WorkerValueStreamAdapter.prototype.onSubscriptionError = function (error) { return this.stream.onSubscriptionError(error); }; return WorkerValueStreamAdapter; }()); exports.WorkerValueStreamAdapter = WorkerValueStreamAdapter;