Skip to content
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

Add haltStrategy option to zipLatest family of Stream combinators #4263

Open
Avaq opened this issue Jan 15, 2025 · 2 comments
Open

Add haltStrategy option to zipLatest family of Stream combinators #4263

Avaq opened this issue Jan 15, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@Avaq
Copy link

Avaq commented Jan 15, 2025

What is the problem this feature would solve?

I use zipLatestWith extensively in my FRP to combine two reactive states into one. Sometimes, I'm zipping a finite data stream with an infinite reactive state stream (such as the ones created by SubscriptionRef). In those cases, I want my data stream to control when the resulting zipped stream ends.

What is the feature you are proposing to solve the problem?

  const data = Stream.make(1, 2, 3, 4, 5).pipe(Stream.schedule(Schedule.spaced('1 second')));
  const state = yield* SubscriptionRef.make(2);

  return Stream.zipLatestWith(
    data,
    state.changes,
    (x, y) => x * y,
    {
      haltStrategy: HaltStrategy.Left
//    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//      This is what I'm proposing.
    }
  );

What alternatives have you considered?

I've tried doing some wacky stuff, like:

const data = Stream.make(1, 2, 3, 4, 5).pipe(Stream.schedule(Schedule.spaced('1 second')));
const state = yield* SubscriptionRef.make(2);
const sharedData = yield* Stream.share(data);

return Stream.zipLatestWith(
  sharedData,
  state.changes.pipe(Stream.haltWhen(
    Stream.runDrain(sharedData)
  )),
  (x, y) => x * y,
);

...and some equally hacky variations, but none of it works.

@Avaq Avaq added the enhancement New feature or request label Jan 15, 2025
@Avaq
Copy link
Author

Avaq commented Jan 15, 2025

Oh and I'm not sure if there are other combinators like the merge* and zipLatest* families, where two streams are combined in parallel, in real-time. But if there are, these combinators would all be good candidates for a haltStrategy option I think.

Maybe actually flatMap with switch or concurrency is one? Where a haltingStrategy could be used to determine whether the returned stream halts if the input ends, or halts when the stream returned from the mapper ends, or either or both.

@Avaq
Copy link
Author

Avaq commented Jan 15, 2025

Generalizing even further: Any situation where a Failure signal can short-circuit the combination of two streams would be a situation where the user may want to pass a haltingStrategy option to also potentially short-circuit when one of the input streams end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant