diffusion
Version:
Diffusion JavaScript client
192 lines (191 loc) • 7.9 kB
JavaScript
;
/**
* @module ValueStream
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValueStreamTimeseriesToPrimitiveAdapter = exports.ValueStreamTimeseriesAdapter = exports.ValueStreamAdapter = exports.ValueStreamAdapterBase = void 0;
var errors_1 = require("./../../../errors/errors");
var datatypes_1 = require("./../../data/datatypes");
var time_series_event_1 = require("./../../services/timeseries/time-series-event");
/**
* Adapter for transforming the type of the emitted value to the datatype of
* the ValueStream.
*/
var ValueStreamAdapterBase = /** @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
*/
function ValueStreamAdapterBase(stream, datatype, topicSpec) {
this.stream = stream;
this.datatype = datatype;
this.sourceDatatype = this.getSourceDataType(topicSpec);
}
/**
* @inheritdoc
*/
ValueStreamAdapterBase.prototype.selects = function (specification) {
return this.stream.selects(specification);
};
/**
* @inheritdoc
*/
ValueStreamAdapterBase.prototype.onOpen = function () {
return this.stream.onOpen();
};
/**
* @inheritdoc
*/
ValueStreamAdapterBase.prototype.onDelta = function (path, specification, oldContent, content, delta, oldValue, newValue) {
return this.stream.onDelta(path, specification, oldContent, content, delta, (oldContent !== null) ? this.readAsDataType(oldContent, oldValue) : null, this.readAsDataType(content, newValue));
};
/**
* @inheritdoc
*/
ValueStreamAdapterBase.prototype.onValue = function (path, specification, oldContent, content, oldValue, newValue) {
return this.stream.onValue(path, specification, oldContent, content, (oldContent !== null) ? this.readAsDataType(oldContent, oldValue) : null, this.readAsDataType(content, newValue));
};
/**
* @inheritdoc
*/
ValueStreamAdapterBase.prototype.onSubscription = function (path, specification) {
return this.stream.onSubscription(path, specification);
};
/**
* @inheritdoc
*/
ValueStreamAdapterBase.prototype.onUnsubscription = function (path, specification, reason) {
return this.stream.onUnsubscription(path, specification, reason);
};
/**
* @inheritdoc
*/
ValueStreamAdapterBase.prototype.onSubscriptionError = function (error) {
return this.stream.onSubscriptionError(error);
};
return ValueStreamAdapterBase;
}());
exports.ValueStreamAdapterBase = ValueStreamAdapterBase;
/**
* Adapter for transforming the type of the emitted value to the datatype of
* the ValueStream.
*/
var ValueStreamAdapter = /** @class */ (function (_super) {
__extends(ValueStreamAdapter, _super);
/**
* Create a new ValueStreamAdapter instance
*
* @param stream the stream that is wrapped
* @param datatype the data type of the value stream
* @param topicSpec the topic type information
*/
function ValueStreamAdapter(stream, datatype, topicSpec) {
return _super.call(this, stream, datatype, topicSpec) || this;
}
/**
* @inheritdoc
*/
ValueStreamAdapter.prototype.getSourceDataType = function (topicSpec) {
return datatypes_1.DataTypes.getByValue(topicSpec.type);
};
/**
* @inheritdoc
*/
ValueStreamAdapter.prototype.readAsDataType = function (buffer) {
return this.sourceDatatype.readAs(this.datatype.valueClass, buffer);
};
return ValueStreamAdapter;
}(ValueStreamAdapterBase));
exports.ValueStreamAdapter = ValueStreamAdapter;
var ValueStreamTimeseriesAdapter = /** @class */ (function (_super) {
__extends(ValueStreamTimeseriesAdapter, _super);
/**
* Create a new ValueStreamTimeseriesAdapter instance
*
* @param stream the stream that is wrapped
* @param datatype the data type of the value stream
* @param topicSpec the topic type information
*/
function ValueStreamTimeseriesAdapter(stream, datatype, topicSpec) {
var _this = _super.call(this, stream, datatype, topicSpec) || this;
_this.sourceValuetype = datatypes_1.DataTypes.getByName(_this.sourceDatatype.valueTypeName);
_this.destValuetype = datatypes_1.DataTypes.getByName(_this.datatype.valueTypeName);
return _this;
}
/**
* @inheritdoc
*/
ValueStreamTimeseriesAdapter.prototype.getSourceDataType = function (topicSpec) {
return datatypes_1.DataTypes.timeseries(datatypes_1.DataTypes.getByName(topicSpec.properties.TIME_SERIES_EVENT_VALUE_TYPE));
};
/**
* @inheritdoc
*/
ValueStreamTimeseriesAdapter.prototype.readAsDataType = function (buffer, sourceEvent) {
var sourceBuffer;
try {
sourceBuffer = this.sourceValuetype.writeValueToArray(sourceEvent.value);
}
catch (e) {
throw new errors_1.InvalidDataError(e.message);
}
var destValue = this.sourceValuetype.readAs(this.destValuetype.valueClass, sourceBuffer);
return new time_series_event_1.EventImpl(sourceEvent.metadata, sourceEvent.originalEvent, destValue);
};
return ValueStreamTimeseriesAdapter;
}(ValueStreamAdapterBase));
exports.ValueStreamTimeseriesAdapter = ValueStreamTimeseriesAdapter;
var ValueStreamTimeseriesToPrimitiveAdapter = /** @class */ (function (_super) {
__extends(ValueStreamTimeseriesToPrimitiveAdapter, _super);
/**
* Create a new ValueStreamTimeseriesAdapter instance
*
* @param stream the stream that is wrapped
* @param datatype the data type of the value stream
* @param topicSpec the topic type information
*/
function ValueStreamTimeseriesToPrimitiveAdapter(stream, datatype, topicSpec) {
var _this = _super.call(this, stream, datatype, topicSpec) || this;
_this.sourceValuetype = datatypes_1.DataTypes.getByName(_this.sourceDatatype.valueTypeName);
return _this;
}
/**
* @inheritdoc
*/
ValueStreamTimeseriesToPrimitiveAdapter.prototype.getSourceDataType = function (topicSpec) {
return datatypes_1.DataTypes.timeseries(datatypes_1.DataTypes.getByName(topicSpec.properties.TIME_SERIES_EVENT_VALUE_TYPE));
};
/**
* @inheritdoc
*/
ValueStreamTimeseriesToPrimitiveAdapter.prototype.readAsDataType = function (buffer, sourceEvent) {
var sourceBuffer;
try {
sourceBuffer = this.sourceValuetype.writeValueToArray(sourceEvent.value);
}
catch (e) {
throw new errors_1.InvalidDataError(e.message);
}
return this.sourceValuetype.readAs(this.datatype.valueClass, sourceBuffer);
};
return ValueStreamTimeseriesToPrimitiveAdapter;
}(ValueStreamAdapterBase));
exports.ValueStreamTimeseriesToPrimitiveAdapter = ValueStreamTimeseriesToPrimitiveAdapter;