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

Refactor settled to leverage waitUntil. #292

Merged
merged 1 commit into from
Dec 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I added another entry. Does It seem bad?

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);
});
}