tor-browser

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

reducer.js (3853B)


      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 function initialOIState(overrides) {
      6  return {
      7    expandedPaths: new Set(),
      8    loadedProperties: new Map(),
      9    evaluations: new Map(),
     10    watchpoints: new Map(),
     11    ...overrides,
     12  };
     13 }
     14 
     15 function reducer(state = initialOIState(), action = {}) {
     16  const { type, data } = action;
     17 
     18  const cloneState = overrides => ({ ...state, ...overrides });
     19 
     20  if (type === "NODE_EXPAND") {
     21    return cloneState({
     22      expandedPaths: new Set(state.expandedPaths).add(data.node.path),
     23    });
     24  }
     25 
     26  if (type === "NODE_COLLAPSE") {
     27    const expandedPaths = new Set(state.expandedPaths);
     28    expandedPaths.delete(data.node.path);
     29    return cloneState({ expandedPaths });
     30  }
     31 
     32  if (type == "SET_WATCHPOINT") {
     33    const { watchpoint, property, path } = data;
     34    const obj = state.loadedProperties.get(path);
     35 
     36    return cloneState({
     37      loadedProperties: new Map(state.loadedProperties).set(
     38        path,
     39        updateObject(obj, property, watchpoint)
     40      ),
     41      watchpoints: new Map(state.watchpoints).set(data.actor, data.watchpoint),
     42    });
     43  }
     44 
     45  if (type === "REMOVE_WATCHPOINT") {
     46    const { path, property, actor } = data;
     47    const obj = state.loadedProperties.get(path);
     48    const watchpoints = new Map(state.watchpoints);
     49    watchpoints.delete(actor);
     50 
     51    return cloneState({
     52      loadedProperties: new Map(state.loadedProperties).set(
     53        path,
     54        updateObject(obj, property, null)
     55      ),
     56      watchpoints,
     57    });
     58  }
     59 
     60  if (type === "NODE_PROPERTIES_LOADED") {
     61    return cloneState({
     62      loadedProperties: new Map(state.loadedProperties).set(
     63        data.node.path,
     64        action.data.properties
     65      ),
     66    });
     67  }
     68 
     69  if (type === "ROOTS_CHANGED") {
     70    return cloneState();
     71  }
     72 
     73  if (type === "GETTER_INVOKED") {
     74    return cloneState({
     75      evaluations: new Map(state.evaluations).set(data.node.path, {
     76        getterValue:
     77          data.result &&
     78          data.result.value &&
     79          (data.result.value.throw || data.result.value.return),
     80      }),
     81    });
     82  }
     83 
     84  // NOTE: we clear the state on resume because otherwise the scopes pane
     85  // would be out of date. Bug 1514760
     86  // we clear the state when selecting a thread or frame because otherwise the
     87  // preview popup could show outdated values. Bug 1954182
     88  if (type === "RESUME" || type === "PAUSED" || type == "NAVIGATE" || type == "SELECT_THREAD" || type == "SELECT_FRAME") {
     89      return initialOIState({ watchpoints: state.watchpoints });
     90  }
     91 
     92  return state;
     93 }
     94 
     95 function updateObject(obj, property, watchpoint) {
     96  return {
     97    ...obj,
     98    ownProperties: {
     99      ...obj.ownProperties,
    100      [property]: {
    101        ...obj.ownProperties[property],
    102        watchpoint,
    103      },
    104    },
    105  };
    106 }
    107 
    108 function getObjectInspectorState(state) {
    109  return state.objectInspector || state;
    110 }
    111 
    112 function getExpandedPaths(state) {
    113  return getObjectInspectorState(state).expandedPaths;
    114 }
    115 
    116 function getExpandedPathKeys(state) {
    117  return [...getExpandedPaths(state).keys()];
    118 }
    119 
    120 function getWatchpoints(state) {
    121  return getObjectInspectorState(state).watchpoints;
    122 }
    123 
    124 function getLoadedProperties(state) {
    125  return getObjectInspectorState(state).loadedProperties;
    126 }
    127 
    128 function getLoadedPropertyKeys(state) {
    129  return [...getLoadedProperties(state).keys()];
    130 }
    131 
    132 function getEvaluations(state) {
    133  return getObjectInspectorState(state).evaluations;
    134 }
    135 
    136 const selectors = {
    137  getWatchpoints,
    138  getEvaluations,
    139  getExpandedPathKeys,
    140  getExpandedPaths,
    141  getLoadedProperties,
    142  getLoadedPropertyKeys,
    143 };
    144 
    145 Object.defineProperty(module.exports, "__esModule", {
    146  value: true,
    147 });
    148 module.exports = { ...selectors, initialOIState };
    149 module.exports.default = reducer;