support for multiple callbacks per topic started
This commit is contained in:
@ -7,7 +7,7 @@ type TopicHandlerCallback = (message: any) => any
|
||||
|
||||
interface TopicHandler {
|
||||
topic: string,
|
||||
callback: TopicHandlerCallback
|
||||
callbacks: TopicHandlerCallback[]
|
||||
}
|
||||
|
||||
export function passThrough(message: any) {
|
||||
@ -25,8 +25,19 @@ export class MqttClient {
|
||||
}
|
||||
|
||||
register(topic: string, callback: TopicHandlerCallback) : void {
|
||||
this._topicHandlers.push({topic, callback})
|
||||
log.info(`handler registered for topic ${topic}`)
|
||||
let done: boolean = false
|
||||
for (let topicHandler of this._topicHandlers) {
|
||||
if (topicHandler.topic === topic) {
|
||||
topicHandler.callbacks.push(callback)
|
||||
done = true
|
||||
log.info(`additional callback added for topic ${topic}`)
|
||||
}
|
||||
}
|
||||
if (! done) {
|
||||
let cbs : TopicHandlerCallback[] = [ callback ]
|
||||
this._topicHandlers.push({topic:topic, callbacks:cbs})
|
||||
log.info(`first callback added for topic ${topic}`)
|
||||
}
|
||||
}
|
||||
|
||||
exec() : void {
|
||||
@ -43,7 +54,7 @@ export class MqttClient {
|
||||
for (let topicHandler of this._topicHandlers) {
|
||||
if (this.topicMatch(topicHandler.topic, topic)) {
|
||||
log.info(`received topic ${topic} matches registered topic ${topicHandler.topic}`)
|
||||
topicHandler.callback(payload)
|
||||
// topicHandler.callback(payload)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user