diffusion
Version:
Diffusion JavaScript client
152 lines (151 loc) • 5.72 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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TopicsImpl = 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 log = logger.create('Session');
/**
* 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;
_this.internal = internal;
_this.UNSUBSCRIBE = internal.getServiceLocator().obtain(Services.UNSUBSCRIBE);
_this.SUBSCRIBE = internal.getServiceLocator().obtain(Services.SUBSCRIBE);
_this.streamRegistry = internal.getStreamRegistry();
return _this;
}
/**
* @inheritdoc
*/
TopicsImpl.prototype.unsubscribe = function (topic) {
var _this = this;
var selectors = [];
for (var _i = 1; _i < arguments.length; _i++) {
selectors[_i - 1] = arguments[_i];
}
var args = arguments;
var parseSelector = this.internal.getContext().parseSelector;
return new Promise(function (resolve, reject) {
try {
var selector = (args.length > 1)
? parseSelector(Array.prototype.slice.call(args))
: parseSelector(topic);
if (_this.internal.checkConnected(reject)) {
var unsubscribeCallback = function (err) {
if (err) {
log.debug('Unsubscribe failed', topic);
reject(err);
}
else {
log.debug('Unsubscribe complete', topic);
resolve();
}
};
log.debug('Unsubscribing', topic);
_this.UNSUBSCRIBE.send(selector, unsubscribeCallback);
}
}
catch (err) {
reject(err);
}
});
};
/**
* @inheritdoc
*/
TopicsImpl.prototype.fetchRequest = function () {
return new fetch_request_1.FetchRequestImpl(this.internal);
};
/**
* @inheritdoc
*/
TopicsImpl.prototype.select = function (topic) {
var _this = this;
var selectors = [];
for (var _i = 1; _i < arguments.length; _i++) {
selectors[_i - 1] = arguments[_i];
}
var args = arguments;
var parseSelector = this.internal.getContext().parseSelector;
return new Promise(function (resolve, reject) {
log.debug('Subscribing', topic);
try {
var selector = (args.length > 1)
? parseSelector(Array.prototype.slice.call(args))
: parseSelector(topic);
if (_this.internal.checkConnected(reject)) {
_this.SUBSCRIBE.send(selector, 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;