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

Consider API for next major version of Step #24

Open
creationix opened this issue Dec 29, 2011 · 5 comments
Open

Consider API for next major version of Step #24

creationix opened this issue Dec 29, 2011 · 5 comments

Comments

@creationix
Copy link
Owner

https://gist.github.com/1524578#comments

@creationix
Copy link
Owner Author

See some background at #7

@creationix
Copy link
Owner Author

Also merged with #6

@digitalrinaldo
Copy link

Does Step work with events. Suppose you have an imap server that sends multiple message events and an end event after the last message has been sent. How do I get the end event to further wait for the message events to be processed use group?

@creationix
Copy link
Owner Author

@digitalrinaldo, Step is meant to be a control-flow structure for async callbacks. Events are a little more complicated, but interface via callbacks.

Group is for cases where you have a finite number of async functions and want to be notified when they are all done. Events on a stream is different because you don't know how many there will be till then "end" event fires.

// Using the old API
Step(
  function () {
    var chunks = [];
    this.parallel(null, chunks);
    var slot = this.parallel();
    emitter.on('data', function (chunk) { chunks.push(chunk); });
    emitter.on('error', slot);
    emitter.on('end', slot); // Assuming end doesn't have any arguments
  },
  function (err, chunks, dummy) {

  }
);

// Using the twostep API
Step(
  function () {
    var chunks = [];
    this.pass(chunks);
    var slot = this.slot();
    emitter.on('data', function (chunk) { chunks.push(chunk); });
    emitter.on('error', slot);
    emitter.on('end', slot); // Assuming end doesn't have any arguments
  },
  function (err, chunks, dummy) {

  }
);

@xavi-
Copy link
Contributor

xavi- commented Feb 25, 2012

@creationix, That's a good workaround. I've been manually executing callbacks:

Step(
  function () {
    var chunks = [], next = this;
    emitter.on('data', function (chunk) { chunks.push(chunk); });
    emitter.on('error', function(err) { next(err); });
    emitter.on('end', function() { next(null, chunks) });
  },
  function (err, chunks) {

  }
);

It'd be cool if Step2 had connivence method to better support stream APIs. Maybe something like this:

Step(
  function () {
    this.listen(emitter); // Listens for the data, error, end events for me
    emitter.pipe(this.pipe()) // Or, `this.pipe()` creates a writeable stream
  },
  function (err, chunks1, chunks2) {
    // chunks1 and 2 are arrays of buffers or strings
  }
);

Not sure if you need both this.listen and this.pipe, but having either would definitely get rid of boiler plate. Stream APIs are becoming very popular.

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