-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby): [loki] sync db autosaves (#10212)
This PR orchestrates the saving of db state for both loki and redux into one place. Previously, loki would use its `autosave` feature and save to disk every 1000ms. Whereas now, it uses the same style as `gatsby/redux` where a debounced save is triggered for each new event. Note that both database save asynchronously (redux's write to disk is async) so this isn't a perfect "Stop the world" synchronized save. However it's better than it was before.
- Loading branch information
Showing
4 changed files
with
74 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const _ = require(`lodash`) | ||
const redux = require(`../redux`) | ||
const { emitter } = redux | ||
|
||
// Even if we are using loki, we still include redux in the list of | ||
// dbs since it still has pages, config, etc. | ||
const dbs = [redux] | ||
if (process.env.GATSBY_DB_NODES === `loki`) { | ||
dbs.push(require(`./loki`)) | ||
} | ||
|
||
// calls `saveState()` on all DBs | ||
function saveState() { | ||
for (const db of dbs) { | ||
db.saveState() | ||
} | ||
} | ||
const saveStateDebounced = _.debounce(saveState, 1000) | ||
|
||
/** | ||
* Sets up listeners so that once bootstrap has finished, all | ||
* databases save their state to disk. If we're in `develop` mode, | ||
* then any new event triggers a debounced save as well. | ||
*/ | ||
function startAutosave() { | ||
// During development, once bootstrap is finished, persist state on changes. | ||
let bootstrapFinished = false | ||
if (process.env.gatsby_executing_command === `develop`) { | ||
emitter.on(`BOOTSTRAP_FINISHED`, () => { | ||
bootstrapFinished = true | ||
saveState() | ||
}) | ||
emitter.on(`*`, () => { | ||
if (bootstrapFinished) { | ||
saveStateDebounced() | ||
} | ||
}) | ||
} | ||
|
||
// During builds, persist state once bootstrap has finished. | ||
if (process.env.gatsby_executing_command === `build`) { | ||
emitter.on(`BOOTSTRAP_FINISHED`, () => { | ||
saveState() | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { | ||
startAutosave, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters