diffusion
Version:
Diffusion JavaScript client
170 lines (169 loc) • 6.9 kB
JavaScript
"use strict";
/**
* @module SessionFetchRequest
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionFetchRequestImpl = exports.SessionFetchRange = void 0;
var errors_1 = require("./../../../errors/errors");
var session_fetch_result_1 = require("./../../features/clients/session-fetch-result");
var Services = require("./../../services/client-control-services");
var session_fetch_query_1 = require("./../../services/session-fetch-query/session-fetch-query");
var logger = require("./../../util/logger");
var math_1 = require("./../../util/math");
var response_success_1 = require("./../../util/response-success");
var Long = require("long");
var client_control_options_1 = require("../../../features/client-control-options");
var log = logger.create('Session Fetch Request');
/**
* A session fetch request range
*/
var SessionFetchRange = /** @class */ (function () {
/**
* Create a new SessionFetchRange instance
*
* @param from the lower limit of the range
* @param to the upper limit of the range
*/
function SessionFetchRange(from, to) {
this.from = from;
this.to = to;
}
/**
* Check if the range is unbounded
*
* @return `true` if no limits are set
*/
SessionFetchRange.prototype.isUnbounded = function () {
return this.from === undefined && this.to === undefined;
};
return SessionFetchRange;
}());
exports.SessionFetchRange = SessionFetchRange;
function requireNonNegative(n, parameter) {
if (!Number.isInteger(n) || n < 0) {
throw new errors_1.IllegalArgumentError("Invalid argument '" + parameter + "': " + n);
}
return n;
}
function requireNonNegativeLong(n, parameter) {
if (n instanceof Long && n.gte(0)) {
return n;
}
else if (typeof n === 'number' && Number.isInteger(n) && n >= 0) {
return Long.fromNumber(n);
}
throw new errors_1.IllegalArgumentError("Invalid argument '" + parameter + "': " + n);
}
/**
* Implementation of the {@link SessionFetchRequest} builder interface
*/
var SessionFetchRequestImpl = /** @class */ (function () {
/**
* Create a new {@link SessionFetchRequestImpl} instance
*
* @param internal the internal session
* @param opts the internal options specifying what to fetch
*/
function SessionFetchRequestImpl(internal, opts) {
var defaultOptions = {
range: new SessionFetchRange(),
filter: 'all',
withProperties: null,
limit: math_1.MAX_INT32,
maximumResultSize: internal.getOptions().maxMessageSize
};
this.internal = internal;
this.options = Object.assign(defaultOptions, opts);
}
/**
* Deep copy the options into a new {@link SessionFetchRequestOptions} instance
*
* @return the new options instance
*/
SessionFetchRequestImpl.prototype.copyOptions = function () {
return {
range: new SessionFetchRange(this.options.range.from, this.options.range.to),
filter: this.options.filter,
withProperties: this.options.withProperties,
limit: this.options.limit,
maximumResultSize: this.options.maximumResultSize
};
};
/**
* @inheritdoc
*/
SessionFetchRequestImpl.prototype.from = function (time) {
var limit = requireNonNegativeLong(time, 'time');
return new SessionFetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { range: new SessionFetchRange(limit, this.options.range.to) }));
};
/**
* @inheritdoc
*/
SessionFetchRequestImpl.prototype.to = function (time) {
var limit = requireNonNegativeLong(time, 'time');
return new SessionFetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { range: new SessionFetchRange(this.options.range.from, limit) }));
};
/**
* @inheritdoc
*/
SessionFetchRequestImpl.prototype.withProperties = function (properties) {
return new SessionFetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { withProperties: properties }));
};
/**
* @inheritdoc
*/
SessionFetchRequestImpl.prototype.first = function (count) {
return new SessionFetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { limit: requireNonNegative(count, 'count') }));
};
/**
* @inheritdoc
*/
SessionFetchRequestImpl.prototype.last = function (count) {
return new SessionFetchRequestImpl(this.internal, Object.assign(this.copyOptions(), { limit: 0 - requireNonNegative(count, 'count') }));
};
/**
* @inheritdoc
*/
SessionFetchRequestImpl.prototype.maximumResultSize = function (maximumSize) {
return new SessionFetchRequestImpl(this.internal, Object.assign(this.copyOptions(), {
maximumResultSize: Math.min(requireNonNegative(maximumSize, 'maximumSize'), this.internal.getOptions().maxMessageSize)
}));
};
/**
* @inheritdoc
*/
SessionFetchRequestImpl.prototype.filter = function (filter) {
return new SessionFetchRequestImpl(this.internal, Object.assign(this.copyOptions(), {
filter: filter
}));
};
/**
* @inheritdoc
*/
SessionFetchRequestImpl.prototype.fetch = function () {
var serviceLocator = this.internal.getServiceLocator();
var SESSION_FETCH = serviceLocator.obtain(Services.SESSION_FETCH);
var propertySet = this.options.withProperties && new Set(this.options.withProperties);
var requiresStartTime = !propertySet
|| propertySet.has(client_control_options_1.ClientControlOptions.PropertyKeys.START_TIME)
|| propertySet.has(client_control_options_1.ClientControlOptions.PropertyKeys.ALL_FIXED_PROPERTIES[0]);
var query = new session_fetch_query_1.SessionFetchQuery(this.options.filter, this.options.range, propertySet, this.options.limit, this.options.maximumResultSize, requiresStartTime);
return new Promise(function (resolve, reject) {
SESSION_FETCH.send(query, function (err, fetchQueryResult) {
if (!response_success_1.responseSuccess(err, fetchQueryResult)) {
log.debug('Session fetch query failed');
reject(err);
}
else if (fetchQueryResult.errors) {
log.debug('Session fetch query failed');
reject(fetchQueryResult.errors.errors);
}
else {
resolve(new session_fetch_result_1.SessionFetchResultImpl(fetchQueryResult.results || [], fetchQueryResult.totalCount || 0));
}
});
});
};
return SessionFetchRequestImpl;
}());
exports.SessionFetchRequestImpl = SessionFetchRequestImpl;