Skip to content

Commit 31f1c72

Browse files
Moocarwardpeet
authored andcommitted
refactor(gatsby): expicitly call db saveState and autoSave (#13007)
* [gatsby/db]: expicitly call db saveState and autoSave * don't db.startAutosave() during bootstrap * saveState at beginning of db.startAutosave()
1 parent 0215f74 commit 31f1c72

File tree

4 files changed

+11
-24
lines changed

4 files changed

+11
-24
lines changed

packages/gatsby/src/bootstrap/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const tracer = require(`opentracing`).globalTracer()
2323
const preferDefault = require(`./prefer-default`)
2424
const nodeTracking = require(`../db/node-tracking`)
2525
const withResolverContext = require(`../schema/context`)
26-
require(`../db`).startAutosave()
2726
// Add `util.promisify` polyfill for old node versions
2827
require(`util.promisify/shim`)()
2928

packages/gatsby/src/commands/build.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const bootstrap = require(`../bootstrap`)
77
const apiRunnerNode = require(`../utils/api-runner-node`)
88
const { copyStaticDirs } = require(`../utils/get-static-dir`)
99
const { initTracer, stopTracer } = require(`../utils/tracer`)
10+
const db = require(`../db`)
1011
const chalk = require(`chalk`)
1112
const tracer = require(`opentracing`).globalTracer()
1213
const signalExit = require(`signal-exit`)
@@ -42,6 +43,8 @@ module.exports = async function build(program: BuildArgs) {
4243
parentSpan: buildSpan,
4344
})
4445

46+
await db.saveState()
47+
4548
await apiRunnerNode(`onPreBuild`, {
4649
graphql: graphqlRunner,
4750
parentSpan: buildSpan,

packages/gatsby/src/commands/develop.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const getSslCert = require(`../utils/get-ssl-cert`)
3131
const slash = require(`slash`)
3232
const { initTracer } = require(`../utils/tracer`)
3333
const apiRunnerNode = require(`../utils/api-runner-node`)
34+
const db = require(`../db`)
3435
const telemetry = require(`gatsby-telemetry`)
3536
const detectPortInUseAndPrompt = require(`../utils/detect-port-in-use-and-prompt`)
3637
const onExit = require(`signal-exit`)
@@ -297,6 +298,8 @@ module.exports = async (program: any) => {
297298

298299
program.port = await detectPortInUseAndPrompt(port, rlInterface)
299300

301+
db.startAutosave()
302+
300303
const [compiler] = await startServer(program)
301304

302305
function prepareUrls(protocol, host, port) {

packages/gatsby/src/db/index.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,15 @@ async function saveState() {
2727
const saveStateDebounced = _.debounce(saveState, 1000)
2828

2929
/**
30-
* Sets up listeners so that once bootstrap has finished, all
31-
* databases save their state to disk. If we're in `develop` mode,
32-
* then any new event triggers a debounced save as well.
30+
* Starts listening to redux actions and triggers a database save to
31+
* disk upon any action (debounced to every 1 second)
3332
*/
3433
function startAutosave() {
35-
// During development, once bootstrap is finished, persist state on changes.
36-
let bootstrapFinished = false
37-
if (process.env.gatsby_executing_command === `develop`) {
38-
emitter.on(`BOOTSTRAP_FINISHED`, () => {
39-
bootstrapFinished = true
40-
saveState()
41-
})
42-
emitter.on(`*`, () => {
43-
if (bootstrapFinished) {
44-
saveStateDebounced()
45-
}
46-
})
47-
}
48-
49-
// During builds, persist state once bootstrap has finished.
50-
if (process.env.gatsby_executing_command === `build`) {
51-
emitter.on(`BOOTSTRAP_FINISHED`, () => {
52-
saveState()
53-
})
54-
}
34+
saveStateDebounced()
35+
emitter.on(`*`, () => saveStateDebounced())
5536
}
5637

5738
module.exports = {
5839
startAutosave,
40+
saveState,
5941
}

0 commit comments

Comments
 (0)