baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript
https://baconjs.github.io
MIT License
6.47k stars 331 forks source link

hasSubscribers #496

Open kmcclosk opened 9 years ago

kmcclosk commented 9 years ago

If I'm off in the weeds, forgive me.

I'm using React, but not react-bacon, although I'm doing something very similiar. This isn't specific to React however.

Let's say I want to attach a Bacon.Bus to every object that is being used to drive my view. However, I defer creating the Bacon.Bus until someone expresses interest via subscribe (in this case, a React component). Since ReactJS has a mount/unmount concept, I'd like to unsubscribe on unmount, which is easy, but also delete the Bacon.Bus if there are no other subscribers. In fact, you could imagine a pool of Bacon.Bus'es used for this purpose. Multiple views of course would be subscribers of the same Bacon.Bus, or perhaps none at all.

I swore that at one time that Bacon.Bus had a hasSubscribers method on it, however now I see that hasSubscribers is on the Dispatcher object. Dispatcher is not documented, so perhaps I shouldn't depend on it? It sounds reasonable to check if a bus has subscribers? If not, perhaps there is a concept I'm missing.

Thanks.

kmcclosk commented 9 years ago

Actually I notice that in version 0.7.10 of BaconJS, hasSubscribers was on EventStream, proxied to dispatcher. Version 0.7.10 is the version that bacon-react uses. I checked the release notes and this change wasn't noted. Can anyone shed light on it.

Many thanks.

Macil commented 9 years ago

If hasSubscribers existed, then I think there should also be a way to detect when that value changes. But then that gets a little meta: would you expose those changes as a stream? Would only buses have this subscriberChanges stream, or every stream including the subscriberChanges stream itself?

If you want to detect unsubscription, then you might want to use Bacon.fromBinder instead of or around a Bus.

raimohanska commented 9 years ago

Is some library (bacon-react?) depending on the existence of hasSubscribers? It surely can be added. PR with test will be merged.