Skip to content

Commit

Permalink
refactor(gatsby): Move query/page-query-runner to query/index (gatsby…
Browse files Browse the repository at this point in the history
…js#13375)

* move page-query-runner.js to src/query/index.js

* move query-watcher functions into query/index

* fix failing tests
  • Loading branch information
Moocar authored Apr 17, 2019
1 parent d4e09f2 commit bbb8c36
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 60 deletions.
12 changes: 5 additions & 7 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ process.on(`unhandledRejection`, (reason, p) => {
})

const { extractQueries } = require(`../query/query-watcher`)
const pageQueryRunner = require(`../query/page-query-runner`)
const queryUtil = require(`../query`)
const { writePages } = require(`../query/pages-writer`)
const { writeRedirects } = require(`./redirects-writer`)

Expand Down Expand Up @@ -452,24 +452,22 @@ module.exports = async (args: BootstrapArgs) => {
require(`./page-hot-reloader`)(graphqlRunner)
}

const queryIds = pageQueryRunner.calcInitialDirtyQueryIds(store.getState())
const { staticQueryIds, pageQueryIds } = pageQueryRunner.groupQueryIds(
queryIds
)
const queryIds = queryUtil.calcInitialDirtyQueryIds(store.getState())
const { staticQueryIds, pageQueryIds } = queryUtil.groupQueryIds(queryIds)

activity = report.activityTimer(`run static queries`, {
parentSpan: bootstrapSpan,
})
activity.start()
await pageQueryRunner.processStaticQueries(staticQueryIds, {
await queryUtil.processStaticQueries(staticQueryIds, {
activity,
state: store.getState(),
})
activity.end()

activity = report.activityTimer(`run page queries`)
activity.start()
await pageQueryRunner.processPageQueries(pageQueryIds, { activity })
await queryUtil.processPageQueries(pageQueryIds, { activity })
activity.end()

require(`../redux/actions`).boundActionCreators.setProgramStatus(
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const db = require(`../db`)
const telemetry = require(`gatsby-telemetry`)
const detectPortInUseAndPrompt = require(`../utils/detect-port-in-use-and-prompt`)
const onExit = require(`signal-exit`)
const pageQueryRunner = require(`../query/page-query-runner`)
const queryUtil = require(`../query`)
const queryQueue = require(`../query/queue`)
const queryWatcher = require(`../query/query-watcher`)

Expand Down Expand Up @@ -92,7 +92,7 @@ async function startServer(program) {
await bootstrap(program)

db.startAutosave()
pageQueryRunner.startListening(queryQueue.createDevelopQueue())
queryUtil.startListening(queryQueue.createDevelopQueue())
queryWatcher.startWatchDeletePage()

await createIndexHtml()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Queue = require(`better-queue`)
const convertHrtime = require(`convert-hrtime`)
const { store, emitter } = require(`../redux`)
const queryQueue = require(`./queue`)
const { boundActionCreators } = require(`../redux/actions`)

let seenIdsWithoutDataDependencies = []
let queuedDirtyActions = []
Expand All @@ -26,10 +27,6 @@ emitter.on(`DELETE_NODE`, action => {
queuedDirtyActions.push({ payload: action.payload })
})

const enqueueExtractedQueryId = pathname => {
extractedQueryIds.add(pathname)
}

/////////////////////////////////////////////////////////////////////
// Calculate dirty static/page queries

Expand Down Expand Up @@ -267,6 +264,29 @@ const startListening = queue => {
emitter.on(`API_RUNNING_QUEUE_EMPTY`, runQueuedQueries)
}

const enqueueExtractedQueryId = pathname => {
extractedQueryIds.add(pathname)
}

const getPagesForComponent = componentPath => {
const state = store.getState()
return [...state.pages.values()].filter(
p => p.componentPath === componentPath
)
}

const enqueueExtractedPageComponent = componentPath => {
const pages = getPagesForComponent(componentPath)
// Remove page data dependencies before re-running queries because
// the changing of the query could have changed the data dependencies.
// Re-running the queries will add back data dependencies.
boundActionCreators.deleteComponentsDependencies(
pages.map(p => p.path || p.id)
)
pages.forEach(page => enqueueExtractedQueryId(page.path))
runQueuedQueries()
}

module.exports = {
calcInitialDirtyQueryIds,
groupQueryIds,
Expand All @@ -275,4 +295,5 @@ module.exports = {
startListening,
runQueuedQueries,
enqueueExtractedQueryId,
enqueueExtractedPageComponent,
}
39 changes: 3 additions & 36 deletions packages/gatsby/src/query/query-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ const { store, emitter } = require(`../redux/`)
const { boundActionCreators } = require(`../redux/actions`)
const queryCompiler = require(`./query-compiler`).default
const report = require(`gatsby-cli/lib/reporter`)
const {
enqueueExtractedQueryId,
runQueuedQueries,
} = require(`./page-query-runner`)
const queryUtil = require(`./index`)
const debug = require(`debug`)(`gatsby:query-watcher`)

const getQueriesSnapshot = () => {
Expand Down Expand Up @@ -89,7 +86,7 @@ const handleQuery = (
)

boundActionCreators.deleteComponentsDependencies([query.jsonName])
enqueueExtractedQueryId(query.jsonName)
queryUtil.enqueueExtractedQueryId(query.jsonName)
}
return true
}
Expand Down Expand Up @@ -153,7 +150,7 @@ const updateStateAndRunQueries = isFirstRun => {
`)
}

runQueuedQueries()
queryUtil.runQueuedQueries()

return null
})
Expand Down Expand Up @@ -202,33 +199,6 @@ exports.extractQueries = () => {
})
}

const queueQueriesForPageComponent = componentPath => {
const pages = getPagesForComponent(componentPath)
// Remove page data dependencies before re-running queries because
// the changing of the query could have changed the data dependencies.
// Re-running the queries will add back data dependencies.
boundActionCreators.deleteComponentsDependencies(
pages.map(p => p.path || p.id)
)
pages.forEach(page => enqueueExtractedQueryId(page.path))
runQueuedQueries()
}

const runQueryForPage = path => {
enqueueExtractedQueryId(path)
runQueuedQueries()
}

exports.queueQueriesForPageComponent = queueQueriesForPageComponent
exports.runQueryForPage = runQueryForPage

const getPagesForComponent = componentPath => {
const state = store.getState()
return [...state.pages.values()].filter(
p => p.componentPath === componentPath
)
}

const filesToWatch = new Set()
let watcher
const watchComponent = componentPath => {
Expand All @@ -251,9 +221,6 @@ const debounceCompile = _.debounce(() => {
updateStateAndRunQueries()
}, 100)

exports.watchComponent = watchComponent
exports.debounceCompile = debounceCompile

const watch = rootDir => {
if (watcher) return

Expand Down
11 changes: 6 additions & 5 deletions packages/gatsby/src/redux/machines/__tests__/page-component.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { interpret } = require(`xstate`)
const machine = require(`../page-component`)

jest.mock(`../../../query/query-watcher`)
const { runQueryForPage } = require(`../../../query/query-watcher`)
jest.mock(`../../../query`)
const { enqueueExtractedQueryId, runQueuedQueries } = require(`../../../query`)

const getService = (args = {}) =>
interpret(
Expand All @@ -19,7 +19,8 @@ const sleep = (delay = 50) => new Promise(resolve => setTimeout(resolve, delay))

describe(`bootstrap`, () => {
beforeEach(() => {
runQueryForPage.mockClear()
enqueueExtractedQueryId.mockClear()
runQueuedQueries.mockClear()
})

it(`handles not running queries during bootstrap`, () => {
Expand Down Expand Up @@ -56,7 +57,7 @@ describe(`bootstrap`, () => {
service.send({ type: `NEW_PAGE_CREATED`, path: `/test` })
// there is setTimeout in action handler for `NEW_PAGE_CREATED`
await sleep()
expect(runQueryForPage).not.toBeCalled()
expect(runQueuedQueries).not.toBeCalled()
})

it(`will queue query when page if new page is created after bootstrap`, async () => {
Expand All @@ -65,6 +66,6 @@ describe(`bootstrap`, () => {
service.send({ type: `NEW_PAGE_CREATED`, path })
// there is setTimeout in action handler for `NEW_PAGE_CREATED`
await sleep()
expect(runQueryForPage).toBeCalledWith(path)
expect(runQueuedQueries).toBeCalledWith(path)
})
})
11 changes: 5 additions & 6 deletions packages/gatsby/src/redux/machines/page-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ module.exports = Machine(
},
actions: {
runPageComponentQueries: (context, event) => {
const {
queueQueriesForPageComponent,
} = require(`../../query/query-watcher`)
const queryUtil = require(`../../query`)
// Wait a bit as calling this function immediately triggers
// an Action call which Redux squawks about.
setTimeout(() => {
queueQueriesForPageComponent(context.componentPath)
queryUtil.enqueueExtractedPageComponent(context.componentPath)
}, 0)
},
setQuery: assign({
Expand All @@ -98,12 +96,13 @@ module.exports = Machine(
setPage: assign({
pages: (ctx, event) => {
if (event.path) {
const { runQueryForPage } = require(`../../query/query-watcher`)
const queryUtil = require(`../../query`)
// Wait a bit as calling this function immediately triggers
// an Action call which Redux squawks about.
setTimeout(() => {
if (!ctx.isInBootstrap) {
runQueryForPage(event.path)
queryUtil.enqueueExtractedQueryId(event.path)
queryUtil.runQueuedQueries(event.path)
}
}, 0)

Expand Down

0 comments on commit bbb8c36

Please sign in to comment.