UNPKG

diffusion

Version:

Diffusion JavaScript client

116 lines (115 loc) 5.06 kB
"use strict"; /** * @module TopicUpdate */ Object.defineProperty(exports, "__esModule", { value: true }); exports.NewAddTopicAndUpdateStream = void 0; var errors_1 = require("./../../errors/errors"); var add_and_set_topic_request_1 = require("./../services/topic-update/add-and-set-topic-request"); var update_stream_add_topic_request_1 = require("./../services/topic-update/update-stream-add-topic-request"); var pending_set_stream_1 = require("./../topic-update/pending-set-stream"); var pending_validate_stream_1 = require("./../topic-update/pending-validate-stream"); var logger = require("./../util/logger"); var response_success_1 = require("./../util/response-success"); var log = logger.create('diffusion.TopicUpdate'); /** * An update stream delegate that is used when a new stream is created that * should add a topic. No calls to {@link set} or {@link validate} have been * made yet. */ var NewAddTopicAndUpdateStream = /** @class */ (function () { /** * Create a new NewAddTopicAndUpdateStream instance * * @param streamServices the stream services * @param options the update stream options * @param stream the stream that using this stream as a delegate * @param path the topic path to add and update * @param specification the topic specification of the topic to add * @param constraint the topic update constraint */ function NewAddTopicAndUpdateStream(streamServices, options, stream, path, specification, constraint) { this.streamServices = streamServices; this.options = options; this.stream = stream; this.path = path; this.specification = specification; this.constraint = constraint; } /** * @inheritdoc */ NewAddTopicAndUpdateStream.prototype.set = function (value) { var _this = this; this.options.checkOperation(value); var result = new Promise(function (resolve, reject) { var buffer = _this.options.dataType.toBytes(value).asArray(); _this.streamServices.STREAM_ADD_AND_SET_TOPIC_SERVICE.send(new add_and_set_topic_request_1.AddAndSetTopicRequest(_this.path, _this.specification, buffer, _this.constraint), function (err, response) { if (!response_success_1.responseSuccess(err, response)) { log.debug('Stream add and set topic failed'); _this.stream.onSetFailed(err); reject(err); } else { _this.stream.onSetComplete(response.streamId, response.retainsValue, response.supportsConflation); resolve(response.creationResult); } }); }); this.stream.setDelegate(new pending_set_stream_1.PendingSetStream(this.streamServices, this.options, this.stream, result, value)); return result; }; /** * @inheritdoc */ NewAddTopicAndUpdateStream.prototype.get = function () { throw new errors_1.IllegalStateError('The set method has not been called'); }; /** * @inheritdoc */ NewAddTopicAndUpdateStream.prototype.validate = function () { var _this = this; var result = new Promise(function (resolve, reject) { _this.streamServices.STREAM_ADD_TOPIC_SERVICE.send(new update_stream_add_topic_request_1.UpdateStreamAddTopicRequest(_this.path, _this.specification, _this.constraint), function (err, response) { if (!response_success_1.responseSuccess(err, response)) { log.debug('Stream add topic failed'); _this.stream.onValidateFailed(err); reject(err); } else { _this.stream.onValidateComplete(response.streamId, response.retainsValue, response.supportsConflation); resolve(response.creationResult); } }); }); this.stream.setDelegate(new pending_validate_stream_1.PendingValidateStream(this.streamServices, this.options, this.stream, result)); return result; }; /** * @inheritdoc */ NewAddTopicAndUpdateStream.prototype.onSetComplete = function () { throw new errors_1.IllegalStateError('No set request has been sent'); }; /** * @inheritdoc */ NewAddTopicAndUpdateStream.prototype.onSetFailed = function () { throw new errors_1.IllegalStateError('No set request has been sent'); }; /** * @inheritdoc */ NewAddTopicAndUpdateStream.prototype.onValidateComplete = function () { throw new errors_1.IllegalStateError('No validate request has been sent'); }; /** * @inheritdoc */ NewAddTopicAndUpdateStream.prototype.onValidateFailed = function () { throw new errors_1.IllegalStateError('No validate request has been sent'); }; return NewAddTopicAndUpdateStream; }()); exports.NewAddTopicAndUpdateStream = NewAddTopicAndUpdateStream;