diffusion
Version:
Diffusion JavaScript client
548 lines (547 loc) • 23.2 kB
JavaScript
"use strict";
/**
* @module Session
*/
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 __());
};
})();
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.SessionImpl = void 0;
var errors_1 = require("./../../errors/errors");
var emitter_1 = require("./../events/emitter");
var client_control_1 = require("./../features/client-control");
var gateway_control_1 = require("./../features/gateway-control");
var log_retrieval_1 = require("./../features/log-retrieval");
var messages_1 = require("./../features/messages");
var metrics_1 = require("./../features/metrics");
var ping_1 = require("./../features/ping");
var remote_servers_1 = require("./../features/remote-servers");
var replace_license_1 = require("./../features/replace-license");
var security_1 = require("./../features/security");
var session_trees_1 = require("./../features/session-trees");
var time_series_1 = require("./../features/time-series/time-series");
var topic_control_1 = require("./../features/topic-control");
var topic_notifications_1 = require("./../features/topic-notifications");
var topic_update_1 = require("./../features/topic-update");
var topic_views_1 = require("./../features/topic-views");
var topics_1 = require("./../features/topics");
var options_impl_1 = require("./../session/options-impl");
var session_lock_options_1 = require("../../features/session-lock-options");
var core_1 = require("../features/core");
var environment_1 = require("../util/environment");
/**
* Immplementation of the {@link Session} interface
*/
var SessionImpl = /** @class */ (function (_super) {
__extends(SessionImpl, _super);
/**
* Create a new SessionImpl instance
*
* @param sessionID the session ID
* @param options the session options
* @param internalSession the internal session
*/ // eslint-disable-next-line complexity
function SessionImpl(sessionId, options, internalSession) {
var _this = this;
var factory = emitter_1.Emitter.create();
_this = _super.call(this, internalSession, factory) || this;
var emitter = factory.emitter(_this);
// re-emit internal session events
internalSession.on({
reconnect: function () {
emitter.emit('reconnect');
},
disconnect: function (reason) {
emitter.emit('disconnect', reason);
},
error: function (err) {
emitter.emit('error', err);
},
close: function (reason) {
emitter.emit('close', reason);
}
});
_this.sessionId = sessionId;
_this.core = new core_1.Core(internalSession);
// tslint:disable-next-line:strict-type-predicates
if (topic_control_1.TopicControlImpl !== undefined) {
_this.topicsFeature = new topic_control_1.TopicControlImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (client_control_1.ClientControlImpl !== undefined) {
_this.clientsFeature = new client_control_1.ClientControlImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (gateway_control_1.GatewayControlImpl !== undefined) {
_this.gatewaysFeature = new gateway_control_1.GatewayControlImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (messages_1.MessagesImpl !== undefined) {
_this.messagesFeature = new messages_1.MessagesImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (topic_notifications_1.TopicNotificationsImpl !== undefined) {
_this.notificationsFeature = new topic_notifications_1.TopicNotificationsImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (remote_servers_1.RemoteServersImpl !== undefined) {
_this.remoteServersFeature = new remote_servers_1.RemoteServersImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (security_1.SecurityImpl !== undefined) {
_this.securityFeature = new security_1.SecurityImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (session_trees_1.SessionTreesImpl !== undefined) {
_this.sessionTreesFeature = new session_trees_1.SessionTreesImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (time_series_1.TimeSeriesImpl !== undefined) {
_this.timeseriesFeature = new time_series_1.TimeSeriesImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (topic_update_1.TopicUpdateImpl !== undefined) {
_this.topicUpdateFeature = new topic_update_1.TopicUpdateImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (topic_views_1.TopicViewsImpl !== undefined) {
_this.topicViewsFeature = new topic_views_1.TopicViewsImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (log_retrieval_1.LogRetrieval !== undefined) {
_this.logRetrievalFeature = new log_retrieval_1.LogRetrieval(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (metrics_1.MetricsImpl !== undefined) {
_this.metricsFeature = new metrics_1.MetricsImpl(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (replace_license_1.LicenceUpdater !== undefined) {
_this.licenceUpdaterFeature = new replace_license_1.LicenceUpdater(internalSession);
}
// tslint:disable-next-line:strict-type-predicates
if (ping_1.PingImpl !== undefined) {
_this.pingFeature = new ping_1.PingImpl(internalSession);
}
// remove credentials to prevent 3rd party access
_this.options = options !== undefined
? options.with({
credentials: undefined
})
: {};
return _this;
}
Object.defineProperty(SessionImpl.prototype, "topics", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.topicsFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (topic_control_1.TopicControlImpl !== undefined) {
this.topicsFeature = new topic_control_1.TopicControlImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Topic Control feature has not been loaded');
}
}
return this.topicsFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "clients", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.clientsFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (client_control_1.ClientControlImpl !== undefined) {
this.clientsFeature = new client_control_1.ClientControlImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Client Control feature has not been loaded');
}
}
return this.clientsFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "messages", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.messagesFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (messages_1.MessagesImpl !== undefined) {
this.messagesFeature = new messages_1.MessagesImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Messages feature has not been loaded');
}
}
return this.messagesFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "notifications", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.notificationsFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (topic_notifications_1.TopicNotificationsImpl !== undefined) {
this.notificationsFeature = new topic_notifications_1.TopicNotificationsImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Topic Notifications feature has not been loaded');
}
}
return this.notificationsFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "security", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.securityFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (security_1.SecurityImpl !== undefined) {
this.securityFeature = new security_1.SecurityImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Security feature has not been loaded');
}
}
return this.securityFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "sessionTrees", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.sessionTreesFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (session_trees_1.SessionTreesImpl !== undefined) {
this.sessionTreesFeature = new session_trees_1.SessionTreesImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Session Trees feature has not been loaded');
}
}
return this.sessionTreesFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "timeseries", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.timeseriesFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (time_series_1.TimeSeriesImpl !== undefined) {
this.timeseriesFeature = new time_series_1.TimeSeriesImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Timeseries feature has not been loaded');
}
}
return this.timeseriesFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "topicUpdate", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.topicUpdateFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (topic_update_1.TopicUpdateImpl !== undefined) {
this.topicUpdateFeature = new topic_update_1.TopicUpdateImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Topic Update feature has not been loaded');
}
}
return this.topicUpdateFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "topicViews", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.topicViewsFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (topic_views_1.TopicViewsImpl !== undefined) {
this.topicViewsFeature = new topic_views_1.TopicViewsImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Topic Views feature has not been loaded');
}
}
return this.topicViewsFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "remoteServers", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.remoteServersFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (remote_servers_1.RemoteServersImpl !== undefined) {
this.remoteServersFeature = new remote_servers_1.RemoteServersImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Remote Servers feature has not been loaded');
}
}
return this.remoteServersFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "logRetrieval", {
/**
* Internal log retrieval capabilities via {@link SessionImpl.logRetrieval}
*/
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.logRetrievalFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (log_retrieval_1.LogRetrieval !== undefined) {
this.logRetrievalFeature = new log_retrieval_1.LogRetrieval(this.internal);
}
else {
throw new errors_1.IllegalStateError('Log Retrieval feature has not been loaded');
}
}
return this.logRetrievalFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "metrics", {
/**
* Internal metrics capabilities via {@link SessionImpl.metrics}
*/
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.metricsFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (metrics_1.MetricsImpl !== undefined) {
this.metricsFeature = new metrics_1.MetricsImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Metrics feature has not been loaded');
}
}
return this.metricsFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "licenceUpdater", {
/**
* The Licence updating service
*/
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.licenceUpdaterFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (replace_license_1.LicenceUpdater !== undefined) {
this.licenceUpdaterFeature = new replace_license_1.LicenceUpdater(this.internal);
}
else {
throw new errors_1.IllegalStateError('Licence Updater feature has not been loaded');
}
}
return this.licenceUpdaterFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "gateways", {
/**
* Internal gateway control capabilities via {@link SessionImpl.gateways}
*/
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.gatewaysFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (gateway_control_1.GatewayControlImpl !== undefined) {
this.gatewaysFeature = new gateway_control_1.GatewayControlImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Gateway Control feature has not been loaded');
}
}
return this.gatewaysFeature;
},
enumerable: false,
configurable: true
});
Object.defineProperty(SessionImpl.prototype, "ping", {
get: function () {
// tslint:disable-next-line:strict-type-predicates
if (this.pingFeature === undefined) {
// tslint:disable-next-line:strict-type-predicates
if (ping_1.PingImpl !== undefined) {
this.pingFeature = new ping_1.PingImpl(this.internal);
}
else {
throw new errors_1.IllegalStateError('Ping feature has not been loaded');
}
}
return this.pingFeature;
},
enumerable: false,
configurable: true
});
/**
* Updates the session with missing features that may have been loaded
* asynchronously. This method only has an effect when running the client
* as a modular client in the browser.
*
*/ // eslint-disable-next-line complexity
SessionImpl.prototype.updateFeatures = function () {
this.internal.updateFeatures();
};
/**
* @inheritdoc
*/
SessionImpl.prototype.close = function () {
this.internal.close();
return this;
};
/**
* @inheritdoc
*/
SessionImpl.prototype.closeSession = function () {
var _this = this;
if (this.closeReason !== undefined) {
return Promise.resolve(this.closeReason);
}
return new Promise(function (resolve, reject) {
_this.on('close', function (reason) {
_this.closeReason = reason;
resolve(reason);
});
_this.close();
});
};
/**
* @inheritdoc
*/
SessionImpl.prototype.isClosed = function () {
var state = this.internal.getState();
return state === 'closing' || state === 'closed';
};
/**
* @inheritdoc
*/
SessionImpl.prototype.isConnected = function () {
return this.internal.isConnected();
};
SessionImpl.prototype.toString = function () {
return "Session<" + this.sessionId + " " + this.internal.getState() + ">";
};
/**
* @inheritdoc
*/
SessionImpl.prototype.getPrincipal = function () {
return this.options.principal || '';
};
/**
* @inheritdoc
*/
SessionImpl.prototype.getSessionProperties = function () {
return this.core.getSessionProperties();
};
/**
* @inheritdoc
*/
SessionImpl.prototype.lock = function (lockName, scope) {
if (scope === void 0) { scope = session_lock_options_1.SessionLockScope.UNLOCK_ON_SESSION_LOSS; }
return this.internal.lock(lockName, scope);
};
/**
* @inheritdoc
*/
SessionImpl.prototype.pingServer = function () {
return this.ping.pingServer();
};
/**
* Get the server's maximum message size.
*
* <p>Not exposed through the API.
*/
SessionImpl.prototype.getServerMaximumMessageSize = function () {
return this.internal.getServerMaximumMessageSize();
};
/**
* Create a new session object
*
* @param internalSessionFactory a factory function for creating new internal sessions
* @param options the user supplied options, may be undefined when connecting to a shared sesssion
* @returns a result that resolves with the new session
* @throws an {@link IllegalArgumentError} if the options are invalid.
*/ // eslint-disable-next-line @typescript-eslint/member-ordering
SessionImpl.create = function (internalSessionFactory, options, connectToShared) {
var _a;
if (connectToShared === void 0) { connectToShared = false; }
// merge defaults if options argument is defined
var optionsImpl;
if (!connectToShared) {
optionsImpl = (typeof options === 'string')
? new options_impl_1.OptionsImpl({ host: options })
: new options_impl_1.OptionsImpl(options);
optionsImpl.properties = __assign(__assign({}, optionsImpl.properties), (_a = {}, _a['$Environment'] = environment_1.getEnvironmentString(), _a));
}
// connection promise
return new Promise(function (resolve, reject) {
var internalSession = internalSessionFactory(optionsImpl);
function onConnect(sessionId, opts) {
internalSession.off('connect', onConnect);
internalSession.off('close', onError);
internalSession.off('error', onError);
resolve(new SessionImpl(sessionId, opts || optionsImpl, internalSession));
}
function onError(err) {
internalSession.off('connect', onConnect);
internalSession.off('close', onError);
internalSession.off('error', onError);
reject(err);
}
internalSession.on('connect', onConnect);
internalSession.on('close', onError);
internalSession.on('error', onError);
internalSession.connect();
});
};
return SessionImpl;
}(topics_1.TopicsImpl));
exports.SessionImpl = SessionImpl;