Bug 1924993 - [devtools] Debugger tests wait before typing in conditional panel r...
[gecko.git] / devtools / client / webconsole / middleware / history-persistence.js
blobb6b6ca9f7c5c30e19eddce02559254de0aaa45ec
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 const {
8 APPEND_TO_HISTORY,
9 CLEAR_HISTORY,
10 EVALUATE_EXPRESSION,
11 } = require("resource://devtools/client/webconsole/constants.js");
13 const historyActions = require("resource://devtools/client/webconsole/actions/history.js");
15 loader.lazyRequireGetter(
16 this,
17 "asyncStorage",
18 "resource://devtools/shared/async-storage.js"
21 /**
22 * History persistence middleware is responsible for loading
23 * and maintaining history of executed expressions in JSTerm.
25 function historyPersistenceMiddleware(webConsoleUI, store) {
26 let historyLoaded = false;
27 asyncStorage.getItem("webConsoleHistory").then(
28 value => {
29 if (Array.isArray(value)) {
30 store.dispatch(historyActions.historyLoaded(value));
32 historyLoaded = true;
34 err => {
35 historyLoaded = true;
36 console.error(err);
40 return next => action => {
41 const res = next(action);
43 const triggerStoreActions = [
44 APPEND_TO_HISTORY,
45 CLEAR_HISTORY,
46 EVALUATE_EXPRESSION,
49 // Save the current history entries when modified, but wait till
50 // entries from the previous session are loaded.
51 const { isPrivate } =
52 webConsoleUI.hud?.commands?.targetCommand?.targetFront?.targetForm || {};
54 if (
55 !isPrivate &&
56 historyLoaded &&
57 triggerStoreActions.includes(action.type)
58 ) {
59 const state = store.getState();
60 asyncStorage
61 .setItem("webConsoleHistory", state.history.entries)
62 .catch(e => {
63 console.error("Error when saving WebConsole input history", e);
64 });
67 return res;
71 module.exports = historyPersistenceMiddleware;