UNPKG

diffusion

Version:

Diffusion JavaScript client

140 lines (139 loc) 5.53 kB
"use strict"; /** * @module TopicUpdate */ var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateStreamBuilderImpl = void 0; var errors_1 = require("./../../errors/errors"); var options_impl_1 = require("./../session/options-impl"); var math_1 = require("./../util/math"); var require_non_null_1 = require("./../util/require-non-null"); var recoverable_update_stream_1 = require("./recoverable-update-stream"); var update_constraint_1 = require("./update-constraint"); var DEFAULT_SPECIFICATION = null; var DEFAULT_CONSTRAINT = new update_constraint_1.Unconstrained(); var DEFAULT_SUPPRESS_DELTAS = false; /** * Implementation of {@link UpdateStreamBuilder}. */ var UpdateStreamBuilderImpl = /** @class */ (function () { /** * Constructor. */ function UpdateStreamBuilderImpl(context, updateStreamFactory) { /** * The optional topic specification for the update stream */ this.topicSpecification = DEFAULT_SPECIFICATION; /** * The optional topic constraint for the update stream */ this.topicConstraint = DEFAULT_CONSTRAINT; /** * Flag indicating whether deltas should be suppressed */ this.theSuppressDeltas = DEFAULT_SUPPRESS_DELTAS; this.context = context; this.updateStreamFactory = updateStreamFactory; } /** * Check if the supplied topic type is supported * * @param type the topic type or specification * @return `true` if the type is one of the supported topic types */ UpdateStreamBuilderImpl.prototype.isSupportedTopicType = function (type) { // eslint-disable-next-line @typescript-eslint/naming-convention var TopicType = this.context.TopicType; return Object.keys(TopicType).some(function (t) { return (TopicType[t] === type); }); }; /** * @inheritdoc */ UpdateStreamBuilderImpl.prototype.specification = function (topicSpecification) { if (topicSpecification === null || topicSpecification === undefined) { this.topicSpecification = null; } else if (this.isSupportedTopicType(topicSpecification)) { this.topicSpecification = new this.context.TopicSpecification(topicSpecification); } else { this.topicSpecification = topicSpecification; } return this; }; /** * @inheritdoc */ UpdateStreamBuilderImpl.prototype.constraint = function (updateConstraint) { this.topicConstraint = updateConstraint === null || updateConstraint === undefined ? DEFAULT_CONSTRAINT : updateConstraint; return this; }; /** * @inheritdoc */ UpdateStreamBuilderImpl.prototype.suppressDeltas = function (suppressDeltas) { this.theSuppressDeltas = suppressDeltas; return this; }; /** * @inheritdoc */ UpdateStreamBuilderImpl.prototype.reset = function () { this.topicSpecification = DEFAULT_SPECIFICATION; this.topicConstraint = DEFAULT_CONSTRAINT; this.theSuppressDeltas = DEFAULT_SUPPRESS_DELTAS; return this; }; UpdateStreamBuilderImpl.prototype.build = function (path, dataType, retryStrategy) { var _this = this; require_non_null_1.requireNonNull(path, 'path'); require_non_null_1.requireNonNull(dataType, 'dataType'); var updateStreamSupplier = function () { if (_this.topicSpecification !== null) { return _this.updateStreamFactory.createUpdateStreamThatAddsTopic(path, _this.topicSpecification, dataType, _this.topicConstraint, _this.theSuppressDeltas); } else { return _this.updateStreamFactory.createUpdateStream(path, dataType, _this.topicConstraint, _this.theSuppressDeltas); } }; if (retryStrategy === undefined) { return updateStreamSupplier(); } var strategy = options_impl_1.DEFAULT_RETRY_STRATEGY; if (retryStrategy.attempts !== undefined) { require_non_null_1.requireNonNull(retryStrategy.interval, 'retry strategy interval'); if (retryStrategy.interval < 1) { throw new errors_1.IllegalArgumentError('Invalid retry strategy interval.'); } if (retryStrategy.attempts < 1) { throw new errors_1.IllegalArgumentError('Invalid retry strategy attempts.'); } strategy = __assign({}, retryStrategy); } else if (retryStrategy.interval !== undefined) { if (retryStrategy.interval < 1) { throw new errors_1.IllegalArgumentError('Invalid retry strategy interval.'); } strategy = { interval: retryStrategy.interval, attempts: math_1.MAX_SAFE_INTEGER }; } return new recoverable_update_stream_1.RecoverableUpdateStreamImpl(updateStreamSupplier, strategy); }; return UpdateStreamBuilderImpl; }()); exports.UpdateStreamBuilderImpl = UpdateStreamBuilderImpl;