Skip to content

Commit 5c67037

Browse files
authored
fix: migrate LruMap from collections to lru-cache. (mqttjs#1396)
1 parent b4925d0 commit 5c67037

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

lib/topic-alias-send.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* Module dependencies
55
*/
6-
const LruMap = require('collections/lru-map')
6+
const LruMap = require('lru-cache')
77
const NumberAllocator = require('number-allocator').NumberAllocator
88

99
/**
@@ -17,7 +17,7 @@ function TopicAliasSend (max) {
1717
}
1818

1919
if (max > 0) {
20-
this.aliasToTopic = new LruMap()
20+
this.aliasToTopic = new LruMap({ max: max })
2121
this.topicToAlias = {}
2222
this.numberAllocator = new NumberAllocator(1, max)
2323
this.max = max
@@ -37,9 +37,9 @@ TopicAliasSend.prototype.put = function (topic, alias) {
3737
}
3838
const entry = this.aliasToTopic.get(alias)
3939
if (entry) {
40-
delete this.topicToAlias[entry.topic]
40+
delete this.topicToAlias[entry]
4141
}
42-
this.aliasToTopic.set(alias, { topic: topic, alias: alias })
42+
this.aliasToTopic.set(alias, topic)
4343
this.topicToAlias[topic] = alias
4444
this.numberAllocator.use(alias)
4545
this.length = this.aliasToTopic.length
@@ -52,9 +52,7 @@ TopicAliasSend.prototype.put = function (topic, alias) {
5252
* @returns {String} - if mapped topic exists return topic, otherwise return undefined
5353
*/
5454
TopicAliasSend.prototype.getTopicByAlias = function (alias) {
55-
const entry = this.aliasToTopic.get(alias)
56-
if (typeof entry === 'undefined') return entry
57-
return entry.topic
55+
return this.aliasToTopic.get(alias)
5856
}
5957

6058
/**
@@ -74,7 +72,7 @@ TopicAliasSend.prototype.getAliasByTopic = function (topic) {
7472
* Clear all entries
7573
*/
7674
TopicAliasSend.prototype.clear = function () {
77-
this.aliasToTopic.clear()
75+
this.aliasToTopic.reset()
7876
this.topicToAlias = {}
7977
this.numberAllocator.clear()
8078
this.length = 0
@@ -87,7 +85,7 @@ TopicAliasSend.prototype.clear = function () {
8785
TopicAliasSend.prototype.getLruAlias = function () {
8886
const alias = this.numberAllocator.firstVacant()
8987
if (alias) return alias
90-
return this.aliasToTopic.min().alias
88+
return this.aliasToTopic.keys()[this.aliasToTopic.length - 1]
9189
}
9290

9391
module.exports = TopicAliasSend

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@
6262
"net": false
6363
},
6464
"dependencies": {
65-
"collections": "^5.1.13",
6665
"commist": "^1.0.0",
6766
"concat-stream": "^2.0.0",
6867
"debug": "^4.1.1",
6968
"duplexify": "^4.1.1",
7069
"help-me": "^3.0.0",
7170
"inherits": "^2.0.3",
71+
"lru-cache": "^6.0.0",
7272
"minimist": "^1.2.5",
7373
"mqtt-packet": "^6.8.0",
7474
"number-allocator": "^1.0.9",

0 commit comments

Comments
 (0)