tor-browser

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

source-actors.js (3535B)


      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 * This reducer stores the list of all source actors as well their breakable lines.
      7 *
      8 * There is a one-one relationship with Source Actors from the server codebase,
      9 * as well as SOURCE Resources distributed by the ResourceCommand API.
     10 *
     11 */
     12 function initialSourceActorsState() {
     13  return {
     14    // Map(Source Actor ID: string => SourceActor: object)
     15    // See create.js: `createSourceActor` for the shape of the source actor objects.
     16    mutableSourceActors: new Map(),
     17 
     18    // Map(Source Actor ID: string => Breakable lines: Promise or Array<Number>)
     19    // The array is the list of all lines where breakpoints can be set.
     20    // The value can be a promise to indicate the lines are being loaded.
     21    mutableBreakableLines: new Map(),
     22 
     23    // Set(Source Actor ID: string)
     24    // List of all IDs of source actor which have a valid related source map / original source.
     25    // The SourceActor object may have a sourceMapURL attribute set,
     26    // but this may be invalid. The source map URL or source map file content may be invalid.
     27    // In these scenarios we will remove the source actor from this set.
     28    mutableSourceActorsWithSourceMap: new Set(),
     29 
     30    // Map(Source Actor ID: string => string)
     31    // Store the exception message when processing the sourceMapURL field of the source actor.
     32    mutableSourceMapErrors: new Map(),
     33 
     34    // Map(Source Actor ID: string => string)
     35    // When a bundle has a functional sourcemap, reports the resolved source map URL.
     36    mutableResolvedSourceMapURL: new Map(),
     37  };
     38 }
     39 
     40 export const initial = initialSourceActorsState();
     41 
     42 export default function update(state = initialSourceActorsState(), action) {
     43  switch (action.type) {
     44    case "INSERT_SOURCE_ACTORS": {
     45      for (const sourceActor of action.sourceActors) {
     46        state.mutableSourceActors.set(sourceActor.id, sourceActor);
     47 
     48        // If the sourceMapURL attribute is set, consider that it is valid.
     49        // But this may be revised later and removed from this Set.
     50        if (sourceActor.sourceMapURL) {
     51          state.mutableSourceActorsWithSourceMap.add(sourceActor.id);
     52        }
     53      }
     54      return {
     55        ...state,
     56      };
     57    }
     58 
     59    case "REMOVE_SOURCES": {
     60      if (!action.actors.length) {
     61        return state;
     62      }
     63      for (const { id } of action.actors) {
     64        state.mutableSourceActors.delete(id);
     65        state.mutableBreakableLines.delete(id);
     66        state.mutableSourceActorsWithSourceMap.delete(id);
     67      }
     68      return {
     69        ...state,
     70      };
     71    }
     72 
     73    case "SET_SOURCE_ACTOR_BREAKABLE_LINES":
     74      state.mutableBreakableLines.set(
     75        action.sourceActor.id,
     76        action.promise || action.breakableLines
     77      );
     78 
     79      return {
     80        ...state,
     81      };
     82 
     83    case "CLEAR_SOURCE_ACTOR_MAP_URL":
     84      if (
     85        state.mutableSourceActorsWithSourceMap.delete(action.sourceActor.id)
     86      ) {
     87        return {
     88          ...state,
     89        };
     90      }
     91      return state;
     92 
     93    case "SOURCE_MAP_ERROR": {
     94      state.mutableSourceMapErrors.set(
     95        action.sourceActor.id,
     96        action.errorMessage
     97      );
     98      return { ...state };
     99    }
    100 
    101    case "RESOLVED_SOURCEMAP_URL": {
    102      state.mutableResolvedSourceMapURL.set(
    103        action.sourceActor.id,
    104        action.resolvedSourceMapURL
    105      );
    106      return { ...state };
    107    }
    108  }
    109 
    110  return state;
    111 }