diffusion
Version:
Diffusion JavaScript client
234 lines (233 loc) • 8.99 kB
JavaScript
;
/**
* @module Topics
*/
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 __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TopicsImpl = exports.DEFAULT_TOPIC_SELECTION_SCOPE = void 0;
var errors_1 = require("./../../errors/errors");
var datatypes_1 = require("./../data/datatypes");
var stream_1 = require("./../events/stream");
var fetch_request_1 = require("./../features/topics/fetch-request");
var ValueStreamProxy = require("./../features/topics/value-stream-proxy");
var Services = require("./../services/topics-services");
var logger = require("./../util/logger");
var require_non_null_1 = require("../util/require-non-null");
var validate_selection_scope_1 = require("../util/validate-selection-scope");
var log = logger.create('Session');
/**
* The default topic selection scope.
*/
exports.DEFAULT_TOPIC_SELECTION_SCOPE = '';
/**
* Implementation of the {@link Topics} feature
*
* {@link TopicsImpl} extends {@link StreamImpl} so that {@link SessionImpl}
* can extend directly from {@link TopicsImpl} without the need for multiple
* inheritance.
*/
var TopicsImpl = /** @class */ (function (_super) {
__extends(TopicsImpl, _super);
/**
* Create a new TopicsImpl instance
*
* @param internal the internal session
*/
function TopicsImpl(internal, helper) {
var _this = _super.call(this, helper) || this;
/**
* @inheritdoc
*/
_this.DEFAULT_SELECTION_SCOPE = exports.DEFAULT_TOPIC_SELECTION_SCOPE;
_this.internal = internal;
_this.UNSUBSCRIBE = internal.getServiceLocator().obtain(Services.UNSUBSCRIBE);
_this.UNSUBSCRIBE_ALL = internal.getServiceLocator().obtain(Services.UNSUBSCRIBE_ALL);
_this.SUBSCRIBE = internal.getServiceLocator().obtain(Services.SUBSCRIBE);
_this.streamRegistry = internal.getStreamRegistry();
return _this;
}
/**
* @inheritdoc
*/
TopicsImpl.prototype.unsubscribe = function (topic) {
var selectors = [];
for (var _i = 1; _i < arguments.length; _i++) {
selectors[_i - 1] = arguments[_i];
}
var args = arguments;
var selectorArray = (args.length > 1)
? __spreadArray([], __read(args)) : topic;
return this.unsubscribeWithScope(selectorArray, this.DEFAULT_SELECTION_SCOPE);
};
TopicsImpl.prototype.unsubscribeWithScope = function (selector, scope) {
var _this = this;
if (scope === void 0) { scope = this.DEFAULT_SELECTION_SCOPE; }
var parseSelector = this.internal.getContext().parseSelector;
return new Promise(function (resolve, reject) {
require_non_null_1.requireNonNull(selector, 'selector');
validate_selection_scope_1.validateSelectionScope(scope);
try {
var topicSelector = parseSelector(selector);
if (_this.internal.checkConnected(reject)) {
var unsubscribeCallback = function (err) {
if (err) {
log.debug('Unsubscribe failed', selector);
reject(err);
}
else {
log.debug('Unsubscribe complete', selector);
resolve();
}
};
log.debug('Unsubscribing', selector);
_this.UNSUBSCRIBE.send({
selector: topicSelector,
scope: scope
}, unsubscribeCallback);
}
}
catch (err) {
reject(err);
}
});
};
/**
* @inheritdoc
*/
TopicsImpl.prototype.unsubscribeAllScopes = function (selector) {
var _this = this;
require_non_null_1.requireNonNull(selector, 'selector');
var parseSelector = this.internal.getContext().parseSelector;
return new Promise(function (resolve, reject) {
try {
var topicSelector = parseSelector(selector);
if (_this.internal.checkConnected(reject)) {
log.debug('Unsubscribing from all scopes', selector);
_this.UNSUBSCRIBE_ALL.send(topicSelector, function (err) {
if (err) {
log.debug('Unsubscribe failed', selector);
reject(err);
}
else {
log.debug('Unsubscribe complete', selector);
resolve();
}
});
}
}
catch (err) {
reject(err);
}
});
};
/**
* @inheritdoc
*/
TopicsImpl.prototype.fetchRequest = function () {
return new fetch_request_1.FetchRequestImpl(this.internal);
};
/**
* @inheritdoc
*/
TopicsImpl.prototype.select = function (topic) {
var selectors = [];
for (var _i = 1; _i < arguments.length; _i++) {
selectors[_i - 1] = arguments[_i];
}
var args = arguments;
var selectorArray = (args.length > 1)
? __spreadArray([], __read(args)) : topic;
return this.selectWithScope(selectorArray, this.DEFAULT_SELECTION_SCOPE);
};
/**
* @inheritdoc
*/
TopicsImpl.prototype.selectWithScope = function (selector, scope) {
var _this = this;
if (scope === void 0) { scope = this.DEFAULT_SELECTION_SCOPE; }
var parseSelector = this.internal.getContext().parseSelector;
return new Promise(function (resolve, reject) {
require_non_null_1.requireNonNull(selector, 'selector');
validate_selection_scope_1.validateSelectionScope(scope);
log.debug('Subscribing', selector);
try {
var topicSelector = parseSelector(selector);
if (_this.internal.checkConnected(reject)) {
_this.SUBSCRIBE.send({
selector: topicSelector,
scope: scope
}, function (err) {
if (!err) {
resolve();
}
else {
reject(err);
}
});
}
}
catch (err) {
reject(err);
}
});
};
/**
* @inheritdoc
*/
TopicsImpl.prototype.addStream = function (topic, datatype) {
if (arguments.length !== 2) {
throw new errors_1.IllegalArgumentError('Both topic selector and value type are needed');
}
if (!datatype || datatypes_1.DataTypes.get(datatype)) {
throw new errors_1.IllegalArgumentError('Data type required');
}
var selector = this.internal.getContext().parseSelector(topic);
return ValueStreamProxy.create(this.streamRegistry, datatype, false, selector);
};
/**
* @inheritdoc
*/
TopicsImpl.prototype.addFallbackStream = function (datatype) {
if (!datatype || datatypes_1.DataTypes.get(datatype)) {
throw new errors_1.IllegalArgumentError('Data type required');
}
return ValueStreamProxy.create(this.streamRegistry, datatype, true);
};
return TopicsImpl;
}(stream_1.StreamImpl));
exports.TopicsImpl = TopicsImpl;