Skip to content

Commit

Permalink
Merge pull request #263 from rwjblue/do-not-modify-backburner-current…
Browse files Browse the repository at this point in the history
…-instance

Refactor settled to avoid triggering a new run-loop.
  • Loading branch information
rwjblue authored Dec 13, 2017
2 parents 577f47c + 069e681 commit a74e395
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions addon-test-support/@ember/test-helpers/settled.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import jQuery from 'jquery';
import Ember from 'ember';
import global from './global';

// TODO: refactor to use `nextTick` from #258
const SET_TIMEOUT = global.setTimeout;
let requests;
function incrementAjaxPendingRequests(_, xhr) {
requests.push(xhr);
Expand All @@ -17,17 +19,18 @@ function decrementAjaxPendingRequests(_, xhr) {
// is possible that in the future this changes to use "true" micro-task
// queues.
//
// The entire point here, is that _whenever_ promises are resolved, this
// The entire point here, is that _whenever_ promises are resolved will be
// before the next run of the JS event loop. Then in the next event loop this
// counter will decrement. In the specific case of AJAX, this means that any
// promises chained off of `$.ajax` will properly have their `.then` called
// _before_ this is decremented (and testing continues)
EmberPromise.resolve().then(() => {
SET_TIMEOUT(() => {
for (let i = 0; i < requests.length; i++) {
if (xhr === requests[i]) {
requests.splice(i, 1);
}
}
});
}, 0);
}

export function _teardownAJAXHooks() {
Expand Down

0 comments on commit a74e395

Please sign in to comment.