summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/object-inspector/actions.js
blob: 370f1b161a66052cfe6909866fc313f42b25b6ce (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* 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/>. */

const { loadItemProperties } = require("resource://devtools/client/shared/components/object-inspector/utils/load-properties.js");
const {
  getPathExpression,
  getParentFront,
  getParentGripValue,
  getValue,
  nodeIsBucket,
  getFront,
} = require("resource://devtools/client/shared/components/object-inspector/utils/node.js");
const { getLoadedProperties, getWatchpoints } = require("resource://devtools/client/shared/components/object-inspector/reducer.js");

/**
 * This action is responsible for expanding a given node, which also means that
 * it will call the action responsible to fetch properties.
 */
function nodeExpand(node, actor) {
  return async ({ dispatch }) => {
    dispatch({ type: "NODE_EXPAND", data: { node } });
    dispatch(nodeLoadProperties(node, actor));
  };
}

function nodeCollapse(node) {
  return {
    type: "NODE_COLLAPSE",
    data: { node },
  };
}

/*
 * This action checks if we need to fetch properties, entries, prototype and
 * symbols for a given node. If we do, it will call the appropriate ObjectFront
 * functions.
 */
function nodeLoadProperties(node, actor) {
  return async ({ dispatch, client, getState }) => {
    const state = getState();
    const loadedProperties = getLoadedProperties(state);
    if (loadedProperties.has(node.path)) {
      return;
    }

    try {
      const properties = await loadItemProperties(
        node,
        client,
        loadedProperties
      );

      // If the client does not have a releaseActor function, it means the actors are
      // handled directly by the consumer, so we don't need to track them.
      if (!client || !client.releaseActor) {
        actor = null;
      }

      dispatch(nodePropertiesLoaded(node, actor, properties));
    } catch (e) {
      console.error(e);
    }
  };
}

function nodePropertiesLoaded(node, actor, properties) {
  return {
    type: "NODE_PROPERTIES_LOADED",
    data: { node, actor, properties },
  };
}

/*
 * This action adds a property watchpoint to an object
 */
function addWatchpoint(item, watchpoint) {
  return async function({ dispatch, client }) {
    const { parent, name } = item;
    let object = getValue(parent);

    if (nodeIsBucket(parent)) {
      object = getValue(parent.parent);
    }

    if (!object) {
      return;
    }

    const path = parent.path;
    const property = name;
    const label = getPathExpression(item);
    const actor = object.actor;

    await client.addWatchpoint(object, property, label, watchpoint);

    dispatch({
      type: "SET_WATCHPOINT",
      data: { path, watchpoint, property, actor },
    });
  };
}

/*
 * This action removes a property watchpoint from an object
 */
function removeWatchpoint(item) {
  return async function({ dispatch, client }) {
    const { parent, name } = item;
    let object = getValue(parent);

    if (nodeIsBucket(parent)) {
      object = getValue(parent.parent);
    }

    const property = name;
    const path = parent.path;
    const actor = object.actor;

    await client.removeWatchpoint(object, property);

    dispatch({
      type: "REMOVE_WATCHPOINT",
      data: { path, property, actor },
    });
  };
}

function getActorIDs(roots) {
  if (!roots) {
    return []
  }

  const actorIds = [];
  for (const root of roots) {
    const front = getFront(root);
    if (front?.actorID) {
      actorIds.push(front.actorID);
    }
  }

  return actorIds;
}

function closeObjectInspector(roots) {
  return ({ client }) => {
    releaseActors(client, roots);
  };
}

/*
 * This action is dispatched when the `roots` prop, provided by a consumer of
 * the ObjectInspector (inspector, console, …), is modified. It will clean the
 * internal state properties (expandedPaths, loadedProperties, …) and release
 * the actors consumed with the previous roots.
 * It takes a props argument which reflects what is passed by the upper-level
 * consumer.
 */
function rootsChanged(roots, oldRoots) {
  return ({ dispatch, client }) => {
    releaseActors(client, oldRoots, roots);
    dispatch({
      type: "ROOTS_CHANGED",
      data: roots,
    });
  };
}

/**
 * Release any actors we don't need anymore
 *
 * @param {Object} client: Object with a `releaseActor` method
 * @param {Array} oldRoots: The roots in which we want to cleanup now-unused actors
 * @param {Array} newRoots: The current roots (might have item that are also in oldRoots)
 */
async function releaseActors(client, oldRoots, newRoots = []) {
  if (!client?.releaseActor ) {
    return;
  }

  let actorIdsToRelease = getActorIDs(oldRoots);
  if (newRoots.length) {
    const newActorIds = getActorIDs(newRoots);
    actorIdsToRelease = actorIdsToRelease.filter(id => !newActorIds.includes(id));
  }

  if (!actorIdsToRelease.length) {
    return;
  }
  await Promise.all(actorIdsToRelease.map(client.releaseActor));
}

function invokeGetter(node, receiverId) {
  return async ({ dispatch, client, getState }) => {
    try {
      const objectFront =
        getParentFront(node) ||
        client.createObjectFront(getParentGripValue(node));
      const getterName = node.propertyName || node.name;

      const result = await objectFront.getPropertyValue(getterName, receiverId);
      dispatch({
        type: "GETTER_INVOKED",
        data: {
          node,
          result,
        },
      });
    } catch (e) {
      console.error(e);
    }
  };
}

module.exports = {
  closeObjectInspector,
  invokeGetter,
  nodeExpand,
  nodeCollapse,
  nodeLoadProperties,
  nodePropertiesLoaded,
  rootsChanged,
  addWatchpoint,
  removeWatchpoint,
};