UNPKG

diffusion

Version:

Diffusion JavaScript client

107 lines (106 loc) 3.71 kB
"use strict"; /** * @module TopicUpdate */ Object.defineProperty(exports, "__esModule", { value: true }); exports.SetStream = void 0; var update_stream_request_1 = require("./../services/topic-update/update-stream-request"); var invalid_set_stream_1 = require("./../topic-update/invalid-set-stream"); var logger = require("./../util/logger"); var topic_update_1 = require("../../topic-update/topic-update"); var log = logger.create('diffusion.TopicUpdate'); /** * An update stream delegate that is used when a {@link set} operation has * completed successfully. A cached value is available. */ var SetStream = /** @class */ (function () { /** * Create a new SetStream instance * * @param streamServices the stream services * @param options the update stream options * @param stream the stream that using this stream as a delegate * @param streamId the stream ID * @param value the cached value */ function SetStream(streamServices, options, stream, streamId, value) { this.streamServices = streamServices; this.options = options; this.stream = stream; this.streamId = streamId; this.cache = options.dataType.from(value); } /** * @inheritdoc */ SetStream.prototype.set = function (value) { var _this = this; this.options.checkOperation(value); return new Promise(function (resolve, reject) { var buffer = _this.options.toBuffer(_this.cache, value); _this.cache = _this.options.dataType.from(value); _this.options.deltaService.send(new update_stream_request_1.UpdateStreamRequest(_this.streamId, buffer), function (err) { if (err) { log.debug('Set update stream failed'); _this.stream.onSetFailed(err); reject(err); } else { _this.stream.onSetComplete(_this.streamId, _this.options.retainsValue, _this.options.supportsConflation); resolve(); } }); }); }; /** * @inheritdoc */ SetStream.prototype.get = function () { return this.options.dataType.readValue(this.cache); }; /** * @inheritdoc */ SetStream.prototype.validate = function () { var _this = this; return new Promise(function (resolve, reject) { _this.streamServices.CHECK_UPDATE_STREAM.send(_this.streamId, function (err) { if (err) { log.debug('Validate update stream failed'); _this.stream.onValidateFailed(err); reject(err); } else { _this.stream.onValidateComplete(_this.streamId, _this.options.retainsValue, _this.options.supportsConflation); resolve(topic_update_1.TopicCreationResult.EXISTS); } }); }); }; /** * @inheritdoc */ SetStream.prototype.onSetComplete = function () { // no-op }; /** * @inheritdoc */ SetStream.prototype.onSetFailed = function (err) { this.stream.setDelegate(new invalid_set_stream_1.InvalidSetStream(err, this.cache)); }; /** * @inheritdoc */ SetStream.prototype.onValidateComplete = function () { // no-op }; /** * @inheritdoc */ SetStream.prototype.onValidateFailed = function (err) { this.stream.setDelegate(new invalid_set_stream_1.InvalidSetStream(err, this.cache)); }; return SetStream; }()); exports.SetStream = SetStream;