diffusion
Version:
Diffusion JavaScript client
96 lines (95 loc) • 4.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValueStreamWorkerReceiver = void 0;
var datatypes_1 = require("./../../data/datatypes");
var buffer_input_stream_1 = require("./../../io/buffer-input-stream");
var TopicSpecificationSerialiser = require("./../../topics/details/topic-specification-serialiser");
var logger = require("./../../util/logger");
var uint8array_1 = require("./../../util/uint8array");
var error_reason_1 = require("../../../errors/error-reason");
var topic_type_1 = require("../../../topics/topic-type");
var topics_1 = require("../../../topics/topics");
var log = logger.create('ValueStreamWorkerReceiver');
function deserialiseTopicSpecification(specification) {
var bis = new buffer_input_stream_1.BufferInputStream(uint8array_1.uint8FromBase64(specification));
return TopicSpecificationSerialiser.read(bis);
}
function deserialiseValue(value, streamDatatype, specification) {
if (!value) {
return null;
}
if (streamDatatype.name() === 'any') {
var datatype = (specification.type === topic_type_1.TopicTypeEnum.TIME_SERIES)
? datatypes_1.DataTypes.timeseries(datatypes_1.DataTypes.get(specification.properties.TIME_SERIES_EVENT_VALUE_TYPE))
: datatypes_1.DataTypes.get(specification.type);
return datatype.readValue(value);
}
return streamDatatype.readValue(value);
}
/**
* A wrapper around a StreamAdapter that receives and deserialises data from the
* shared session running on the shared worker.
*/
var ValueStreamWorkerReceiver = /** @class */ (function () {
function ValueStreamWorkerReceiver(stream, datatype) {
this.stream = stream;
this.datatype = datatype;
}
/**
* Handle open event
*/
ValueStreamWorkerReceiver.prototype.onOpen = function () {
this.stream.onOpen();
};
/**
* Handle a value event
*
* @param data the JSON serialised data from the worker
*/
ValueStreamWorkerReceiver.prototype.onValue = function (data) {
try {
var response = JSON.parse(data);
var specification = deserialiseTopicSpecification(response.specification);
var oldContent = response.oldValue ? uint8array_1.uint8FromBase64(response.oldValue) : null;
var newContent = uint8array_1.uint8FromBase64(response.newValue);
this.stream.onValue(response.path, specification, oldContent, newContent, deserialiseValue(oldContent, this.datatype, specification), deserialiseValue(newContent, this.datatype, specification));
}
catch (err) {
log.error('Invalid data', err);
}
};
/**
* Handle a subscription event
*
* @param path the topic path
* @param specification the topic specification
*/
ValueStreamWorkerReceiver.prototype.onSubscription = function (data) {
var response = JSON.parse(data);
this.stream.onSubscription(response.path, deserialiseTopicSpecification(response.specification));
};
/**
* Handle a subscription event
*
* @param path the topic path
* @param specification the topic specification
* @param reason the reason for unsubscribing
*/
ValueStreamWorkerReceiver.prototype.onUnsubscription = function (data) {
var response = JSON.parse(data);
var reason = response.reason !== undefined
? topics_1.UnsubscribeReasonEnum[topics_1.UnsubscribeReasonEnum[response.reason]]
: topics_1.UnsubscribeReasonEnum.UNKNOWN_UNSUBSCRIBE_REASON;
this.stream.onUnsubscription(response.path, deserialiseTopicSpecification(response.specification), reason);
};
/**
* Handle a subscription error
*
* @param error the error
*/
ValueStreamWorkerReceiver.prototype.onSubscriptionError = function (error) {
this.stream.onSubscriptionError(error_reason_1.ErrorReason[error_reason_1.ErrorReason[parseInt(error, 10)]]);
};
return ValueStreamWorkerReceiver;
}());
exports.ValueStreamWorkerReceiver = ValueStreamWorkerReceiver;