1 | "use strict";
|
2 |
|
3 | const _ = require(`lodash`);
|
4 |
|
5 | const report = require(`gatsby-cli/lib/reporter`);
|
6 |
|
7 | const redux = require(`../redux`);
|
8 |
|
9 | const {
|
10 | emitter
|
11 | } = redux;
|
12 |
|
13 |
|
14 | const dbs = [redux];
|
15 |
|
16 | if (process.env.GATSBY_DB_NODES === `loki`) {
|
17 | dbs.push(require(`./loki`));
|
18 | }
|
19 |
|
20 |
|
21 | let saveInProgress = false;
|
22 |
|
23 | async 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 |
|
36 | const saveStateDebounced = _.debounce(saveState, 1000);
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function startAutosave() {
|
44 | saveStateDebounced();
|
45 | emitter.on(`*`, () => saveStateDebounced());
|
46 | }
|
47 |
|
48 | module.exports = {
|
49 | startAutosave,
|
50 | saveState
|
51 | };
|
52 |
|
\ | No newline at end of file |