Skip to content

Commit

Permalink
Merge pull request #292 from rwjblue/refactor-wait-until
Browse files Browse the repository at this point in the history
Refactor settled to leverage waitUntil.
  • Loading branch information
rwjblue authored Dec 30, 2017
2 parents 7598c5b + db300fa commit 9eb3e32
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 46 deletions.
29 changes: 3 additions & 26 deletions addon-test-support/@ember/test-helpers/settled.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { run } from '@ember/runloop';

import { Promise as EmberPromise } from 'rsvp';
import jQuery from 'jquery';

import Ember from 'ember';
import { nextTick, futureTick } from './-utils';
import { nextTick } from './-utils';
import waitUntil from './wait-until';

// Ember internally tracks AJAX requests in the same way that we do here for
// legacy style "acceptance" tests using the `ember-testing.js` asset provided
Expand Down Expand Up @@ -190,9 +190,6 @@ export function isSettled() {
return true;
}

const TIMEOUTS = [0, 1, 2, 5];
const MAX_TIMEOUT = 10;

/**
Returns a promise that resolves when in a settled state (see `isSettled` for
a definition of "settled state").
Expand All @@ -203,25 +200,5 @@ const MAX_TIMEOUT = 10;
export default function settled() {
let options = arguments[0];

return new EmberPromise(function(resolve) {
// eslint-disable-next-line require-jsdoc
function scheduleCheck(counter) {
let timeout = TIMEOUTS[counter];
if (timeout === undefined) {
timeout = MAX_TIMEOUT;
}

futureTick(function() {
let settled = isSettled(options);
if (settled) {
// Synchronously resolve the promise
run(null, resolve);
} else {
scheduleCheck(counter + 1);
}
}, timeout);
}

scheduleCheck(0);
});
return waitUntil(() => isSettled(options), { timeout: Infinity });
}
48 changes: 28 additions & 20 deletions addon-test-support/@ember/test-helpers/wait-until.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Promise } from 'rsvp';
import { futureTick } from './-utils';

import { nextTick } from './-utils';
const TIMEOUTS = [0, 1, 2, 5, 7];
const MAX_TIMEOUT = 10;

/**
Wait for the provided callback to return a truthy value.
Expand All @@ -21,29 +23,35 @@ export default function waitUntil(callback, options = {}) {
let waitUntilTimedOut = new Error('waitUntil timed out');

return new Promise(function(resolve, reject) {
// starting at -10 because the first invocation happens on 0
// but still increments the time...
let time = -10;
let time = 0;

// eslint-disable-next-line require-jsdoc
function tick() {
time += 10;

let value;
try {
value = callback();
} catch (error) {
reject(error);
function scheduleCheck(timeoutsIndex) {
let interval = TIMEOUTS[timeoutsIndex];
if (interval === undefined) {
interval = MAX_TIMEOUT;
}

if (value) {
resolve(value);
} else if (time < timeout) {
nextTick(tick, 10);
} else {
reject(waitUntilTimedOut);
}
futureTick(function() {
time += interval;

let value;
try {
value = callback();
} catch (error) {
reject(error);
}

if (value) {
resolve(value);
} else if (time < timeout) {
scheduleCheck(timeoutsIndex + 1);
} else {
reject(waitUntilTimedOut);
}
}, interval);
}

nextTick(tick);
scheduleCheck(0);
});
}

0 comments on commit 9eb3e32

Please sign in to comment.