Skip to content

Commit

Permalink
Core: refactor settled to leverage waitUntil
Browse files Browse the repository at this point in the history
Migrates ramp up functionality to `waitUntil` and migrates `settled` to
leverage `waitUntil`.
  • Loading branch information
rwjblue committed Dec 30, 2017
1 parent 7598c5b commit 1c9a169
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 46 deletions.
34 changes: 8 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,10 @@ 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(
() => {
return 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 1c9a169

Please sign in to comment.