forked from RangerMauve/mqtt-emitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (172 loc) · 5.67 KB
/
index.js
File metadata and controls
208 lines (172 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict'
/*
mqtt-emitter by RangerMauve. Version 0.0.2
*/
var MQTTPattern = require('mqtt-pattern')
var MQTTStore = require('mqtt-store')
module.exports = MQTTEmitter
/**
* Creates a new MQTTEmitter instance
*/
function MQTTEmitter () {
this._listeners = new MQTTStore()
this._regexes = []
}
// Inherit the PatternEmitter methods and stuff
MQTTEmitter.prototype = Object.create({
addListener: addListener,
on: addListener,
once: once,
removeListener: removeListener,
removeAllListeners: removeAllListeners,
listeners: listeners,
emit: emit,
onadd: onadd,
onremove: onremove
})
// convenience method to retrieve the set of listeners bound to a topicString from store
function get (store, topicString) {
var res = store.get(topicString)
if (res !== MQTTStore.NO_RESULT) return res.value
else return undefined
}
/**
* Listen for MQTT messages that match a given pattern.
* @see {@link https://github.com/RangerMauve/mqtt-regex|mqtt-regex}
* @param {String} topic MQTT topic pattern with optional placeholders
* @param {Object} options optional MQTT subscribe options
* @param {Function} handler Callback which takes the MQTT payload and topic params
* @return {MQTTEmitter} Returns self for use in chaining
*/
function addListener (topic, options, handler) {
if (typeof options === 'function') {
handler = options
options = {}
}
var topicString = MQTTPattern.clean(topic)
var listeners = get(this._listeners, topicString)
if (!listeners) {
listeners = this._listeners.put(topicString, []).value
}
var isNew = (listeners.length === 0)
listeners.push({
fn: handler,
pattern: topic,
options: options
})
if (isNew) this.onadd(topicString, options)
return this
}
/**
* Adds a one time listener for the event
* @param {String} topic Topic pattern to listen on
* @param {Function} handler Function to call the next time this topic appears
* @return {MQTTEmitter} Returns self for use in chaining
*/
function once (topic, handler) {
var self = this
onceHandler.handler = handler
this.on(topic, onceHandler)
return this
function onceHandler (data, params) {
handler.call(self, data, params)
self.removeListener(topic, onceHandler)
}
}
/**
* Removes an existing listener
* @param {String} topic Topic pattern to unsubscribe from
* @param {Function} handler Handler that was used originally
* @return {MQTTEmitter} Returns self for use in chaining
*/
function removeListener (topic, handler) {
var topicString = MQTTPattern.clean(topic)
var listeners = get(this._listeners, topicString)
if (!listeners || !listeners.length) return this
var hasFiltered = false
var filteredListeners = listeners.filter(function (listener) {
if (hasFiltered) return true
var matches = (listener.fn === handler)
if (!matches) return true
hasFiltered = true
return false
})
if (!filteredListeners.length) this.onremove(topicString)
if (hasFiltered) {
this._listeners.put(topicString, filteredListeners)
}
return this
}
/**
* Removes all listeners for this type of topic.
* @param {String} topic Topic pattern to unsubscribe from
* @return {MQTTEmitter} Returns self for use in chaining
*/
function removeAllListeners (topic) {
if (topic) {
var topicString = MQTTPattern.clean(topic)
var listeners = get(this._listeners, topicString)
if (!listeners.length) return this
this._listeners.put(topicString, [])
this.onremove(topicString)
} else {
var topicStrings = this._listeners.findMatching('#').filter(function ({ value: listeners }) {
return listeners && listeners.length > 0
}).map(function ({ key }) {
return MQTTPattern.clean(key)
})
// reversing the topicStrings to keep the original order of mqtt-store version 1.0.4 with 2.1.0
for (var i in topicStrings.reverse()) {
this.onremove(topicStrings[i])
}
this._listeners = new MQTTStore()
}
return this
}
/**
* Returns an array of listeners that match this topic
* @param {String} topic The topic pattern to get listeners for
* @return {Array} Array of handler functions
*/
function listeners (topic) {
var topicString = MQTTPattern.clean(topic)
return (get(this._listeners, topicString) || []).map(function (listener) {
return listener.fn
})
}
/**
* Process a new MQTT event and dispatch it to relevant listeners
* @param {String} topic The raw MQTT topic string recieved from a connection
* @param {Any} payload This is the payload from the MQTT topic event
* @return {Boolean} Returns true if there were any listeners called for this topic
*/
function emit (topic, payload) {
var topicString = MQTTPattern.clean(topic)
var matching = this._listeners.findPatterns(topicString)
if (!matching.length) return false
matching.forEach(function ({ value: listeners }) {
listeners.forEach(function (listener) {
var pattern = listener.pattern
var params = MQTTPattern.exec(pattern, topic)
listener.fn(payload, params, topic, listener.pattern)
})
})
return true
}
/**
* Hook for reacting to new MQTT topics
* @param {String} topic MQTT topic that is being subscribed to
* @param options options MQTT subscription options
*/
function onadd (topic, options = {}) {
// Detect when new topics are added, maybe
// Can be useful for auto-subscribing on actual MQTT connection
}
/**
* Hook for reacting to removed MQTT topics
* @param {String} topic MQTT topiuc that is being unsubscribed from
*/
function onremove (topic) {
// Detect when topics are no longer listened to here
// Can be useful for auto-unsubscribing on an actual MQTT connection
}