diffusion
Version:
Diffusion JavaScript client
143 lines (142 loc) • 5.67 kB
JavaScript
;
/**
* @module TopicUpdate
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PendingValidateAndSetStream = void 0;
var errors_1 = require("./../../errors/errors");
var update_stream_request_1 = require("./../services/topic-update/update-stream-request");
var invalid_set_stream_1 = require("./../topic-update/invalid-set-stream");
var set_stream_1 = require("./../topic-update/set-stream");
var topic_update_1 = require("../../topic-update/topic-update");
/**
* An update stream delegate that is used when first a {@link validate}
* operation and then a {@link set} operation is called on a new stream. The
* server has not yet responded to the initial validate operation.
*/
var PendingValidateAndSetStream = /** @class */ (function () {
/**
* Create a new PendingValidateAndSetStream instance
*
* @param streamServices the stream services
* @param options the update stream options
* @param stream the stream that using this stream as a delegate
* @param pendingResult the pending result
* @param deferredEmitter the deferred emitter that should be emitted once
* the pending result completes
* @param value the value of the pending set operation
*/
function PendingValidateAndSetStream(streamServices, options, stream, pendingResult, deferredEmitter, value) {
this.streamServices = streamServices;
this.options = options;
this.stream = stream;
this.pendingResult = pendingResult;
this.deferredResolvers = [deferredEmitter];
this.nextValue = value;
}
/**
* @inheritdoc
*/
PendingValidateAndSetStream.prototype.set = function (value) {
var _this = this;
this.options.checkOperation(value);
this.nextValue = value;
return new Promise(function (resolve, reject) {
_this.deferredResolvers.push({ value: value, resolve: resolve, reject: reject });
});
};
/**
* @inheritdoc
*/
PendingValidateAndSetStream.prototype.get = function () {
return this.nextValue;
};
/**
* @inheritdoc
*/
PendingValidateAndSetStream.prototype.validate = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.pendingResult.then(function (r) {
resolve(topic_update_1.TopicCreationResult.EXISTS);
return r;
}, function (err) {
reject(err);
});
});
};
/**
* @inheritdoc
*/
PendingValidateAndSetStream.prototype.onSetComplete = function () {
throw new errors_1.IllegalStateError('No set request has been sent');
};
/**
* @inheritdoc
*/
PendingValidateAndSetStream.prototype.onSetFailed = function () {
throw new errors_1.IllegalStateError('No set request has been sent');
};
/**
* @inheritdoc
*/
PendingValidateAndSetStream.prototype.onValidateComplete = function (streamId, retainsValue, supportsConflation) {
var _this = this;
this.options.retainsValue = retainsValue;
this.options.supportsConflation = supportsConflation;
if (!retainsValue) {
this.options.toBuffer = function (oldValue, newValue) {
return _this.options.dataType.toBytes(newValue).asArray();
};
this.options.deltaService = this.streamServices.STREAM_SET_TOPIC_SERVICE;
}
this.stream.setDelegate(new set_stream_1.SetStream(this.streamServices, this.options, this.stream, streamId, this.nextValue));
if (retainsValue && supportsConflation) {
this.processPendingUpdatesRetainsValue(streamId);
}
else {
this.processPendingUpdatesDoesntRetainValue(streamId);
}
};
PendingValidateAndSetStream.prototype.processPendingUpdatesRetainsValue = function (streamId) {
var _this = this;
var buffer = this.options.dataType.toBytes(this.nextValue).asArray();
this.streamServices.STREAM_SET_TOPIC_SERVICE.send(new update_stream_request_1.UpdateStreamRequest(streamId, buffer), function (err) {
if (err) {
_this.deferredResolvers.forEach(function (resolver) {
resolver.reject(err);
});
}
else {
_this.deferredResolvers.forEach(function (resolver) {
resolver.resolve();
});
}
});
};
PendingValidateAndSetStream.prototype.processPendingUpdatesDoesntRetainValue = function (streamId) {
var _this = this;
this.deferredResolvers.forEach(function (resolver) {
var buffer = _this.options.dataType.toBytes(resolver.value).asArray();
_this.streamServices.STREAM_SET_TOPIC_SERVICE.send(new update_stream_request_1.UpdateStreamRequest(streamId, buffer), function (err) {
if (err) {
resolver.reject(err);
}
else {
resolver.resolve();
}
});
});
};
/**
* @inheritdoc
*/
PendingValidateAndSetStream.prototype.onValidateFailed = function (err) {
this.stream.setDelegate(new invalid_set_stream_1.InvalidSetStream(err, this.nextValue));
this.deferredResolvers.forEach(function (resolver) {
resolver.reject(err);
});
};
return PendingValidateAndSetStream;
}());
exports.PendingValidateAndSetStream = PendingValidateAndSetStream;