Skip to content

Commit

Permalink
chore(gatsby): convert loadNodeContent to async and cache it (#25195)
Browse files Browse the repository at this point in the history
* chore(gatsby): convert func with promise to async

* No need to wrap this in a promise anymore

* Need to await because using the resulting value locally

Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
  • Loading branch information
pvdz and gatsbybot committed Jun 22, 2020
1 parent 50854fb commit 1364453
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions packages/gatsby/src/db/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,32 @@ interface NodeStore {
/**
* Get content for a node from the plugin that created it.
*
* @param {Object} node
* @returns {promise}
* @param {IGatsbyNode} node
* @returns {Promise<string>}
*/
function loadNodeContent(node) {
async function loadNodeContent(node) {
if (_.isString(node.internal.content)) {
return Promise.resolve(node.internal.content)
} else {
return new Promise(resolve => {
// Load plugin's loader function
const plugin = store
.getState()
.flattenedPlugins.find(plug => plug.name === node.internal.owner)
const { loadNodeContent } = require(plugin.resolve)
if (!loadNodeContent) {
throw new Error(
`Could not find function loadNodeContent for plugin ${plugin.name}`
)
}

return loadNodeContent(node).then(content => {
// TODO update node's content field here.
resolve(content)
})
})
return node.internal.content
}

// Load plugin's loader function
const plugin = store
.getState()
.flattenedPlugins.find(plug => plug.name === node.internal.owner)

const { loadNodeContent } = require(plugin.resolve)

if (!loadNodeContent) {
throw new Error(
`Could not find function loadNodeContent for plugin ${plugin.name}`
)
}

const content = await loadNodeContent(node)

node.internal.content = content

return content
}

module.exports = {
Expand Down

0 comments on commit 1364453

Please sign in to comment.