tor-browser

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

dom-mutation-breakpoints.js (3789B)


      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 "use strict";
      5 
      6 const initialReducerState = {
      7  counter: 1,
      8  breakpoints: [],
      9 };
     10 
     11 exports.reducer = domMutationBreakpointReducer;
     12 function domMutationBreakpointReducer(state = initialReducerState, action) {
     13  switch (action.type) {
     14    case "ADD_DOM_MUTATION_BREAKPOINT": {
     15      const hasExistingBp = state.breakpoints.some(
     16        bp =>
     17          bp.nodeFront === action.nodeFront &&
     18          bp.mutationType === action.mutationType
     19      );
     20 
     21      if (hasExistingBp) {
     22        break;
     23      }
     24 
     25      state = {
     26        ...state,
     27        counter: state.counter + 1,
     28        breakpoints: [
     29          ...state.breakpoints,
     30          {
     31            id: `${state.counter}`,
     32            nodeFront: action.nodeFront,
     33            targetFront: action.nodeFront.targetFront,
     34            mutationType: action.mutationType,
     35            enabled: true,
     36          },
     37        ],
     38      };
     39      break;
     40    }
     41    case "REMOVE_DOM_MUTATION_BREAKPOINT":
     42      for (const [index, bp] of state.breakpoints.entries()) {
     43        if (
     44          bp.nodeFront === action.nodeFront &&
     45          bp.mutationType === action.mutationType
     46        ) {
     47          state = {
     48            ...state,
     49            breakpoints: [
     50              ...state.breakpoints.slice(0, index),
     51              ...state.breakpoints.slice(index + 1),
     52            ],
     53          };
     54          break;
     55        }
     56      }
     57      break;
     58    case "REMOVE_DOM_MUTATION_BREAKPOINTS_FOR_FRONTS": {
     59      const { nodeFronts } = action;
     60      const nodeFrontSet = new Set(nodeFronts);
     61 
     62      const breakpoints = state.breakpoints.filter(
     63        bp => !nodeFrontSet.has(bp.nodeFront)
     64      );
     65 
     66      // Since we might not have made any actual changes, we verify first
     67      // to avoid unnecessary changes in the state.
     68      if (state.breakpoints.length !== breakpoints.length) {
     69        state = {
     70          ...state,
     71          breakpoints,
     72        };
     73      }
     74      break;
     75    }
     76 
     77    case "REMOVE_TARGET": {
     78      const { targetFront } = action;
     79      // When a target is destroyed, remove breakpoints associated with it.
     80      const breakpoints = state.breakpoints.filter(
     81        bp => bp.targetFront !== targetFront
     82      );
     83 
     84      // Since we might not have made any actual changes, we verify first
     85      // to avoid unnecessary changes in the state.
     86      if (state.breakpoints.length !== breakpoints.length) {
     87        state = {
     88          ...state,
     89          breakpoints,
     90        };
     91      }
     92      break;
     93    }
     94 
     95    case "SET_DOM_MUTATION_BREAKPOINTS_ENABLED_STATE": {
     96      const { enabledStates } = action;
     97      const toUpdateById = new Map(enabledStates);
     98 
     99      const breakpoints = state.breakpoints.map(bp => {
    100        const newBpState = toUpdateById.get(bp.id);
    101        if (typeof newBpState === "boolean" && newBpState !== bp.enabled) {
    102          bp = {
    103            ...bp,
    104            enabled: newBpState,
    105          };
    106        }
    107 
    108        return bp;
    109      });
    110 
    111      // Since we might not have made any actual changes, we verify first
    112      // to avoid unnecessary changes in the state.
    113      if (state.breakpoints.some((bp, i) => breakpoints[i] !== bp)) {
    114        state = {
    115          ...state,
    116          breakpoints,
    117        };
    118      }
    119      break;
    120    }
    121  }
    122  return state;
    123 }
    124 
    125 exports.getDOMMutationBreakpoints = getDOMMutationBreakpoints;
    126 function getDOMMutationBreakpoints(state) {
    127  return state.domMutationBreakpoints.breakpoints;
    128 }
    129 
    130 exports.getDOMMutationBreakpoint = getDOMMutationBreakpoint;
    131 function getDOMMutationBreakpoint(state, id) {
    132  return (
    133    state.domMutationBreakpoints.breakpoints.find(v => v.id === id) || null
    134  );
    135 }