diffusion
Version:
Diffusion JavaScript client
154 lines (153 loc) • 5.82 kB
JavaScript
"use strict";
/**
* @module TopicUpdate
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PendingSetStream = 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");
var uint8array_1 = require("../util/uint8array");
/**
* An update stream delegate that is used when a {@link set} operation is called
* on a new stream. The server has not yet responded to the initial set
* operation.
*/
var PendingSetStream = /** @class */ (function () {
/**
* Create a new PendingSetStream 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 value the value of the pending set operation
*/
function PendingSetStream(streamServices, options, stream, pendingResult, value) {
this.streamServices = streamServices;
this.options = options;
this.stream = stream;
this.pendingResult = pendingResult;
this.deferredResolvers = [];
this.value = value;
this.nextValue = value;
}
/**
* @inheritdoc
*/
PendingSetStream.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
*/
PendingSetStream.prototype.get = function () {
return this.nextValue;
};
/**
* @inheritdoc
*/
PendingSetStream.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
*/
PendingSetStream.prototype.onSetComplete = 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);
}
};
PendingSetStream.prototype.processPendingUpdatesRetainsValue = function (streamId) {
var _this = this;
var buffer = this.options.dataType.toBytes(this.value).asArray();
var nextBuffer = this.options.dataType.toBytes(this.nextValue).asArray();
if (!uint8array_1.isEqualUint8Arrays(buffer, nextBuffer)) {
var diffBuffer = this.options.toBuffer(this.value, this.nextValue);
this.options.deltaService.send(new update_stream_request_1.UpdateStreamRequest(streamId, diffBuffer), function (err) {
if (err) {
_this.deferredResolvers.forEach(function (resolver) {
resolver.reject(err);
});
}
else {
_this.deferredResolvers.forEach(function (resolver) {
resolver.resolve();
});
}
});
}
else {
this.deferredResolvers.forEach(function (resolver) {
resolver.resolve();
});
}
};
PendingSetStream.prototype.processPendingUpdatesDoesntRetainValue = function (streamId) {
var _this = this;
var value = this.value;
this.deferredResolvers.forEach(function (resolver) {
var diffBuffer = _this.options.toBuffer(value, resolver.value);
value = resolver.value;
_this.options.deltaService.send(new update_stream_request_1.UpdateStreamRequest(streamId, diffBuffer), function (err) {
if (err) {
resolver.reject(err);
}
else {
resolver.resolve();
}
});
});
};
/**
* @inheritdoc
*/
PendingSetStream.prototype.onSetFailed = function (err) {
this.stream.setDelegate(new invalid_set_stream_1.InvalidSetStream(err, this.nextValue));
this.deferredResolvers.forEach(function (resolver) {
resolver.reject(err);
});
};
/**
* @inheritdoc
*/
PendingSetStream.prototype.onValidateComplete = function () {
throw new errors_1.IllegalStateError('No validate request has been sent');
};
/**
* @inheritdoc
*/
PendingSetStream.prototype.onValidateFailed = function () {
throw new errors_1.IllegalStateError('No validate request has been sent');
};
return PendingSetStream;
}());
exports.PendingSetStream = PendingSetStream;