-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: multicast by default? #207
Comments
An example for a bug I just encountered because of this: I have a stream that needs to fetch a value from the server on app startup. It receives an init event from Because this value was used by more than just one stream later, the app suddenly made 4 requests to the server to get this static value, although I had |
Another bug a I had because of missing Compared to the bug above, these are two quite different behaviours although they have the same root cause. That’s why it always takes time to figure out if |
tl;dr there are significant performance implications. For now, it's not likely to change, but we're actively investigating ways we could make it work. It's been an open question, and there are good reasons on both sides. If you've seen any of the discussions about hot vs. cold streams in other libraries, this is related. It's a very tricky problem for push-based implementations (like all of the popular current generation reactive JS libs), and unfortunately (as you've seen), introduces additional cognitive load. Other approaches, such as pull-based (like the experiment here), can avoid the problems (tho they have their own issues to work out!). One interesting discussion has been around the intent of reactive streams. Some feel that the intended use case is point-to-point, and doing something different from that requires action on the part of the developer. IOW, reactive streams aren't event emitters. That's complicated by the fact that some streams, like The reason multicasting isn't the default in most.js is that doing the necessary bookkeeping at every node in the stream graph has significant performance implications. In the vast majority of "intermediate" nodes, multicasting isn't necessary. For example, take a function like: const getTheStuff(url) =>
most.fromPromise(getUrl(url))
.filter(removeSillyThings)
.map(extractOnlyTheGoodParts) In that case, There's been some work to reduce the cost in the case where a node has exactly one observer, but so far, it simply isn't possible to achieve the current non-multicasted level of performance. So, right now, identifying the right points to allow multiple observers is the responsibility of the application developer. So, if you know that const getTheStuff(url) =>
most.fromPromise(getUrl(url))
.filter(removeSillyThings)
.map(extractOnlyTheGoodParts)
.multicast() So, for now, we'll probably continue to experiment with ways to make multicasting "acceptably" fast. If that's possible, then it'll definitely be worth revisiting whether multicast-by-default is the right thing! |
Thanks for the long answer. That makes more sense to me now. So in general is the rule to add most.fromEvent('visibilitychange', document)
.map(() => !document.hidden)
.startWith(!document.hidden); This resulted in streams not emitting events anymore that are subscribed to |
Interesting. I'll try to reproduce it and let you know what I find. If you discover more info, let me know! |
Looks like this is not supposed to break then? I’ll close this issue and open a new one for the visibility bug. Will try to find the root cause. |
Correct. I don't see why adding multicast would cause a problem. Thanks for opening another issue! |
I know this is closed and the library is designed like this on purpose, but just wanted to +1 this request as in the short term, I think the API documentation of multicast could be clearer: Rather than being an opt-in optimization, you have to use multicast if the stream is subscribed from multiple points to get the more intuitive behavior we're used to: i.e Also, the doc could be more precise about where we need to use multicast. It's less obvious what should be done with setups like |
Just in case you don't receive notifications on edits; @briancavalier |
After working with
most
for a while (which I greatly enjoy, thanks for that) I still get bugs because I forgot tomulticast
a stream that has more than one listener. Most often I don’t spot this first and lose time debugging until I figure out that it’s amulticast
issue again.Would it be possible to make streams
multicast
by default and haveunicast
as an optimisation? As a user it’s much easier to have my graph work as expected and addunicast
as an optimisation later if a stream has just one listener, than suddenly getting unexpected behaviour because a new listener was added.The text was updated successfully, but these errors were encountered: