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

Simplify fix for scan+take dispose. Simplify take #59

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/combinators/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,27 @@ function stepTakeWhile (p, stepper, s) {
* @returns {Stream} stream containing at most the first n items from this stream
*/
function take(n, stream) {
var stepper = stream.step;
return stream.beget(function(s) {
return stepTake(stepper, s);
}, new Yield(stream.scheduler.now(), void 0, new Pair(n|0, stream.state)));
// Note: abusing Yield, using it as a Tuple :/
return stream.beget(stepTake, new Yield(stream.scheduler.now(), n|0, stream), disposeTake);
}

function disposeTake(t, x, s) {
// Unwrap original stream's state
var stream = s.state;
return stream.dispose(t, x, stream.state);
}

function stepTake(stepper, s) {
var ss = s.state;
function stepTake(s) {
var stream = s.state;

if(ss.value === 0) {
return new End(s.time, s.value, ss.state);
if(s.value === 0) {
return new End(s.time, s.value, stream.state);
}

return when(function (i) {
return i.done ? i
: i.withState(new Yield(i.time, i.value, new Pair(ss.value-1, i.state)));
}, when(stepper, ss.state));
: i.withState(new Yield(i.time, s.value-1, stream.beget(stream.step, i.state)));
}, when(stream.step, stream.state));
}

/**
Expand Down
16 changes: 9 additions & 7 deletions lib/combinators/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,19 @@ function scan(f, initial, stream) {
function stepScan (f, s) {
var stream = s.state;
return when(function (i) {
var next = stream.beget(stream.step, i.state);

if (i.done) {
return i.withState(stream);
return i.withState(new Pair(i.value, next));
}

var x = f(s.value, i.value);
return new Yield(i.time, x, new Pair(x, stream.beget(stream.step, i.state)));
return new Yield(i.time, x, new Pair(x, next));
}, when(stream.step, stream.state));
}

function disposeScan(t, x, stream) {
// Unwrap original stream's state from scan state
var state = stream.state.state;
return stream.dispose(t, x, state);
}
function disposeScan(t, x, s) {
// Unwrap original stream's state
var stream = s.state;
return stream.dispose(t, x, stream.state);
}
2 changes: 1 addition & 1 deletion test/extend-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('cycle', function() {
var s = new Stream(identity, items, void 0, dispose);

return drain(take(1, extend.cycle(s))).then(function() {
expect(dispose).toHaveBeenCalledWith(0, 1, sentinel);
expect(dispose).toHaveBeenCalledWith(0, 0, sentinel);
});
});
});
Expand Down