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

support drop(), issue #11 #75

Closed
wants to merge 4 commits into from
Closed

Conversation

alexbeletsky
Copy link
Contributor

Provides support for .drop()

return this.consume(function (err, x, push, next) {
if (err) {
push(err);
if (n < 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be n >= 0 ?

@caolan
Copy link
Owner

caolan commented May 12, 2014

Thanks for this, looks good apart from the comment I added. You might want to add a test for error handling to make sure it does what's expected here.

@alexbeletsky
Copy link
Contributor Author

@caolan thanks! I'm wondering how to trigger err during consuming.. are there some tests that do anything similar?

Will extend test cases and re-apply.

@caolan
Copy link
Owner

caolan commented May 12, 2014

@alexanderbeletsky you'll have to use a generator stream and push(error) onto it to cause the downstream .drop() to consume them. See the .errors() and .stopOnError() tests for examples: https://github.com/caolan/highland/blob/master/test/test.js#L408-L427

@alexbeletsky
Copy link
Contributor Author

@caolan will be happy if you take a look on updated version :)

* @id drop
* @section Streams
* @name Stream.drop(n)
* @param {Number} n - n - integer representing number of values to read from source
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate n -.

@vqvu
Copy link
Collaborator

vqvu commented May 22, 2014

I'm a little concerned about the handling of errors.

  1. The function seems to impose an unnecessary stopOnError, since it pushes nil once it see the first error that comes after the first n values.
  2. All errors before the first n values are emitted. This seems to contradict the behavior of take, where no errors after the first n values are emitted. I would expect that only errors emitted after the first n values are allowed through.

@alexbeletsky
Copy link
Contributor Author

@vqvu I think your concerns are right.. I just wasn't sure about right behavior. Would be happy to clarify.

The current behavior,

exports['drop - errors'] = function (test) {
    test.expect(3);
    var s = _(function (push, next) {
        push(null, 1),
        push(new Error('error'), 2),
        push(null, 3),
        push(null, 4),
        push(null, _.nil)
    });
    var f = s.drop(2);
    f.pull(function (err, x) {
        test.equal(err.message, 'error');
    });
    f.pull(function (err, x) {
        test.equal(x, 4);
    });
    f.pull(function (err, x) {
        test.equal(x, _.nil);
    });
    test.done();
};

Expected behaviour,

exports['drop - errors'] = function (test) {
    test.expect(3);
    var s = _(function (push, next) {
        push(null, 1),
        push(new Error('error'), 2),
        push(null, 3),
        push(null, 4),
        push(null, _.nil)
    });
    var f = s.drop(2);
    f.pull(function (err, x) {
        test.equal(x, 4);
    });
    f.pull(function (err, x) {
        test.equal(x, _.nil);
    });
    test.done();
};

exports['drop - errors 2'] = function (test) {
    test.expect(3);
    var s = _(function (push, next) {
        push(null, 1),
        push(null, 2),
        push(new Error('error'), 3),
        push(null, 4),
        push(null, _.nil)
    });
    var f = s.drop(2);
    f.pull(function (err, x) {
        test.equal(err.message, 'error');
    });
    f.pull(function (err, x) {
        test.equal(x, _.nil);
    });
    test.done();
};

?

@caolan
Copy link
Owner

caolan commented May 23, 2014

I'm not sure. The reason take() doesn't return any errors after the first n elements is because the rest of the stream isn't evaluated. With drop() we have to evaluate the first n items in order to ignore them. If an evaluation results in an error then it should probably be passed along the stream regardless of whether a result in that position would be ignored... though I could potentially see the case for dropping them too, I think I'd rather err on the side of caution...

@vqvu
Copy link
Collaborator

vqvu commented May 23, 2014

@caolan Good point. It's probably better to be safe. I'm more used to the Rx way of handling errors, so I think I was thrown off by the "more than one error per stream" semantics that we have here.

@alexanderbeletsky I'm now OK with keeping the drop - error behavor as it currently is, but I think drop - error 2 should be:

exports['drop - errors 2'] = function (test) {
    test.expect(3);
    var s = _(function (push, next) {
        push(null, 1),
        push(null, 2),
        push(new Error('error'), 3),
        push(null, 4),
        push(null, _.nil)
    });
    var f = s.drop(2);
    f.pull(function (err, x) {
        test.equal(err.message, 'error');
    });
    f.pull(function (err, x) {
        test.equal(x, 4);
    });
    f.pull(function (err, x) {
        test.equal(x, _.nil);
    });
    test.done();
};

An error shouldn't cause the stream to stop, since no other stream operators do this.

@caolan
Copy link
Owner

caolan commented May 27, 2014

"An error shouldn't cause the stream to stop" - correct :) - since it's lazy it's up to the consumer to decide whether to continue reading. Obviously, if the stream is a data source and the error is unrecoverable, then the next value will just be nil.

@caolan
Copy link
Owner

caolan commented Jul 12, 2014

@vqvu @alexanderbeletsky where are we with this?

@alexbeletsky
Copy link
Contributor Author

@caolan I need to correct that to latest discussed behaviour. Will try to re-submit asap.

@apaleslimghost
Copy link
Collaborator

@alexanderbeletsky what's the status on this? Current branch has merge conflicts so you might want to rebase on master.

@alexbeletsky
Copy link
Contributor Author

@quarterto unfortunately, I have to admit I failed to understand how the error handling should work, so my progress just stopped.

I will try to rebase and re-submit so we can discuss it again.

@jeromew
Copy link
Collaborator

jeromew commented Jan 5, 2015

@alexbeletsky sorry for the delay on this.

regarding the question on how error handling should work : all errors should be passed along downstream.

that would mean replace

+    if (err) {
+            push(err);
+            if (n < 0) {
+                next();
+            }
+            else {
+                push(null, nil);
+            }
+        }

by

+       if (err) {
+            push(err);
+            next();
+        }

do you still have the motivation / available time to finish this PR or would you prefer if someone can take it over ?

@alexbeletsky
Copy link
Contributor Author

Yes, I'm still willing to help and make this PR happen!

On Mon, Jan 5, 2015, 21:21 jeromew notifications@github.com wrote:

@alexbeletsky https://github.com/alexbeletsky sorry for the delay on
this.

regarding the question on how error handling should work : all errors
should be passed along downstream.

that would mean replace

  • if (err) {+ push(err);+ if (n < 0) {
  •            next();+            }+            else {+                push(null, nil);+            }+        }
    

by

  •   if (err) {+            push(err);
    
  •        next();+        }
    

do you still have the motivation / available time to finish this PR or
would you prefer if someone can take it over ?


Reply to this email directly or view it on GitHub
#75 (comment).

@svozza
Copy link
Collaborator

svozza commented Mar 1, 2015

What's the status of this? Do we even need it now that we have transducers?

@vqvu
Copy link
Collaborator

vqvu commented Mar 1, 2015

We don't need it, but I think we should have it as a pair with take.

@svozza
Copy link
Collaborator

svozza commented Mar 3, 2015

Yep, it makes sense to have the both of them.

@alexbeletsky There's not very much left on this to do so I grabbed the changes from your pull request using the hub tool and I can make the minor adjustments for you. Do you mind if I finish this pull request on your behalf? It's just it's been open for a while and it would be nice to close it.

@alexbeletsky
Copy link
Contributor Author

@svozza please, go ahead! Thanks a lot for your help, if any question plz ping me.

@svozza
Copy link
Collaborator

svozza commented Mar 3, 2015

No probs!

@vqvu
Copy link
Collaborator

vqvu commented Mar 5, 2015

Superseded by #244.

@vqvu vqvu closed this Mar 5, 2015
@svozza svozza mentioned this pull request Apr 9, 2015
@vqvu vqvu added this to the v2.5.0 milestone Apr 17, 2015
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

Successfully merging this pull request may close these issues.

6 participants