summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/actions/dom-mutation-breakpoints.js
blob: a81c3e3d57bd171ec76aa3a671e03222d801f8ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
"use strict";

const { assert } = require("devtools/shared/DevToolsUtils");
const {
  getDOMMutationBreakpoint,
  getDOMMutationBreakpoints,
} = require("devtools/client/framework/reducers/dom-mutation-breakpoints");

exports.registerWalkerListeners = registerWalkerListeners;
function registerWalkerListeners(store, walker) {
  walker.on("mutations", mutations => handleWalkerMutations(mutations, store));
}

function handleWalkerMutations(mutations, store) {
  // If we got BP updates for detach/unload, we want to drop those nodes from
  // the list of active DOM mutation breakpoints. We explicitly check these
  // cases because BP updates could also happen due to explicitly API
  // operations to add/remove bps.
  const mutationItems = mutations.filter(
    mutation => mutation.type === "mutationBreakpoint"
  );
  if (mutationItems.length > 0) {
    store.dispatch(updateBreakpointsForMutations(mutationItems));
  }
}

exports.createDOMMutationBreakpoint = createDOMMutationBreakpoint;
function createDOMMutationBreakpoint(nodeFront, mutationType) {
  assert(typeof nodeFront === "object" && nodeFront);
  assert(typeof mutationType === "string");

  return async function({ dispatch, getState }) {
    const walker = nodeFront.walkerFront;

    dispatch({
      type: "ADD_DOM_MUTATION_BREAKPOINT",
      nodeFront,
      mutationType,
    });

    await walker.setMutationBreakpoints(nodeFront, {
      [mutationType]: true,
    });
  };
}

exports.deleteDOMMutationBreakpoint = deleteDOMMutationBreakpoint;
function deleteDOMMutationBreakpoint(nodeFront, mutationType) {
  assert(typeof nodeFront === "object" && nodeFront);
  assert(typeof mutationType === "string");

  return async function({ dispatch, getState }) {
    const walker = nodeFront.walkerFront;
    await walker.setMutationBreakpoints(nodeFront, {
      [mutationType]: false,
    });

    dispatch({
      type: "REMOVE_DOM_MUTATION_BREAKPOINT",
      nodeFront,
      mutationType,
    });
  };
}

function updateBreakpointsForMutations(mutationItems) {
  return async function({ dispatch, getState }) {
    const removedNodeFronts = [];
    const changedNodeFronts = new Set();

    for (const { target: nodeFront, mutationReason } of mutationItems) {
      switch (mutationReason) {
        case "api":
          changedNodeFronts.add(nodeFront);
          break;
        default:
          console.error(
            "Unexpected mutation reason",
            mutationReason,
            ", removing"
          );
        // Fall Through
        case "detach":
        case "unload":
          removedNodeFronts.push(nodeFront);
          break;
      }
    }

    if (removedNodeFronts.length > 0) {
      dispatch({
        type: "REMOVE_DOM_MUTATION_BREAKPOINTS_FOR_FRONTS",
        nodeFronts: removedNodeFronts,
      });
    }
    if (changedNodeFronts.size > 0) {
      const enabledStates = [];
      for (const {
        id,
        nodeFront,
        mutationType,
        enabled,
      } of getDOMMutationBreakpoints(getState())) {
        if (changedNodeFronts.has(nodeFront)) {
          const bpEnabledOnFront = nodeFront.mutationBreakpoints[mutationType];
          if (bpEnabledOnFront !== enabled) {
            // Sync the bp state from the front into the store.
            enabledStates.push([id, bpEnabledOnFront]);
          }
        }
      }

      dispatch({
        type: "SET_DOM_MUTATION_BREAKPOINTS_ENABLED_STATE",
        enabledStates,
      });
    }
  };
}

exports.toggleDOMMutationBreakpointState = toggleDOMMutationBreakpointState;
function toggleDOMMutationBreakpointState(id, enabled) {
  assert(typeof id === "string");
  assert(typeof enabled === "boolean");

  return async function({ dispatch, getState }) {
    const bp = getDOMMutationBreakpoint(getState(), id);
    if (!bp) {
      throw new Error(`No DOM mutation BP with ID ${id}`);
    }

    const walker = bp.nodeFront.getParent();
    await walker.setMutationBreakpoints(bp.nodeFront, {
      [bp.mutationType]: enabled,
    });
  };
}