-
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.
sync autosaves between loki and redux
- Loading branch information
Showing
4 changed files
with
75 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,51 @@ | ||
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) { | ||
console.log(`save state`) | ||
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