tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

history-persistence.js (1874B)


      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/. */
      4 
      5 "use strict";
      6 
      7 const {
      8  APPEND_TO_HISTORY,
      9  CLEAR_HISTORY,
     10  EVALUATE_EXPRESSION,
     11 } = require("resource://devtools/client/webconsole/constants.js");
     12 
     13 const historyActions = require("resource://devtools/client/webconsole/actions/history.js");
     14 
     15 loader.lazyRequireGetter(
     16  this,
     17  "asyncStorage",
     18  "resource://devtools/shared/async-storage.js"
     19 );
     20 
     21 /**
     22 * History persistence middleware is responsible for loading
     23 * and maintaining history of executed expressions in JSTerm.
     24 */
     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));
     31      }
     32      historyLoaded = true;
     33    },
     34    err => {
     35      historyLoaded = true;
     36      console.error(err);
     37    }
     38  );
     39 
     40  return next => action => {
     41    const res = next(action);
     42 
     43    const triggerStoreActions = [
     44      APPEND_TO_HISTORY,
     45      CLEAR_HISTORY,
     46      EVALUATE_EXPRESSION,
     47    ];
     48 
     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 || {};
     53 
     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        });
     65    }
     66 
     67    return res;
     68  };
     69 }
     70 
     71 module.exports = historyPersistenceMiddleware;