diffusion
Version:
Diffusion JavaScript client
116 lines (97 loc) • 4.66 kB
JavaScript
var _implements = require('util/interface')._implements;
var api = require('../../features/topic-update');
var logger = require('util/logger').create('diffusion.locks.TopicUpdate');
var Services = require('services/services');
var Emitter = require('events/emitter');
var Result = require('events/result');
var Unconstrained = require('topic-update/update-constraint').Unconstrained;
var unconstrained = new Unconstrained();
var UpdateStreamFactory = require('topic-update/update-stream-factory');
var TopicType = require('../../topics/topics').TopicType;
var TopicUpdateImpl = _implements(
api.TopicUpdate,
function TopicUpdateImpl(internal) {
var SET_TOPIC = internal.getServiceLocator().obtain(Services.SET_TOPIC);
var ADD_AND_SET_TOPIC = internal.getServiceLocator().obtain(Services.ADD_AND_SET_TOPIC);
var streamServices = {
CREATE_UPDATE_STREAM: internal.getServiceLocator().obtain(Services.CREATE_UPDATE_STREAM),
CREATE_UPDATE_STREAM_AND_SET: internal.getServiceLocator().obtain(Services.CREATE_UPDATE_STREAM_AND_SET),
CHECK_UPDATE_STREAM: internal.getServiceLocator().obtain(Services.CHECK_UPDATE_STREAM),
STREAM_SET_TOPIC_SERVICE: internal.getServiceLocator().obtain(Services.STREAM_SET_TOPIC_SERVICE),
STREAM_APPLY_DELTA_SERVICE: internal.getServiceLocator().obtain(Services.STREAM_APPLY_DELTA_SERVICE),
STREAM_ADD_TOPIC_SERVICE: internal.getServiceLocator().obtain(Services.STREAM_ADD_TOPIC_SERVICE),
STREAM_ADD_AND_SET_TOPIC_SERVICE:
internal.getServiceLocator().obtain(Services.STREAM_ADD_AND_SET_TOPIC_SERVICE)
};
var updateStreamFactory = new UpdateStreamFactory(streamServices);
var setTopic = function(path, dataType, value, constraint) {
var emitter = new Emitter();
var result = new Result(emitter);
SET_TOPIC.send({
path: path,
topicType: TopicType[dataType.name().toUpperCase()],
value: dataType.from(value).asBuffer(),
constraint: constraint
},function(err, result) {
if (err) {
logger.debug('Set topic failed');
emitter.error(err);
}
else {
emitter.emit('complete', result);
}
});
return result;
};
var addAndSetTopic = function(path, specification, dataType, value, constraint) {
var emitter = new Emitter();
var result = new Result(emitter);
if (specification.type !== TopicType[dataType.name().toUpperCase()]) {
throw new Error("The specification and value have different data types");
}
ADD_AND_SET_TOPIC.send({
path: path,
topicSpecification: specification,
value: dataType.from(value).asBuffer(),
constraint: constraint
},function(err, result) {
if (err) {
logger.debug('Add and set topic failed');
emitter.error(err);
}
else {
emitter.emit('complete', result);
}
});
return result;
};
this.set = function(path, dataType, value, options) {
var constraint = (options!==undefined && options.constraint!==undefined)?options.constraint:unconstrained;
if (options!==undefined && options.specification!==undefined) {
return addAndSetTopic(path, options.specification, dataType, value, constraint);
}
else {
return setTopic(path, dataType, value, constraint);
}
};
this.createUpdateStream = function(path, dataType, options) {
var constraint = (options!==undefined && options.constraint!==undefined)?options.constraint:unconstrained;
if (options!==undefined && options.specification!==undefined) {
return updateStreamFactory.createUpdateStreamThatAddsTopic(
path,
options.specification,
dataType,
constraint
);
}
else {
return updateStreamFactory.createUpdateStream(
path,
dataType,
(constraint !== undefined)?constraint:unconstrained
);
}
};
}
);
module.exports = TopicUpdateImpl;