fix topic array subscription
This commit is contained in:
parent
29026ecb64
commit
ab3d06e9ca
1 changed files with 18 additions and 1 deletions
|
|
@ -8,6 +8,12 @@ class Broker {
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(topic, listener) {
|
subscribe(topic, listener) {
|
||||||
|
if (Array.isArray(topic)) {
|
||||||
|
for (let t of topic) {
|
||||||
|
this.subscribe(t, listener);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
let topics = this.topics;
|
let topics = this.topics;
|
||||||
let channel = topics[topic];
|
let channel = topics[topic];
|
||||||
if (!channel) {
|
if (!channel) {
|
||||||
|
|
@ -36,13 +42,24 @@ class Broker {
|
||||||
}
|
}
|
||||||
|
|
||||||
publish(topic, message, options = {}) {
|
publish(topic, message, options = {}) {
|
||||||
|
if (Array.isArray(topic)) {
|
||||||
|
for (let t of topic) {
|
||||||
|
this.publish(t, message, options);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (topic !== ".topic.publish") {
|
if (topic !== ".topic.publish") {
|
||||||
this.publish(".topic.publish", {topic, message, options});
|
this.publish(".topic.publish", {topic, message, options});
|
||||||
}
|
}
|
||||||
let channel = this.topics[topic];
|
let channel = this.topics[topic];
|
||||||
if (channel && channel.length) {
|
if (channel && channel.length) {
|
||||||
for (let listener of channel) {
|
for (let listener of channel) {
|
||||||
|
try {
|
||||||
listener(message, topic, options);
|
listener(message, topic, options);
|
||||||
|
} catch (e) {
|
||||||
|
// console.log({listener_error: e, topic, options, listener});
|
||||||
|
setTimeout(() => { throw e }, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue