robertdp / purescript-wire

Events and Signals for FRP. Monad instances included
12 stars 1 forks source link

The purpose of `share` #12

Open paluh opened 4 years ago

paluh commented 4 years ago

This is for sure naive question but I'm not sure if I'm able to grasp the idea behind share :: Event a -> Effect (Event a) because it seems that it only forwards values to the handlers. Could you please explain the purpose of this function?

robertdp commented 4 years ago

share is supposed to behave something like multicasting from RxJS. The idea is that you have some initial Event a that goes through some transformation that produces an Event b. Because of the way subscriptions are modelled, a subscriber to Event b is actually subscribing to Event a with all the transformations applied. So the transformation is applied individually per-subscriber.

If there will be multiple subscribers to the result of the transformation, it is better to have only a single subscription to the original event so the transformation is only performed once. This is especially true if the transformation is expensive.

Something like

sharing

paluh commented 4 years ago

Wow! This is really clear and readable explanation - thanks a lot for creating it! Could it be a part of the future docs? ;-)