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

What's the motivation for ending a stream? #216

Open
LAC-Tech opened this issue Apr 17, 2021 · 3 comments
Open

What's the motivation for ending a stream? #216

LAC-Tech opened this issue Apr 17, 2021 · 3 comments

Comments

@LAC-Tech
Copy link

Hi, long time user, first time writer.

Reading the docs again, I notice that there's a concept of ending a stream. What's the motivation for that? I can't think of a scenario, but I'm sure you had one in mind.

@nordfjord
Copy link
Collaborator

Imagine you've got a timer and want to cancel it when listeners stop:

function interval(ms: number) {
  const s = stream()
  let i = 0;
  const interval = setInterval(()=> s(i++), ms)
  s.end.map(()=> clearInterval(interval))
  return s
}

interval(1000)
  .pipe(take(4))
// 1-2-3-4

This can be extrapolated for arbitrary cleanup logic for e.g. web sockets

@dehmer
Copy link

dehmer commented Jul 3, 2023

I assume take in the case must be aware of the additional protocol and has to actively close upstream interval when it's done. take probably also has to close itself so that an appropriate side-effect can be triggered. I this correct?

@nordfjord
Copy link
Collaborator

That sounds about right. Some of the built-in modules propagate ending downwards using a pattern like

const takeUntil = (term, src) =>
  flyd.endsOn(
    flyd.merge(term, src.end),
    flyd.combine((src, self) => {
      self(src());
    }, [src])
  );

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

No branches or pull requests

3 participants