diffusion
Version:
Diffusion JavaScript client
39 lines (29 loc) • 1.09 kB
JavaScript
var Services = require('services/services');
var DataTypes = require('data/datatypes');
var UpdateFailReason = require('../../../topics/topics').UpdateFailReason;
module.exports = function UniversalUpdater(internal) {
var SET_SERVICE = internal.getServiceLocator().obtain(Services.UPDATE_TOPIC_SET);
function dataToBytes(d) {
return d.$buffer.slice(d.$offset, d.$length);
}
this.update = function(topic, content, callback) {
var datatype = DataTypes.get(content);
if (!datatype) {
callback(UpdateFailReason.INCOMPATIBLE_UPDATE);
return;
}
var value = datatype.from(content);
SET_SERVICE.send({
path : topic,
bytes : dataToBytes(value)
}, callback);
};
this.updateValue = function(topic, content, datatype, callback) {
var checkedDataType = DataTypes.getChecked(datatype);
var value = checkedDataType.from(content);
SET_SERVICE.send({
path : topic,
bytes : dataToBytes(value)
}, callback);
};
};