diffusion
Version:
Diffusion JavaScript client
140 lines (139 loc) • 4.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 __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SelectorSet = exports.DELIMITER = void 0;
var abstract_topic_selector_1 = require("./../topics/abstract-topic-selector");
var utils = require("./../topics/topic-path-utils");
var math_1 = require("./../util/math");
var topic_selector_1 = require("../../selectors/topic-selector");
exports.DELIMITER = '////';
/**
* Extract the longest common prefix from a set of topic selectors
*
* @param selectors the topic selectors
* @return the longest common prefix
*/
function extractCommonPrefix(selectors) {
var e_1, _a;
if (selectors.length === 0) {
return '';
}
// set initial value to Integer.MAX_VALUE
var minimum = math_1.MAX_INT32;
var prefixes = [];
selectors.forEach(function (selector, i) {
var part = utils.split(selector.prefix);
minimum = Math.min(minimum, part.length);
prefixes[i] = part;
});
var composite = [];
assembly: for (var j = 0; j < minimum; ++j) {
var part = prefixes[0][j];
try {
for (var prefixes_1 = (e_1 = void 0, __values(prefixes)), prefixes_1_1 = prefixes_1.next(); !prefixes_1_1.done; prefixes_1_1 = prefixes_1.next()) {
var prefix = prefixes_1_1.value;
if (part !== prefix[j]) {
break assembly;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (prefixes_1_1 && !prefixes_1_1.done && (_a = prefixes_1.return)) _a.call(prefixes_1);
}
finally { if (e_1) throw e_1.error; }
}
composite.push(part);
composite.push(utils.PATH_SEPARATOR);
}
return utils.canonicalise(composite.join(''));
}
/**
* Implementation of {@link AbstractTopicSelector} for selector sets
*/
var SelectorSet = /** @class */ (function (_super) {
__extends(SelectorSet, _super);
/**
* Create a new SelectorSet instance
*
* @param selectors the topic selectors in the set
*/
function SelectorSet(selectors) {
var _this = this;
var expanded = [];
selectors.forEach(function (selector) {
if (selector instanceof SelectorSet) {
selector.selectors.forEach(function (nested) {
expanded.push(nested);
});
}
else {
expanded.push(selector);
}
});
var remainder = expanded.join(exports.DELIMITER);
var prefix = extractCommonPrefix(expanded);
_this = _super.call(this, topic_selector_1.Type.SELECTOR_SET, prefix, topic_selector_1.Prefix.SELECTOR_SET + remainder) || this;
_this.selectors = expanded;
return _this;
}
/**
* @inheritdoc
*/
SelectorSet.prototype.selects = function (topicPath) {
var e_2, _a;
try {
for (var _b = __values(this.selectors), _c = _b.next(); !_c.done; _c = _b.next()) {
var selector = _c.value;
if (selector.selects(topicPath)) {
return true;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
return false;
};
/**
* @inheritdoc
*/
SelectorSet.prototype.doSelects = function (canonical) {
return this.selects(canonical);
};
return SelectorSet;
}(abstract_topic_selector_1.AbstractTopicSelector));
exports.SelectorSet = SelectorSet;