robertdp/purescript-wire

The purpose of `share`

paluh opened this issue · 2 comments

paluh commented

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?

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

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