Skip to content

Commit

Permalink
use p-cancelable
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 22, 2017
1 parent e8bf30a commit cd872fa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
3 changes: 2 additions & 1 deletion packages/jest-jasmine2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"jest-matcher-utils": "^20.0.3",
"jest-matchers": "^20.0.3",
"jest-message-util": "^20.0.3",
"jest-snapshot": "^20.0.3"
"jest-snapshot": "^20.0.3",
"p-cancelable": "^0.3.0"
},
"devDependencies": {
"jest-runtime": "^20.0.4"
Expand Down
27 changes: 9 additions & 18 deletions packages/jest-jasmine2/src/queue_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @flow
*/

import PCancelable from 'p-cancelable';
import pTimeout from './p_timeout';

type Options = {
Expand All @@ -24,20 +25,10 @@ type QueueableFn = {
timeout?: () => number,
};

function createCancelToken() {
let cancel;
const promise = new Promise(resolve => {
cancel = resolve;
});

return {
cancel,
promise,
};
}

function queueRunner(options: Options) {
const token = createCancelToken();
const token = new PCancelable((onCancel, resolve) => {
onCancel(resolve);
});

const mapper = ({fn, timeout}) => {
let promise = new Promise(resolve => {
Expand All @@ -60,7 +51,7 @@ function queueRunner(options: Options) {
}
});

promise = Promise.race([promise, token.promise]);
promise = Promise.race([promise, token]);

if (!timeout) {
return promise;
Expand All @@ -80,15 +71,15 @@ function queueRunner(options: Options) {
);
};

const returnPromise = options.queueableFns.reduce(
const res = options.queueableFns.reduce(
(promise, fn) => promise.then(() => mapper(fn)),
Promise.resolve(),
);

return {
cancel: token.cancel,
catch: returnPromise.catch.bind(returnPromise),
then: returnPromise.then.bind(returnPromise),
cancel: token.cancel.bind(token),
catch: res.catch.bind(res),
then: res.then.bind(res),
};
}

Expand Down
10 changes: 4 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4599,6 +4599,10 @@ osenv@^0.1.4:
os-homedir "^1.0.0"
os-tmpdir "^1.0.0"

p-cancelable@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/p-cancelable/-/p-cancelable-0.3.0.tgz#b9e123800bcebb7ac13a479be195b507b98d30fa"

p-finally@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"
Expand Down Expand Up @@ -5208,12 +5212,6 @@ resolve@^1.1.3, resolve@^1.1.4, resolve@^1.1.6, resolve@^1.1.7:
dependencies:
path-parse "^1.0.5"

resolve@^1.3.2:
version "1.3.3"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.3.3.tgz#655907c3469a8680dc2de3a275a8fdd69691f0e5"
dependencies:
path-parse "^1.0.5"

resolve@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.4.0.tgz#a75be01c53da25d934a98ebd0e4c4a7312f92a86"
Expand Down

0 comments on commit cd872fa

Please sign in to comment.