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

Add pendReady() to factor out repetitive code and facilitate use of the mechanism in plugins #1079

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

jmm
Copy link
Collaborator

@jmm jmm commented Jan 19, 2015

This is a proposal to factor out this pattern that's used in several places into a method:

self._pending ++;
somethingAsync(function callback () {
  // ...
  if (-- self._pending === 0) self.emit('_ready');
});

So this instead:

somethingAsync(this.pendReady(function callback () {
  // ...
}));

The reason I didn't name the method _pendReady is because I want to suggest exposing / advertising it to facilitate creating plugins that need to do some kind of async setup. For example, create a plugin that needs to register a transform in a specific position, but needs to do some async setup before the transform can be invoked, e.g.:

function plugin (b, opts) {
  var real_transform;

  function placeholder (file) {
    return real_transform(file);
  }

  b.transform(placeholder);

  something_async(b.pendReady(function () {
    real_transform = ...
  }));
}

If you approve I can write some documentation.

This factors out the existing logic of incrementing _pending, then on
callback execution decrementing it and emitting _ready if nothing is
pending.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant