3
3
/**
4
4
* Module dependencies
5
5
*/
6
- const LruMap = require ( 'collections/ lru-map ' )
6
+ const LruMap = require ( 'lru-cache ' )
7
7
const NumberAllocator = require ( 'number-allocator' ) . NumberAllocator
8
8
9
9
/**
@@ -17,7 +17,7 @@ function TopicAliasSend (max) {
17
17
}
18
18
19
19
if ( max > 0 ) {
20
- this . aliasToTopic = new LruMap ( )
20
+ this . aliasToTopic = new LruMap ( { max : max } )
21
21
this . topicToAlias = { }
22
22
this . numberAllocator = new NumberAllocator ( 1 , max )
23
23
this . max = max
@@ -37,9 +37,9 @@ TopicAliasSend.prototype.put = function (topic, alias) {
37
37
}
38
38
const entry = this . aliasToTopic . get ( alias )
39
39
if ( entry ) {
40
- delete this . topicToAlias [ entry . topic ]
40
+ delete this . topicToAlias [ entry ]
41
41
}
42
- this . aliasToTopic . set ( alias , { topic : topic , alias : alias } )
42
+ this . aliasToTopic . set ( alias , topic )
43
43
this . topicToAlias [ topic ] = alias
44
44
this . numberAllocator . use ( alias )
45
45
this . length = this . aliasToTopic . length
@@ -52,9 +52,7 @@ TopicAliasSend.prototype.put = function (topic, alias) {
52
52
* @returns {String } - if mapped topic exists return topic, otherwise return undefined
53
53
*/
54
54
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 )
58
56
}
59
57
60
58
/**
@@ -74,7 +72,7 @@ TopicAliasSend.prototype.getAliasByTopic = function (topic) {
74
72
* Clear all entries
75
73
*/
76
74
TopicAliasSend . prototype . clear = function ( ) {
77
- this . aliasToTopic . clear ( )
75
+ this . aliasToTopic . reset ( )
78
76
this . topicToAlias = { }
79
77
this . numberAllocator . clear ( )
80
78
this . length = 0
@@ -87,7 +85,7 @@ TopicAliasSend.prototype.clear = function () {
87
85
TopicAliasSend . prototype . getLruAlias = function ( ) {
88
86
const alias = this . numberAllocator . firstVacant ( )
89
87
if ( alias ) return alias
90
- return this . aliasToTopic . min ( ) . alias
88
+ return this . aliasToTopic . keys ( ) [ this . aliasToTopic . length - 1 ]
91
89
}
92
90
93
91
module . exports = TopicAliasSend
0 commit comments