diffusion
Version:
Diffusion JavaScript client
165 lines (164 loc) • 7.16 kB
JavaScript
;
/**
* @module TopicUpdate
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateStreamFactory = void 0;
var errors_1 = require("./../../errors/errors");
var new_add_topic_and_update_stream_1 = require("./../topic-update/new-add-topic-and-update-stream");
var new_update_stream_1 = require("./../topic-update/new-update-stream");
var update_stream_1 = require("./../topic-update/update-stream");
var require_non_null_1 = require("./../util/require-non-null");
var topic_type_1 = require("../../topics/topic-type");
/**
* A factory for creating {@link UpdateStream}s
*/
var UpdateStreamFactory = /** @class */ (function () {
/**
* Create a new UpdateStreamFactory
*
* @param streamServices the stream services
*/
function UpdateStreamFactory(streamServices, context) {
this.context = context;
this.streamServices = streamServices;
}
/**
* Create a function that validates a value
*
* @param dataType the data type of the topic
* @return a callback that checks a value
*/
UpdateStreamFactory.prototype.getValueCheckOperation = function (dataType) {
switch (dataType.name()) {
case 'int64':
case 'double':
case 'string':
return function () { };
default:
return function (value) {
require_non_null_1.requireNonNull(value, 'null can only be passed for int64, double or string topics');
};
}
};
/**
* Get the command service that can be sent to indicate a change in value
*
* @param deltaType the delta type
* @return a {@link ServiceSender} that sends a change in value
*/
UpdateStreamFactory.prototype.getChangeService = function (deltaType, suppressDeltas) {
return (deltaType === undefined || suppressDeltas)
? this.streamServices.STREAM_SET_TOPIC_SERVICE
: this.streamServices.STREAM_APPLY_DELTA_SERVICE;
};
/**
* Get a function that converts a change in value to a Uint8Array
*
* @param dataType the data type
* @param deltaType the binary delta type if it exists
* @return a function for converting values to binary data
*/
UpdateStreamFactory.prototype.getToBytesFunction = function (dataType, deltaType) {
if (deltaType === undefined) {
return function (oldValue, newValue) {
return dataType.toBytes(newValue).asArray();
};
}
else {
return function (oldValue, newValue) {
var delta = deltaType.diff(oldValue, newValue);
return delta.$buffer.subarray(delta.$offset, delta.$offset + delta.$length);
};
}
};
/**
* Get the binary delta type for a given data type
*
* @param dataType the data type
* @return the corresponding binary delta type if it exists
*/
UpdateStreamFactory.prototype.binaryDeltaType = function (dataType, suppressDeltas) {
if (suppressDeltas) {
return undefined;
}
try {
return dataType.binaryDeltaType();
}
catch (e) {
// catch the error and return undefined to signal that no delta type is available
return undefined;
}
};
/**
* Create an update stream
*
* This creates an {@link UpdateStreamImpl} whose delegate has been set to
* a {@link NewUpdateStream}
*
* @param path the topic path to add and update
* @param dataType the data type of the values to update
* @param constraint the topic update constraint
* @return a new update stream
*/
UpdateStreamFactory.prototype.createUpdateStream = function (path, dataType, constraint, suppressDeltas) {
var _this = this;
if (suppressDeltas === void 0) { suppressDeltas = false; }
var deltaType = this.binaryDeltaType(dataType, suppressDeltas);
var options = {
dataType: dataType,
retainsValue: true,
supportsConflation: true,
checkOperation: this.getValueCheckOperation(dataType),
toBuffer: this.getToBytesFunction(dataType, deltaType),
deltaService: this.getChangeService(deltaType, suppressDeltas)
};
var topicType = topic_type_1.TopicTypeEnum[dataType.name().toUpperCase()];
return new update_stream_1.UpdateStreamImpl(function (updateStreamImpl) {
return new new_update_stream_1.NewUpdateStream(_this.streamServices, options, updateStreamImpl, path, topicType, constraint);
});
};
/**
* Create an update stream that adds a topic
*
* This creates an {@link UpdateStreamImpl} whose delegate has been set to
* a {@link NewAddTopicAndUpdateStream}
*
* @param path the topic path to add and update
* @param specification the topic specification of the topic to add
* @param dataType the data type of the values to update
* @param constraint the topic update constraint
* @return a new update stream
*/
UpdateStreamFactory.prototype.createUpdateStreamThatAddsTopic = function (path, specification, dataType, constraint, suppressDeltas) {
var _this = this;
if (suppressDeltas === void 0) { suppressDeltas = false; }
if (specification.type === topic_type_1.TopicTypeEnum.TIME_SERIES) {
var eventType = specification.properties.TIME_SERIES_EVENT_VALUE_TYPE;
// tslint:disable-next-line:strict-type-predicates
if (eventType === null || eventType === undefined) {
throw new errors_1.IllegalArgumentError('The specification for time-series topics requires an event type.');
}
else if (eventType.toLowerCase() !== dataType.name().toLowerCase()) {
throw new errors_1.IllegalArgumentError('The time-series event and value have different data types.');
}
}
else if (dataType !== this.context.DataTypes.getByValue(specification.type)) {
throw new errors_1.IllegalArgumentError('The specification and value have different data types');
}
var deltaType = this.binaryDeltaType(dataType, suppressDeltas);
var options = {
dataType: dataType,
retainsValue: true,
supportsConflation: true,
checkOperation: this.getValueCheckOperation(dataType),
toBuffer: this.getToBytesFunction(dataType, deltaType),
deltaService: this.getChangeService(deltaType, suppressDeltas)
};
return new update_stream_1.UpdateStreamImpl(function (updateStreamImpl) {
return new new_add_topic_and_update_stream_1.NewAddTopicAndUpdateStream(_this.streamServices, options, updateStreamImpl, path, specification, constraint);
});
};
return UpdateStreamFactory;
}());
exports.UpdateStreamFactory = UpdateStreamFactory;