rxmqjs / rxmq.js

JavaScript pub/sub library based on RxJS
144 stars 22 forks source link

Multiple wildcard and non-wildcard subscribers not getting events #25

Closed rakeshramakrishnan closed 6 years ago

rakeshramakrishnan commented 6 years ago

On subscription to a topic via wildcards and full name, only the first wildcard observer gets the events

Code snippet:

const rxmq = require("rxmq")

const {Channel} = rxmq

const channel = new Channel()

const e1 = {value: 1}
const e2 = {value: 2}

const observedEventsObs1 = []
const observedEventsObs2 = []
const observedEventsObs3 = []
const observedEventsPatternObs1 = []
const observedEventsPatternObs2 = []
const observedEventsPatternObs3 = []

channel.observe("topic.publish").subscribe(e => observedEventsObs1.push(e))
channel.observe("topic.publish").subscribe(e => observedEventsObs2.push(e))
channel.observe("topic.publish").subscribe(e => observedEventsObs3.push(e))
channel.observe("topic.*").subscribe(e => observedEventsPatternObs1.push(e))
channel.observe("topic.*").subscribe(e => observedEventsPatternObs2.push(e))
channel.observe("topic.*").subscribe(e => observedEventsPatternObs3.push(e))

channel.subject("topic.publish").next(e1)
channel.subject("topic.publish").next(e2)

console.log("Topic observer 1", observedEventsObs1)
console.log("Topic observer 2", observedEventsObs2)
console.log("Topic observer 3", observedEventsObs3)
console.log("Pattern observer 1", observedEventsPatternObs1)
console.log("Pattern observer 2", observedEventsPatternObs2)
console.log("Pattern observer 3", observedEventsPatternObs3)

/*
Outputs:
Topic observer 1 [ { value: 1 }, { value: 2 } ]
Topic observer 2 [ { value: 1 }, { value: 2 } ]
Topic observer 3 [ { value: 1 }, { value: 2 } ]
Pattern observer 1 [ { value: 1 }, { value: 2 } ]
Pattern observer 2 []
Pattern observer 3 []

Expecting pattern observer 2 and 3 to also get the events
*/

However, if there are no direct topic name observers (i.e., all observers listen only via patterns), then all pattern based observers get the events.

yamalight commented 6 years ago

That's an interesting catch 🤔 I'll try to figure out why it happens and fix it once I have time (PRs welcome), thanks!

yamalight commented 6 years ago

Fix shipped in 1.2.2