From bbb8c36f78af806f6e79310bca9b3c99df107436 Mon Sep 17 00:00:00 2001 From: Anthony Marcar Date: Tue, 16 Apr 2019 18:19:36 -0700 Subject: [PATCH] refactor(gatsby): Move query/page-query-runner to query/index (#13375) * move page-query-runner.js to src/query/index.js * move query-watcher functions into query/index * fix failing tests --- packages/gatsby/src/bootstrap/index.js | 12 +++--- packages/gatsby/src/commands/develop.js | 4 +- .../query/{page-query-runner.js => index.js} | 29 ++++++++++++-- packages/gatsby/src/query/query-watcher.js | 39 ++----------------- .../machines/__tests__/page-component.js | 11 +++--- .../src/redux/machines/page-component.js | 11 +++--- 6 files changed, 46 insertions(+), 60 deletions(-) rename packages/gatsby/src/query/{page-query-runner.js => index.js} (91%) diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index 5bc68dfc6eba7..ef03ab4c6670c 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -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`) @@ -452,16 +452,14 @@ 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(), }) @@ -469,7 +467,7 @@ module.exports = async (args: BootstrapArgs) => { 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( diff --git a/packages/gatsby/src/commands/develop.js b/packages/gatsby/src/commands/develop.js index 467f1228286dc..8e9d08c91a5b4 100644 --- a/packages/gatsby/src/commands/develop.js +++ b/packages/gatsby/src/commands/develop.js @@ -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`) @@ -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() diff --git a/packages/gatsby/src/query/page-query-runner.js b/packages/gatsby/src/query/index.js similarity index 91% rename from packages/gatsby/src/query/page-query-runner.js rename to packages/gatsby/src/query/index.js index 6ee9292313850..2086bd80e86cc 100644 --- a/packages/gatsby/src/query/page-query-runner.js +++ b/packages/gatsby/src/query/index.js @@ -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 = [] @@ -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 @@ -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, @@ -275,4 +295,5 @@ module.exports = { startListening, runQueuedQueries, enqueueExtractedQueryId, + enqueueExtractedPageComponent, } diff --git a/packages/gatsby/src/query/query-watcher.js b/packages/gatsby/src/query/query-watcher.js index b41c2cb8ab90e..9ecc66d29fed8 100644 --- a/packages/gatsby/src/query/query-watcher.js +++ b/packages/gatsby/src/query/query-watcher.js @@ -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 = () => { @@ -89,7 +86,7 @@ const handleQuery = ( ) boundActionCreators.deleteComponentsDependencies([query.jsonName]) - enqueueExtractedQueryId(query.jsonName) + queryUtil.enqueueExtractedQueryId(query.jsonName) } return true } @@ -153,7 +150,7 @@ const updateStateAndRunQueries = isFirstRun => { `) } - runQueuedQueries() + queryUtil.runQueuedQueries() return null }) @@ -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 => { @@ -251,9 +221,6 @@ const debounceCompile = _.debounce(() => { updateStateAndRunQueries() }, 100) -exports.watchComponent = watchComponent -exports.debounceCompile = debounceCompile - const watch = rootDir => { if (watcher) return diff --git a/packages/gatsby/src/redux/machines/__tests__/page-component.js b/packages/gatsby/src/redux/machines/__tests__/page-component.js index ee826c17f009f..c01358a8bc7de 100644 --- a/packages/gatsby/src/redux/machines/__tests__/page-component.js +++ b/packages/gatsby/src/redux/machines/__tests__/page-component.js @@ -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( @@ -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`, () => { @@ -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 () => { @@ -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) }) }) diff --git a/packages/gatsby/src/redux/machines/page-component.js b/packages/gatsby/src/redux/machines/page-component.js index 6e00b36298df1..132071545af4e 100644 --- a/packages/gatsby/src/redux/machines/page-component.js +++ b/packages/gatsby/src/redux/machines/page-component.js @@ -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({ @@ -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)