1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const lru_cache_1 = require("lru-cache");
|
4 | const number_allocator_1 = require("number-allocator");
|
5 | class TopicAliasSend {
|
6 | constructor(max) {
|
7 | if (max > 0) {
|
8 | this.aliasToTopic = new lru_cache_1.LRUCache({ max });
|
9 | this.topicToAlias = {};
|
10 | this.numberAllocator = new number_allocator_1.NumberAllocator(1, max);
|
11 | this.max = max;
|
12 | this.length = 0;
|
13 | }
|
14 | }
|
15 | put(topic, alias) {
|
16 | if (alias === 0 || alias > this.max) {
|
17 | return false;
|
18 | }
|
19 | const entry = this.aliasToTopic.get(alias);
|
20 | if (entry) {
|
21 | delete this.topicToAlias[entry];
|
22 | }
|
23 | this.aliasToTopic.set(alias, topic);
|
24 | this.topicToAlias[topic] = alias;
|
25 | this.numberAllocator.use(alias);
|
26 | this.length = this.aliasToTopic.size;
|
27 | return true;
|
28 | }
|
29 | getTopicByAlias(alias) {
|
30 | return this.aliasToTopic.get(alias);
|
31 | }
|
32 | getAliasByTopic(topic) {
|
33 | const alias = this.topicToAlias[topic];
|
34 | if (typeof alias !== 'undefined') {
|
35 | this.aliasToTopic.get(alias);
|
36 | }
|
37 | return alias;
|
38 | }
|
39 | clear() {
|
40 | this.aliasToTopic.clear();
|
41 | this.topicToAlias = {};
|
42 | this.numberAllocator.clear();
|
43 | this.length = 0;
|
44 | }
|
45 | getLruAlias() {
|
46 | const alias = this.numberAllocator.firstVacant();
|
47 | if (alias)
|
48 | return alias;
|
49 | return [...this.aliasToTopic.keys()][this.aliasToTopic.size - 1];
|
50 | }
|
51 | }
|
52 | exports.default = TopicAliasSend;
|
53 |
|
\ | No newline at end of file |