tor-browser

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

exceptions.js (2082B)


      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 * Exceptions reducer
      7 *
      8 * @module reducers/exceptionss
      9 */
     10 
     11 export function initialExceptionsState() {
     12  return {
     13    // Store exception objects created by actions/exceptions.js's addExceptionFromResources()
     14    // This is keyed by source actor id, and values are arrays of exception objects.
     15    mutableExceptionsMap: new Map(),
     16  };
     17 }
     18 
     19 function update(state = initialExceptionsState(), action) {
     20  switch (action.type) {
     21    case "ADD_EXCEPTION":
     22      return updateExceptions(state, action);
     23    case "REMOVE_SOURCES": {
     24      return removeExceptionsForSourceActors(state, action.actors);
     25    }
     26  }
     27  return state;
     28 }
     29 
     30 function updateExceptions(state, action) {
     31  const { mutableExceptionsMap } = state;
     32  const { exception } = action;
     33  const { sourceActorId } = exception;
     34 
     35  let exceptions = mutableExceptionsMap.get(sourceActorId);
     36  if (!exceptions) {
     37    exceptions = [];
     38    mutableExceptionsMap.set(sourceActorId, exceptions);
     39  } else if (
     40    exceptions.some(({ lineNumber, columnNumber }) => {
     41      return (
     42        lineNumber == exception.lineNumber &&
     43        columnNumber == exception.columnNumber
     44      );
     45    })
     46  ) {
     47    // Avoid adding duplicated exceptions for the same line/column
     48    return state;
     49  }
     50 
     51  // As these arrays are only used by getSelectedSourceExceptions selector method,
     52  // which coalesce multiple arrays and always return new array instance,
     53  // it isn't important to clone these array in case of modification.
     54  exceptions.push(exception);
     55 
     56  return {
     57    ...state,
     58  };
     59 }
     60 
     61 function removeExceptionsForSourceActors(state, sourceActors) {
     62  const { mutableExceptionsMap } = state;
     63  const sizeBefore = mutableExceptionsMap.size;
     64  for (const sourceActor of sourceActors) {
     65    mutableExceptionsMap.delete(sourceActor.id);
     66  }
     67  return mutableExceptionsMap.size != sizeBefore ? { ...state } : state;
     68 }
     69 
     70 export default update;