UNPKG

diffusion

Version:

Diffusion JavaScript client

117 lines (116 loc) 5 kB
"use strict"; /** * @module TopicUpdate */ Object.defineProperty(exports, "__esModule", { value: true }); exports.NewUpdateStream = void 0; var errors_1 = require("./../../errors/errors"); var create_update_stream_and_set_request_1 = require("./../services/topic-update/create-update-stream-and-set-request"); var create_update_stream_request_1 = require("./../services/topic-update/create-update-stream-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 topic_update_1 = require("../../topic-update/topic-update"); var log = logger.create('diffusion.TopicUpdate'); /** * An update stream delegate that is used when a new stream is created that * should not add a topic. No calls to {@link set} or {@link validate} have been * made yet. */ var NewUpdateStream = /** @class */ (function () { /** * Create a new NewUpdateStream 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 topicType the topic type * @param constraint the topic update constraint */ function NewUpdateStream(streamServices, options, stream, path, topicType, constraint) { this.streamServices = streamServices; this.options = options; this.stream = stream; this.path = path; this.topicType = topicType; this.constraint = constraint; } /** * @inheritdoc */ NewUpdateStream.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.CREATE_UPDATE_STREAM_AND_SET.send(new create_update_stream_and_set_request_1.CreateUpdateStreamAndSetRequest(_this.path, _this.topicType, buffer, _this.constraint), function (err, response) { if (!response_success_1.responseSuccess(err, response)) { log.debug('Failed to create update stream and set'); _this.stream.onSetFailed(err); reject(err); } else { _this.stream.onSetComplete(response.streamId, response.retainsValue, response.supportsConflation); resolve(topic_update_1.TopicCreationResult.EXISTS); } }); }); this.stream.setDelegate(new pending_set_stream_1.PendingSetStream(this.streamServices, this.options, this.stream, result, value)); return result; }; /** * @inheritdoc */ NewUpdateStream.prototype.get = function () { throw new errors_1.IllegalStateError('The set method has not been called'); }; /** * @inheritdoc */ NewUpdateStream.prototype.validate = function () { var _this = this; var result = new Promise(function (resolve, reject) { _this.streamServices.CREATE_UPDATE_STREAM.send(new create_update_stream_request_1.CreateUpdateStreamRequest(_this.path, _this.topicType, _this.constraint), function (err, response) { if (!response_success_1.responseSuccess(err, response)) { log.debug('Create update stream failed'); _this.stream.onValidateFailed(err); reject(err); } else { _this.stream.onValidateComplete(response.streamId, response.retainsValue, response.supportsConflation); resolve(topic_update_1.TopicCreationResult.EXISTS); } }); }); this.stream.setDelegate(new pending_validate_stream_1.PendingValidateStream(this.streamServices, this.options, this.stream, result)); return result; }; /** * @inheritdoc */ NewUpdateStream.prototype.onSetComplete = function () { throw new errors_1.IllegalStateError('No set request has been sent'); }; /** * @inheritdoc */ NewUpdateStream.prototype.onSetFailed = function () { throw new errors_1.IllegalStateError('No set request has been sent'); }; /** * @inheritdoc */ NewUpdateStream.prototype.onValidateComplete = function () { throw new errors_1.IllegalStateError('No validate request has been sent'); }; /** * @inheritdoc */ NewUpdateStream.prototype.onValidateFailed = function () { throw new errors_1.IllegalStateError('No validate request has been sent'); }; return NewUpdateStream; }()); exports.NewUpdateStream = NewUpdateStream;