0no-co / wonka

🎩 A tiny but capable push & pull stream library for TypeScript and Flow
MIT License
709 stars 29 forks source link

Add makeReplaySubject source #76

Closed EliaECoyote closed 2 years ago

EliaECoyote commented 4 years ago

Related to #75.

This PR aims at adding a makeReplaySubject source. Please note that replay subjects should replay values even after completion

EliaECoyote commented 4 years ago

hey @kitten, did you get a chance to checkout this PR?

kitten commented 4 years ago

Sorry! I totally forgot to reply here. 😢 I was meaning to ask, do you think it may make more sense to turn this into a replay operator?

EliaECoyote commented 4 years ago

My intention was to add another separate PR for a shareReplay operator, which would be pretty easy to implement through the makeReplaySubject source, e.g.:

let shareReplay = (bufferSize: int, source: sourceT('a)): sourceT('a) => {
  ley replaySubject = Wonka_sources.makeReplaySubject(bufferSize)};
  let _subscription = source
    |> Wonka_sinks.subscribe(state.replaySubject.next);
  state.replaySubject.source;
};

Honestly I don't see any reason to allow devs to use only the shareReplay operator: do you want to elaborate on that please?

kitten commented 4 years ago

Sure can 👍 most of the time it won’t make sense to emulate a replay operator using the replay subject imo, since we’re just creating an operator that accepts operations and re-emits them.

So if we turn this around and have a replay operator instead of a subject, a replay subject would just be the replay operator applied to a subject, which saves us from having two subject types 😇

Austaras commented 4 years ago

Speaking from a personal experience, BehaviorSubject is used more

kitten commented 4 years ago

@Austaras The motivation here is that BehaviorSubject and ReplaySubject can be derived from shareReplay. If you have a peek at the "compliance" tests for operators there's a lot that operators can guarantee, so it'll require a more rigorous implementation that is then also sure to work for the subjects, which can be aliases.

I haven't taken the time to implement and fix some of the issues for Wonka in a while, so I'll pick up this PR and rewrite it into an operator with an alias. @EliaECoyote Sorry that this has been around for a while. I'll make sure to ping you on the progress on this and either way will credit any result of this to you in the changelog and PR.

Austaras commented 4 years ago

Well that may do the work, but IMO the ability to synchronously get latest state is vital and it seems to be impossible for an operator to achieve.