Skip to content

Commit

Permalink
fix(gatsby-transformer-remark): wait for async subplugins (#12578)
Browse files Browse the repository at this point in the history
* move eachPromise to utils file and add (failing) tests

* fix eachPromise
  • Loading branch information
pieh authored Mar 14, 2019
1 parent cac37a9 commit af87e96
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
24 changes: 24 additions & 0 deletions packages/gatsby-transformer-remark/src/__tests__/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { eachPromise } = require(`../utils`)

test(`eachPromise`, async () => {
const args = [`foo`, `bar`, `baz`]

const cb = jest.fn()

await eachPromise(args, arg => {
cb(`Start`, arg)
return new Promise(resolve => {
setTimeout(() => {
cb(`Finish`, arg)
resolve()
}, 200)
})
})

expect(cb).toHaveBeenNthCalledWith(1, `Start`, `foo`)
expect(cb).toHaveBeenNthCalledWith(2, `Finish`, `foo`)
expect(cb).toHaveBeenNthCalledWith(3, `Start`, `bar`)
expect(cb).toHaveBeenNthCalledWith(4, `Finish`, `bar`)
expect(cb).toHaveBeenNthCalledWith(5, `Start`, `baz`)
expect(cb).toHaveBeenNthCalledWith(6, `Finish`, `baz`)
})
14 changes: 1 addition & 13 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const {
cloneTreeUntil,
findLastTextNode,
} = require(`./hast-processing`)
const { eachPromise } = require(`./utils`)

let fileNodes
let pluginsCacheStr = ``
Expand Down Expand Up @@ -68,19 +69,6 @@ const safeGetCache = ({ getCache, cache }) => id => {
return getCache(id)
}

/**
* @template T
* @param {Array<T>} input
* @param {(input: T) => Promise<void>} iterator
* @return Promise<void>
*/
const eachPromise = (input, iterator) =>
input.reduce(
(accumulatorPromise, nextValue) =>
accumulatorPromise.then(() => void iterator(nextValue)),
Promise.resolve()
)

const HeadingType = new GraphQLObjectType({
name: `MarkdownHeading`,
fields: {
Expand Down
12 changes: 12 additions & 0 deletions packages/gatsby-transformer-remark/src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @template T
* @param {Array<T>} input
* @param {(input: T) => Promise<void>} iterator
* @return Promise<void>
*/
exports.eachPromise = (input, iterator) =>
input.reduce(
(accumulatorPromise, nextValue) =>
accumulatorPromise.then(() => iterator(nextValue)),
Promise.resolve()
)

0 comments on commit af87e96

Please sign in to comment.