tor-browser

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

ast.js (1595B)


      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 /**
      6 * Ast reducer
      7 *
      8 * @module reducers/ast
      9 */
     10 
     11 import { makeBreakpointId } from "../utils/breakpoint/index";
     12 
     13 export function initialASTState() {
     14  return {
     15    // We are using mutable objects as we never return the dictionary as-is from the selectors
     16    // but only their values.
     17    // Note that all these dictionaries are storing objects as values
     18    // which all will have:
     19    // * a "source" attribute,
     20    // * a "lines" array.
     21    mutableInScopeLines: new Map(),
     22  };
     23 }
     24 
     25 function update(state = initialASTState(), action) {
     26  switch (action.type) {
     27    case "IN_SCOPE_LINES": {
     28      state.mutableInScopeLines.set(makeBreakpointId(action.location), {
     29        lines: action.lines,
     30        source: action.location.source,
     31      });
     32      return {
     33        ...state,
     34      };
     35    }
     36 
     37    case "RESUME": {
     38      return initialASTState();
     39    }
     40 
     41    case "REMOVE_SOURCES": {
     42      const { sources } = action;
     43      if (!sources.length) {
     44        return state;
     45      }
     46      const { mutableInScopeLines } = state;
     47      let changed = false;
     48      for (const [breakpointId, { source }] in mutableInScopeLines.entries()) {
     49        if (sources.includes(source)) {
     50          mutableInScopeLines.delete(breakpointId);
     51          changed = true;
     52        }
     53      }
     54 
     55      return changed ? { ...state } : state;
     56    }
     57 
     58    default: {
     59      return state;
     60    }
     61  }
     62 }
     63 
     64 export default update;