diffusion
Version:
Diffusion JavaScript client
169 lines (168 loc) • 7.63 kB
JavaScript
;
/**
* @module Session.topicUpdate
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TopicUpdateImpl = void 0;
var errors_1 = require("./../../errors/errors");
var Services = require("./../services/topic-update-services");
var update_constraint_1 = require("./../topic-update/update-constraint");
var update_stream_factory_1 = require("./../topic-update/update-stream-factory");
var logger = require("./../util/logger");
var update_stream_builder_1 = require("../topic-update/update-stream-builder");
var log = logger.create('Session.TopicUpdate');
/**
* An unconstrained topic constraint that is sent to the server when no other
* constraint has been specified.
*/
var unconstrained = new update_constraint_1.Unconstrained();
/**
* The implementation of the {@link TopicUpdate} feature
*/
var TopicUpdateImpl = /** @class */ (function () {
/**
* Create a new instance of the TopicUpdate feature
*
* @param internal the internal session
*/
function TopicUpdateImpl(internal) {
this.internal = internal;
var serviceLocator = internal.getServiceLocator();
this.SET_TOPIC = serviceLocator.obtain(Services.SET_TOPIC);
this.ADD_AND_SET_TOPIC = serviceLocator.obtain(Services.ADD_AND_SET_TOPIC);
this.APPLY_JSON_PATCH = serviceLocator.obtain(Services.APPLY_JSON_PATCH);
var streamServices = {
CREATE_UPDATE_STREAM: serviceLocator.obtain(Services.CREATE_UPDATE_STREAM),
CREATE_UPDATE_STREAM_AND_SET: serviceLocator.obtain(Services.CREATE_UPDATE_STREAM_AND_SET),
CHECK_UPDATE_STREAM: serviceLocator.obtain(Services.CHECK_UPDATE_STREAM),
STREAM_SET_TOPIC_SERVICE: serviceLocator.obtain(Services.STREAM_SET_TOPIC_SERVICE),
STREAM_APPLY_DELTA_SERVICE: serviceLocator.obtain(Services.STREAM_APPLY_DELTA_SERVICE),
STREAM_ADD_TOPIC_SERVICE: serviceLocator.obtain(Services.STREAM_ADD_TOPIC_SERVICE),
STREAM_ADD_AND_SET_TOPIC_SERVICE: serviceLocator.obtain(Services.STREAM_ADD_AND_SET_TOPIC_SERVICE)
};
this.updateStreamFactory = new update_stream_factory_1.UpdateStreamFactory(streamServices, internal.getContext());
}
/**
* Set topic value for an existing topic
*
* @param path the path of the topic
* @param dataType the type of the values
* @param value the value. String, int64, and double topics accept
* `null` or `undefined` , as described above.
* Using `null` or `undefined` with other
* topic types is an error and will throw an `Error` .
* @param constraint the topic update constraint
* @return a result that completes when the operation succeeds
*/
TopicUpdateImpl.prototype.setTopic = function (path, dataType, value, constraint) {
var _this = this;
return new Promise(function (resolve, reject) {
// eslint-disable-next-line @typescript-eslint/naming-convention
var TopicType = _this.internal.getContext().TopicType;
_this.SET_TOPIC.send({
path: path,
topicType: TopicType[dataType.name().toUpperCase()],
value: dataType.toBytes(value).asArray(),
constraint: constraint
}, function (err) {
if (err) {
log.debug('Set topic failed');
reject(err);
}
else {
resolve();
}
});
});
};
/**
* Add a topic and set the topic value
*
* @param path the path of the topic
* @param specification the topic specification
* @param dataType the type of the values
* @param value the value. String, int64, and double topics accept
* `null` or `undefined` , as described above.
* Using `null` or `undefined` with other
* topic types is an error and will throw an `Error` .
* @param constraint the topic update constraint
* @return a result that completes with the topic creation result
* when the operation succeeds
*/
TopicUpdateImpl.prototype.addAndSetTopic = function (path, specification, dataType, value, constraint) {
var _this = this;
// eslint-disable-next-line @typescript-eslint/naming-convention
var TopicType = this.internal.getContext().TopicType;
if (specification.type !== TopicType[dataType.name().toUpperCase()]
&& specification.type !== TopicType.TIME_SERIES) {
throw new errors_1.IllegalArgumentError('The specification and value have different data types');
}
var requestValue = dataType.toBytes(value).asArray();
return new Promise(function (resolve, reject) {
_this.ADD_AND_SET_TOPIC.send({
path: path,
topicSpecification: specification,
value: requestValue,
constraint: constraint
}, function (err, response) {
if (err) {
log.debug('Add and set topic failed');
reject(err);
}
else {
resolve(response.result);
}
});
});
};
/**
* @inheritdoc
*/
TopicUpdateImpl.prototype.set = function (path, dataType, value, options) {
var constraint = (options !== undefined && options.constraint !== undefined)
? options.constraint
: unconstrained;
if (options !== undefined && options.specification !== undefined) {
return this.addAndSetTopic(path, options.specification, dataType, value, constraint);
}
else {
return this.setTopic(path, dataType, value, constraint);
}
};
/**
* @inheritdoc
*/
TopicUpdateImpl.prototype.createUpdateStream = function (path, dataType, options) {
var _a;
var constraint = (_a = options === null || options === void 0 ? void 0 : options.constraint) !== null && _a !== void 0 ? _a : unconstrained;
if (options !== undefined && options.specification !== undefined) {
return this.updateStreamFactory.createUpdateStreamThatAddsTopic(path, options.specification, dataType, constraint);
}
else {
return this.updateStreamFactory.createUpdateStream(path, dataType, constraint);
}
};
TopicUpdateImpl.prototype.newUpdateStreamBuilder = function () {
return new update_stream_builder_1.UpdateStreamBuilderImpl(this.internal.getContext(), this.updateStreamFactory);
};
TopicUpdateImpl.prototype.applyJsonPatch = function (path, patch, constraint) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.APPLY_JSON_PATCH.send({
path: path,
patch: typeof patch === 'string' ? patch : JSON.stringify(patch),
constraint: constraint ? constraint : unconstrained
}, function (err, response) {
if (err) {
log.debug('Apply JSON patch failed');
reject(err);
}
else {
resolve(response);
}
});
});
};
return TopicUpdateImpl;
}());
exports.TopicUpdateImpl = TopicUpdateImpl;