-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update readme with new config option
- Loading branch information
TJ Simons
committed
Mar 23, 2018
1 parent
b8e5fe6
commit fd277de
Showing
5 changed files
with
129 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/gatsby-source-wordpress/src/__tests__/request-in-queue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
jest.mock(`axios`) | ||
|
||
const requestInQueue = require(`../request-in-queue`) | ||
const axios = require(`axios`) | ||
|
||
axios.mockImplementation(opts => { | ||
if (opts.throw) { throw new Error(opts.throw) } | ||
|
||
return opts.url.slice(opts.url.lastIndexOf(`/`) + 1) | ||
}) | ||
|
||
describe(`requestInQueue`, () => { | ||
let requests | ||
|
||
beforeEach(() => { | ||
requests = [ | ||
{ method: `get`, url: `https://gatsbyjs.org/1` }, | ||
{ method: `get`, url: `https://gatsbyjs.org/2` }, | ||
{ method: `get`, url: `https://gatsbyjs.org/3` }, | ||
{ method: `get`, url: `https://gatsbyjs.org/4` }, | ||
] | ||
}) | ||
|
||
afterEach(() => { | ||
axios.mockClear() | ||
}) | ||
|
||
it(`runs all requests in queue`, async () => { | ||
await requestInQueue(requests) | ||
|
||
requests.forEach((req) => { | ||
expect(axios).toHaveBeenCalledWith(req) | ||
}) | ||
}) | ||
|
||
it(`returns the values in the same order they were requested`, async () => { | ||
const responses = await requestInQueue(requests) | ||
expect(responses).toEqual([`1`, `2`, `3`, `4`]) | ||
}) | ||
|
||
it(`stops any requests when one throws an error`, async () => { | ||
try { | ||
await requestInQueue([{ throw: `error` }, ...requests]) | ||
} catch (err) { | ||
expect(err).toBeDefined() | ||
} | ||
expect(axios).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const Queue = require(`better-queue`) | ||
const Promise = require(`bluebird`) | ||
const request = require(`axios`) | ||
|
||
const _defaults = { | ||
id: `url`, | ||
} | ||
|
||
/** | ||
* [handleQueue description] | ||
* @param {[type]} task [description] | ||
* @param {Function} cb [description] | ||
* @return {[type]} [description] | ||
*/ | ||
async function handleQueue(task, cb) { | ||
try { | ||
const response = await request(task) | ||
cb(null, response) | ||
} catch (err) { | ||
cb(err) | ||
} | ||
} | ||
|
||
/** | ||
* @typedef {Options} | ||
* @type {Object} | ||
* @see For a detailed descriptions of the tasks, | ||
* see {@link https://www.npmjs.com/package/better-queue#full-documentation|better-queue on Github} | ||
*/ | ||
|
||
/** | ||
* Run a series of requests tasks in a queue for better flow control | ||
* | ||
* @param {Object[]} tasks An array of Axios formatted request objects | ||
* @param {Options} opts Options that will be given to better-queue | ||
* @return {Promise} Resolves with the accumulated values from the tasks | ||
*/ | ||
module.exports = function requestInQueue (tasks, opts) { | ||
return new Promise((res, rej) => { | ||
const q = new Queue(handleQueue, { ..._defaults, opts }) | ||
|
||
const taskMap = new Map(tasks.map((t) => { | ||
q.push(t) | ||
return [t.url, null] | ||
})) | ||
|
||
q.on(`task_failed`, (id, err) => { | ||
rej(`${id} failed with err: ${err}`) | ||
q.destroy() | ||
}) | ||
|
||
q.on(`task_finish`, (id, response) => { | ||
taskMap.set(id, response) | ||
}) | ||
|
||
q.on(`drain`, () => res(Array.from(taskMap.values()))) | ||
}) | ||
} |