diffusion
Version:
Diffusion JavaScript client
52 lines (41 loc) • 1.86 kB
JavaScript
var DataTypes = require('data/datatypes');
/**
* Adapter for transforming the type of the emitted value to the datatype of
* the ValueStream.
*
* @param {ValueStreamProxy} stream - The stream that is wrapped
* @param {DataType} datatype - The data type of the value stream
* @param {TopicType} topictype - The topic type
*/
module.exports = function ValueStreamAdapter(stream, datatype, topictype) {
var sourceDatatype = DataTypes.getByValue(topictype);
this.subscription = stream.subscription;
this.emitter = stream.emitter;
this.selects = function(specification) {
return stream.selects(specification);
};
this.onOpen = function() {
return stream.onOpen();
};
this.onDelta = function(topic, details, specification, received, delta, oldValue, newValue) {
return stream.onDelta(topic, details, specification, received, delta,
sourceDatatype.readAs(datatype.valueClass, datatype.writeValue(oldValue)),
sourceDatatype.readAs(datatype.valueClass, datatype.writeValue(newValue))
);
};
this.onValue = function(topic, details, specification, received, oldValue, newValue) {
return stream.onValue(topic, details, specification, received,
sourceDatatype.readAs(datatype.valueClass, datatype.writeValue(oldValue)),
sourceDatatype.readAs(datatype.valueClass, datatype.writeValue(newValue))
);
};
this.onSubscription = function(topic, details, specification) {
return stream.onSubscription(topic, details, specification);
};
this.onUnsubscription = function(topic, details, specification, reason) {
return stream.onUnsubscription(topic, details, specification, reason);
};
this.onSubscriptionError = function(error) {
return stream.onSubscriptionError(error);
};
};