UNPKG

diffusion

Version:

Diffusion JavaScript client

300 lines (299 loc) 12.8 kB
"use strict"; /** * @module FetchRequest */ var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.FetchRequestImpl = exports.FetchRange = exports.Limit = void 0; var errors_1 = require("./../../../errors/errors"); var require_non_null_1 = require("./../../../internal/util/require-non-null"); var fetch_request_1 = require("./../../../topics/fetch-request"); var topic_type_1 = require("./../../../topics/topic-type"); var any_datatype_1 = require("./../../data/any-datatype"); var fetch_result_1 = require("./../../features/topics/fetch-result"); var fetch_query_1 = require("./../../services/fetch-query/fetch-query"); var Services = require("./../../services/topics-services"); var logger = require("./../../util/logger"); var math_1 = require("./../../util/math"); var response_success_1 = require("./../../util/response-success"); var log = logger.create('Fetch Request'); /** * A fetch range limit */ var Limit = /** @class */ (function () { /** * Create a new Limit instance * * @param path the limiting path * @param includesPath a flag indicating whether the limiting path is * included in the range */ function Limit(path, includesPath) { this.path = path; this.includesPath = includesPath; } return Limit; }()); exports.Limit = Limit; /** * A fetch request topic range */ var FetchRange = /** @class */ (function () { /** * Create a new FetchRange instance * * @param from the lower limit of the range * @param to the upper limit of the range */ function FetchRange(from, to) { this.from = from; this.to = to; } /** * Check if the range is unbounded * * @return `true` if no limits are set */ FetchRange.prototype.isUnbounded = function () { return this.from === undefined && this.to === undefined; }; return FetchRange; }()); exports.FetchRange = FetchRange; /** * Indicates whether a specified topic type can be read as a specified * data type. * * @param topicType the topic type * @param dataType the data type * * @returns true if the topic type can be read as the data type */ function canReadAs(dataTypes, topicType, dataType) { if (topicType === topic_type_1.TopicType.TIME_SERIES) { return fetch_result_1.TIME_SERIES_DATA_TYPE.canReadAs(dataType); } else { var topicDataType = dataTypes.getByValue(topicType); // tslint:disable-next-line:strict-type-predicates return topicDataType !== null && dataType.valueClass !== undefined && topicDataType.canReadAs(dataType.valueClass); } } /** * Implementation of the {@link FetchRequest} builder interface */ var FetchRequestImpl = /** @class */ (function (_super) { __extends(FetchRequestImpl, _super); /** * Create a new FetchRequestImpl instance * * @param internal the internal session * @param opts the internal options specifying what to fetch */ function FetchRequestImpl(internal, opts) { var _this = _super.call(this) || this; var defaultOptions = { range: new FetchRange(), topicTypes: fetch_request_1.FetchRequest.getAllTypes(), dataType: undefined, withProperties: false, limit: math_1.MAX_INT32, maximumResultSize: internal.getOptions().maxMessageSize, deepBranchDepth: math_1.MAX_INT32, deepBranchLimit: math_1.MAX_INT32, withUnpublishedDelayedTopics: false, withSizes: false }; _this.internal = internal; _this.options = Object.assign(defaultOptions, opts); return _this; } /** * Deep copy the options into a new {@link FetchRequestOptions} instance * * @return the new options instance */ FetchRequestImpl.prototype.copyOptions = function () { return { range: new FetchRange(this.options.range.from, this.options.range.to), topicTypes: new Set(this.options.topicTypes), dataType: this.options.dataType, withProperties: this.options.withProperties, limit: this.options.limit, maximumResultSize: this.options.maximumResultSize, deepBranchDepth: this.options.deepBranchDepth, deepBranchLimit: this.options.deepBranchLimit, withUnpublishedDelayedTopics: this.options.withUnpublishedDelayedTopics, withSizes: this.options.withSizes }; }; /** * @inheritdoc */ FetchRequestImpl.prototype.from = function (topicPath) { var limit = new Limit(topicPath, true); return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { range: new FetchRange(limit, this.options.range.to) })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.after = function (topicPath) { var limit = new Limit(topicPath, false); return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { range: new FetchRange(limit, this.options.range.to) })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.to = function (topicPath) { var limit = new Limit(topicPath, true); return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { range: new FetchRange(this.options.range.from, limit) })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.before = function (topicPath) { var limit = new Limit(topicPath, false); return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { range: new FetchRange(this.options.range.from, limit) })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.topicTypes = function (topicTypes) { var _this = this; if (Array.isArray(topicTypes)) { topicTypes = new Set(topicTypes); } if (topicTypes.size === 0) { throw new errors_1.IllegalArgumentError('No types specified'); } var allTypes = fetch_request_1.FetchRequest.getAllTypes(); var context = this.internal.getContext(); // ensure that those supplied are valid and if there is a value // class that the types can be read as the value class. topicTypes.forEach(function (topicType) { if (!allTypes.has(topicType)) { // eslint-disable-next-line deprecation/deprecation throw new errors_1.IllegalArgumentError("Invalid topic type " + topicType); } if (_this.options.dataType !== undefined && !canReadAs(context.DataTypes, topicType, _this.options.dataType) && topicType !== context.TopicType.TIME_SERIES) { // eslint-disable-next-line deprecation/deprecation throw new errors_1.IllegalArgumentError("Topic type " + topicType + " cannot be read as " + _this.options.dataType); } }); return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { topicTypes: topicTypes })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.withValues = function (dataType) { var _this = this; var selectedTopicTypes; if (dataType !== undefined && !(dataType instanceof any_datatype_1.AnyDataTypeImpl)) { selectedTopicTypes = new Set(this.options.topicTypes); this.options.topicTypes.forEach(function (topicType) { if (!canReadAs(_this.internal.getContext().DataTypes, topicType, dataType)) { selectedTopicTypes.delete(topicType); } }); if (selectedTopicTypes.size === 0) { throw new errors_1.IllegalArgumentError("No selected topic types can be read as " + dataType); } } else { selectedTopicTypes = this.options.topicTypes; } return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { topicTypes: selectedTopicTypes, dataType: dataType })); }; FetchRequestImpl.prototype.withSizes = function () { return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { withSizes: true })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.withProperties = function () { return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { withProperties: true })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.first = function (count) { return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { limit: require_non_null_1.requireNonNegativeInt(count, 'count') })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.last = function (count) { return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { limit: 0 - require_non_null_1.requireNonNegativeInt(count, 'count') })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.maximumResultSize = function (maximumSize) { return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { maximumResultSize: Math.min(require_non_null_1.requireNonNegativeInt(maximumSize, 'maximumSize'), this.internal.getOptions().maxMessageSize) })); }; FetchRequestImpl.prototype.limitDeepBranches = function (deepBranchDepth, deepBranchLimit) { return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { deepBranchDepth: require_non_null_1.requireNonNegativeInt(deepBranchDepth, 'deepBranchDepth'), deepBranchLimit: require_non_null_1.requireNonNegativeInt(deepBranchLimit, 'deepBranchLimit') })); }; FetchRequestImpl.prototype.withUnpublishedDelayedTopics = function () { return new FetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { withUnpublishedDelayedTopics: true })); }; /** * @inheritdoc */ FetchRequestImpl.prototype.fetch = function (topics) { var _this = this; var extraTopics = []; for (var _i = 1; _i < arguments.length; _i++) { extraTopics[_i - 1] = arguments[_i]; } var parseSelector = this.internal.getContext().parseSelector; var serviceLocator = this.internal.getServiceLocator(); var FETCH_QUERY = serviceLocator.obtain(Services.FETCH_QUERY); var selector = (arguments.length > 1) ? parseSelector(Array.prototype.slice.call(arguments)) : parseSelector(topics); var query = new fetch_query_1.FetchQuery(selector, this.options.range, this.options.topicTypes, this.options.dataType !== undefined, this.options.withProperties, this.options.limit, this.options.maximumResultSize, this.options.deepBranchDepth, this.options.deepBranchLimit, this.options.withUnpublishedDelayedTopics, this.options.withSizes); return new Promise(function (resolve, reject) { FETCH_QUERY.send(query, function (err, fetchQueryResult) { var _a, _b; if (!response_success_1.responseSuccess(err, fetchQueryResult)) { log.debug('Fetch query failed'); reject(new errors_1.RuntimeError((_b = (_a = err.reason) !== null && _a !== void 0 ? _a : err.message) !== null && _b !== void 0 ? _b : 'An error occurred', err)); } else { try { var fetchResult = new fetch_result_1.FetchResultImpl(_this.options.dataType, fetchQueryResult, _this.internal.getContext()); resolve(fetchResult); } catch (err) { reject(err); } } }); }); }; return FetchRequestImpl; }(fetch_request_1.FetchRequest)); exports.FetchRequestImpl = FetchRequestImpl;