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

regression: s.end(true) failing to end stream? #176

Closed
leeoniya opened this issue Apr 27, 2018 · 3 comments
Closed

regression: s.end(true) failing to end stream? #176

leeoniya opened this issue Apr 27, 2018 · 3 comments

Comments

@leeoniya
Copy link
Contributor

leeoniya commented Apr 27, 2018

hey @paldepind,

i'm having an issue with the latest flyd relative to a prior version. it seems like s.end(true) is failing to end the stream. in my case this is causing an infinite loop and stack explosion:

v0.2.6 works:
https://jsfiddle.net/tbtpoxwk/

master (Maximum call stack size exceeded):
https://jsfiddle.net/tbtpoxwk/1/

i haven't done a bisect to figure out which commit broke things :/

@nordfjord
Copy link
Collaborator

Your problem is here:

	var sub = flyd.on(val => {
		// this check ignores the initial call during subscription
		if (!sub) return;

		sub.end(true);
		then();
	}, s);

You are assuming that the method passed to flyd.on will run immediately. This is not the case if it happens inside a stream body. This is unfortunately flyd working as intended.

It seems the function you passed to flyd.on is trying to do two things:

  1. drop 1 value
  2. execute only once

We can implement this generically as drop and take

const drop = n => s => {
  return flyd.combine(function(s, self){
   if (--n < 0) self(s());
  }, [s]);
}

const take = n => s => {
  return flyd.combine(function(s, self) {
    if (--n < 0) {
      self.end(true);
      return;
    }
    self(s());
  }, [s])
}

then to achieve the desired result you can utilise these methods like so:

function on(s, then) {
  var sub = s
    .pipe(drop(1))
    .pipe(take(1))
    .map(_ => then());

  return s();
}

@nordfjord
Copy link
Collaborator

here is a fiddle demonstrating the resolution: https://jsfiddle.net/kfmtun8w/1/

For further context about this behaviour of flyd take a look at #160 and #163

@leeoniya
Copy link
Contributor Author

awesome.

thanks @nordfjord!

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

2 participants