UNPKG

1.09 kBJavaScriptView Raw
1"use strict";
2
3const _ = require(`lodash`);
4
5const report = require(`gatsby-cli/lib/reporter`);
6
7const redux = require(`../redux`);
8
9const {
10 emitter
11} = redux; // Even if we are using loki, we still include redux in the list of
12// dbs since it still has pages, config, etc.
13
14const dbs = [redux];
15
16if (process.env.GATSBY_DB_NODES === `loki`) {
17 dbs.push(require(`./loki`));
18} // calls `saveState()` on all DBs
19
20
21let saveInProgress = false;
22
23async function saveState() {
24 if (saveInProgress) return;
25 saveInProgress = true;
26
27 try {
28 await Promise.all(dbs.map(db => db.saveState()));
29 } catch (err) {
30 report.warn(`Error persisting state: ${err && err.message || err}`);
31 }
32
33 saveInProgress = false;
34}
35
36const saveStateDebounced = _.debounce(saveState, 1000);
37/**
38 * Starts listening to redux actions and triggers a database save to
39 * disk upon any action (debounced to every 1 second)
40 */
41
42
43function startAutosave() {
44 saveStateDebounced();
45 emitter.on(`*`, () => saveStateDebounced());
46}
47
48module.exports = {
49 startAutosave,
50 saveState
51};
52//# sourceMappingURL=index.js.map
\No newline at end of file