ember-run-callback

0.0.0 • Public • Published

ember-run-callback

Provides callback(), which lets you easily write runloop and test aware async code:

Ember.Component.extend({
  actions: {
 
    addCreditCard(card) {
      Stripe.addCard(card, callback(() => {
        this.transitionTo('index');
      }));
    }
 
  }
});

Now, in your tests, you can:

click('.add-credit-card');
andThen(function() {
  assert(currentRouteName() === 'index');
});

The callback() method takes the function which you want to be able to run asynchronously, and returns a wrapped function you can use in it's place.

When you invoke callback(), it will notify Ember that some async activity is taking place (via registerWaiter) so your tests will wait for that activity to complete.

When the returned wrapper function is eventually called, it will call your original function (wrapped in an Ember.run()) call, and notify Ember that this particular async activity has finished.

NOTE: When you invoke callback() to get your wrapper function, Ember is immediately notified of an async activity. This means that if you never call the wrapper function that is returned, Ember will never be notified that the async activity ended, and your tests will hang.

If you need to abort the callback, you can use the supplied cancel() method and pass in the wrapper function you got from callback():

import callback, { cancel } from 'ember-run-callback';
 
let wrapper = callback(function() { /* ... */ }));
cancel(wrapper);

See the related RFC for details and the motivation.

Readme

Keywords

Package Sidebar

Install

npm i ember-run-callback

Weekly Downloads

1

Version

0.0.0

License

MIT

Last publish

Collaborators

  • davewasmer