diffusion
Version:
Diffusion JavaScript client
204 lines (203 loc) • 8.89 kB
JavaScript
;
/**
* @module TopicControl
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.TopicControlImpl = void 0;
var errors_1 = require("./../../errors/errors");
var CommandService = require("./../client/command-service");
var control_group_1 = require("./../control/control-group");
var registration_1 = require("./../control/registration");
var remove_topic_1 = require("./../services/remove-topic/remove-topic");
var topic_add_response_1 = require("./../services/topic-add/topic-add-response");
var Services = require("./../services/topic-control-services");
var session_id_1 = require("./../session/session-id");
var logger = require("./../util/logger");
var error_reason_1 = require("../../errors/error-reason");
var log = logger.create('Session.Topics');
/**
* Implementation of the {@link TopicControl} feature
*/
var TopicControlImpl = /** @class */ (function () {
/**
* Create a new TopicControlImpl instance
*
* @param internal the internal session
*/
function TopicControlImpl(internal) {
this.internal = internal;
var serviceLocator = internal.getServiceLocator();
this.TOPIC_ADD_SERVICE = serviceLocator.obtain(Services.TOPIC_ADD);
this.TOPIC_REMOVAL = serviceLocator.obtain(Services.TOPIC_REMOVAL);
// eslint-disable-next-line @typescript-eslint/naming-convention
var TopicAddFailReasonEnum = internal.getContext().TopicAddFailReasonEnum;
// see ErrorReasonException.java
this.errorReasonIdToAddFailReason = {
103: TopicAddFailReasonEnum.PERMISSIONS_FAILURE,
110: TopicAddFailReasonEnum.CLUSTER_REPARTITION,
9004: TopicAddFailReasonEnum.EXISTS_MISMATCH,
9005: TopicAddFailReasonEnum.INVALID_NAME,
9006: TopicAddFailReasonEnum.INVALID_DETAILS,
9007: TopicAddFailReasonEnum.EXCEEDED_LICENSE_LIMIT,
9010: TopicAddFailReasonEnum.EXISTS_INCOMPATIBLE,
9011: TopicAddFailReasonEnum.UNEXPECTED_ERROR
};
// Updater state service
var serviceRegistry = internal.getServiceRegistry();
serviceRegistry.add(Services.MISSING_TOPIC, CommandService.create(function (internalSession, request, callback) {
callback.respond(true);
internalSession.getConversationSet().respondIfPresent(request.cid, {
request: request,
callback: callback
});
}));
}
/**
* Check if the supplied topic type is supported
*
* @param type the topic type or specification
* @return `true` if the type is one of the supported topic types
*/
TopicControlImpl.prototype.isSupportedTopicType = function (type) {
// eslint-disable-next-line @typescript-eslint/naming-convention
var TopicTypeEnum = this.internal.getContext().TopicTypeEnum;
return Object.keys(TopicTypeEnum).some(function (t) { return (TopicTypeEnum[t] === type); });
};
/**
* @inheritdoc
*/
TopicControlImpl.prototype.add = function (path, specification) {
var _this = this;
var topicSpecificationType = this.internal.getContext().TopicSpecification;
return new Promise(function (resolve, reject) {
if (!path) {
reject({
id: 0,
reason: 'Path cannot be empty or null'
});
return;
}
if (_this.internal.checkConnected(reject)) {
log.debug('Adding topic', path);
var topicSpecification = _this.isSupportedTopicType(specification)
? new topicSpecificationType(specification)
: specification;
if (!(topicSpecification instanceof topicSpecificationType)) {
throw new errors_1.IllegalArgumentError('Invalid topic specification or type');
}
// use Protocol 13 - TopicAdd service
_this.TOPIC_ADD_SERVICE.send({
path: path, specification: topicSpecification
}, function (err, response) {
if (err) {
reject(_this.errorReasonIdToAddFailReason[err.id]);
}
else {
resolve({
topic: path,
added: response.status === topic_add_response_1.TopicAddResponseStatus.OK
});
}
});
}
});
};
/**
* @inheritdoc
*/
TopicControlImpl.prototype.remove = function (expression) {
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) {
if (_this.internal.checkConnected(reject)) {
log.debug('Removing topics', expression);
try {
_this.TOPIC_REMOVAL.send(new remove_topic_1.RemoveTopic((args.length > 1)
? parseSelector(Array.prototype.slice.call(args))
: parseSelector(expression)), function (err, response) {
if (err) {
log.debug('Topic removal failed', expression);
reject(err);
}
else {
log.debug('Topic removal complete', expression);
resolve({ removedCount: response });
}
});
}
catch (err) {
log.debug('Error parsing selector', expression);
reject(err);
}
}
});
};
/**
* @inheritdoc
*/
TopicControlImpl.prototype.addMissingTopicHandler = function (path, handler) {
var _this = this;
return new Promise(function (resolve, reject) {
if (!handler) {
reject(new Error('Missing Topic handler is null or undefined'));
return;
}
if (_this.internal.checkConnected(reject)) {
var adapter = {
active: function (close) {
log.debug('Missing Topic Handler registered for ' + path);
handler.onRegister(path, close);
resolve();
},
respond: function (response) {
var callback = response.callback;
log.debug('Missing Topic Handler notification for ' +
response.request.sessionID +
' using ' +
response.request.selector);
try {
var request = response.request;
handler.onMissingTopic({
path: request.selector.prefix,
selector: request.selector,
// tslint:disable-next-line:no-string-literal
sessionID: session_id_1.SessionId.fromString(request.sessionProperties['$SessionId']),
sessionProperties: request.sessionProperties,
serverNames: request.serverNames
});
}
catch (err) {
handler.onError(path, err);
callback.fail(error_reason_1.ErrorReason.CALLBACK_EXCEPTION, err && err.message);
// rethrow so that the conversation can be discarded
throw err;
}
return false;
},
close: function (err) {
log.debug('Missing Topic Handler closed for ' + path);
if (err) {
handler.onError(path, err);
}
else {
handler.onClose(path);
}
}
};
var params = {
definition: Services.MISSING_TOPIC,
group: control_group_1.DEFAULT,
path: path
};
resolve(registration_1.registerTopicHandler(_this.internal, params, adapter));
}
});
};
return TopicControlImpl;
}());
exports.TopicControlImpl = TopicControlImpl;