Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit 2fd4ad7

Browse files
committed
Move the logic to load properties of a node to util file.
This will allow a consumer (e.g. the debugger) to preload properties of a given root and use them as it wishes (e.g., for the debugger, use all the direct properties as root for the ObjectInspector of the Popup preview).
1 parent a431e67 commit 2fd4ad7

File tree

3 files changed

+84
-71
lines changed

3 files changed

+84
-71
lines changed

packages/devtools-reps/src/object-inspector/index.js

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,9 @@ const {
3939
nodeIsPrototype,
4040
nodeIsSetter,
4141
nodeIsWindow,
42-
shouldLoadItemEntries,
43-
shouldLoadItemIndexedProperties,
44-
shouldLoadItemNonIndexedProperties,
45-
shouldLoadItemPrototype,
46-
shouldLoadItemSymbols,
42+
loadItemProperties,
4743
} = require("./utils/node");
4844

49-
const {
50-
enumEntries,
51-
enumIndexedProperties,
52-
enumNonIndexedProperties,
53-
getPrototype,
54-
enumSymbols,
55-
} = require("./utils/client");
56-
5745
import type {
5846
CachedNodes,
5947
LoadedProperties,
@@ -244,68 +232,20 @@ class ObjectInspector extends Component {
244232
if (expand === true) {
245233
const gripItem = getClosestGripNode(item);
246234
const value = getValue(gripItem);
247-
248235
const path = item.path;
249-
const [start, end] = item.meta
250-
? [item.meta.startIndex, item.meta.endIndex]
251-
: [];
252-
253-
let promises = [];
254-
let objectClient;
255-
const getObjectClient = () => {
256-
if (objectClient) {
257-
return objectClient;
258-
}
259-
return this.props.createObjectClient(value);
260-
};
261236

262-
if (shouldLoadItemIndexedProperties(item, loadedProperties)) {
263-
promises.push(enumIndexedProperties(getObjectClient(), start, end));
264-
}
265-
266-
if (shouldLoadItemNonIndexedProperties(item, loadedProperties)) {
267-
promises.push(enumNonIndexedProperties(getObjectClient(), start, end));
268-
}
269-
270-
if (shouldLoadItemEntries(item, loadedProperties)) {
271-
promises.push(enumEntries(getObjectClient(), start, end));
272-
}
273-
274-
if (shouldLoadItemPrototype(item, loadedProperties)) {
275-
promises.push(getPrototype(getObjectClient()));
276-
}
277-
278-
if (shouldLoadItemSymbols(item, loadedProperties)) {
279-
promises.push(enumSymbols(getObjectClient(), start, end));
280-
}
281-
282-
if (promises.length > 0) {
283-
// Set the loading state with the pending promises.
237+
const onItemPropertiesLoaded = loadItemProperties(
238+
item, this.props.createObjectClient, loadedProperties);
239+
if (onItemPropertiesLoaded !== null) {
284240
this.setState((prevState, props) => {
285241
const nextLoading = new Map(prevState.loading);
286-
nextLoading.set(path, promises);
242+
nextLoading.set(path, onItemPropertiesLoaded);
287243
return {
288244
loading: nextLoading
289245
};
290246
});
291247

292-
const responses = await Promise.all(promises);
293-
294-
// Let's loop through the responses to build a single response object.
295-
const response = responses.reduce((accumulator, res) => {
296-
Object.entries(res).forEach(([k, v]) => {
297-
if (accumulator.hasOwnProperty(k)) {
298-
if (Array.isArray(accumulator[k])) {
299-
accumulator[k].push(...v);
300-
} else if (typeof accumulator[k] === "object") {
301-
accumulator[k] = Object.assign({}, accumulator[k], v);
302-
}
303-
} else {
304-
accumulator[k] = v;
305-
}
306-
});
307-
return accumulator;
308-
}, {});
248+
const properties = await onItemPropertiesLoaded;
309249

310250
this.setState((prevState, props) => {
311251
const nextLoading = new Map(prevState.loading);
@@ -320,7 +260,7 @@ class ObjectInspector extends Component {
320260
actors: isRoot
321261
? prevState.actors
322262
: (new Set(prevState.actors)).add(value.actor),
323-
loadedProperties: (new Map(prevState.loadedProperties)).set(path, response),
263+
loadedProperties: (new Map(prevState.loadedProperties)).set(path, properties),
324264
loading: nextLoading,
325265
};
326266
});

packages/devtools-reps/src/object-inspector/tests/component/state.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe("ObjectInspector - state", () => {
111111
// Once all the loading promises are resolved, the loading
112112
// state property should be cleaned up, and actors and loadedProperties
113113
// should have the expected values.
114-
await Promise.all(state.loading.get("root-1"));
114+
await state.loading.get("root-1");
115115

116116
state = wrapper.state();
117117
expect(state.loading.has("root-1")).toBeFalsy();
@@ -131,7 +131,7 @@ describe("ObjectInspector - state", () => {
131131
// Once all the loading promises are resolved, the loading
132132
// state property should be cleaned up, and actors and loadedProperties
133133
// should have the expected values.
134-
await Promise.all(state.loading.get("root-1/__proto__"));
134+
await state.loading.get("root-1/__proto__");
135135
expect(formatObjectInspector(wrapper)).toMatchSnapshot();
136136
state = wrapper.state();
137137

@@ -168,7 +168,7 @@ describe("ObjectInspector - state", () => {
168168
// Once all the loading promises are resolved, the loading
169169
// state property should be cleaned up, and actors and loadedProperties
170170
// should have the expected values.
171-
await Promise.all(state.loading.get("root-2"));
171+
await state.loading.get("root-2");
172172
expect(formatObjectInspector(wrapper)).toMatchSnapshot();
173173

174174
state = wrapper.state();
@@ -187,7 +187,7 @@ describe("ObjectInspector - state", () => {
187187
// Once all the loading promises are resolved, the loading
188188
// state property should be cleaned up, and actors and loadedProperties
189189
// should have the expected values.
190-
await Promise.all(state.loading.get("root-2/__proto__"));
190+
await state.loading.get("root-2/__proto__");
191191
expect(formatObjectInspector(wrapper)).toMatchSnapshot();
192192
state = wrapper.state();
193193

packages/devtools-reps/src/object-inspector/utils/node.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,21 @@ const NODE_TYPES = {
2929
PROTOTYPE: Symbol("__proto__"),
3030
};
3131

32+
const {
33+
enumEntries,
34+
enumIndexedProperties,
35+
enumNonIndexedProperties,
36+
getPrototype,
37+
enumSymbols,
38+
} = require("./client");
39+
3240
import type {
3341
CachedNodes,
3442
GripProperties,
3543
LoadedProperties,
3644
Node,
3745
NodeContents,
46+
ObjectClient,
3847
RdpGrip,
3948
} from "../types";
4049

@@ -762,6 +771,69 @@ function getClosestNonBucketNode(item: Node) : Node | null {
762771
return getClosestNonBucketNode(parent);
763772
}
764773

774+
function loadItemProperties(
775+
item: Node,
776+
createObjectClient: (RdpGrip | NodeContents) => ObjectClient,
777+
loadedProperties: LoadedProperties
778+
) : Promise<Object> | null {
779+
const [start, end] = item.meta
780+
? [item.meta.startIndex, item.meta.endIndex]
781+
: [];
782+
783+
let objectClient;
784+
const getObjectClient = () => {
785+
if (objectClient) {
786+
return objectClient;
787+
}
788+
789+
const gripItem = getClosestGripNode(item);
790+
const value = getValue(gripItem);
791+
return createObjectClient(value);
792+
};
793+
794+
let loadingPromises = [];
795+
if (shouldLoadItemIndexedProperties(item, loadedProperties)) {
796+
loadingPromises.push(enumIndexedProperties(getObjectClient(), start, end));
797+
}
798+
799+
if (shouldLoadItemNonIndexedProperties(item, loadedProperties)) {
800+
loadingPromises.push(enumNonIndexedProperties(getObjectClient(), start, end));
801+
}
802+
803+
if (shouldLoadItemEntries(item, loadedProperties)) {
804+
loadingPromises.push(enumEntries(getObjectClient(), start, end));
805+
}
806+
807+
if (shouldLoadItemPrototype(item, loadedProperties)) {
808+
loadingPromises.push(getPrototype(getObjectClient()));
809+
}
810+
811+
if (shouldLoadItemSymbols(item, loadedProperties)) {
812+
loadingPromises.push(enumSymbols(getObjectClient(), start, end));
813+
}
814+
815+
if (loadingPromises.length === 0) {
816+
return null;
817+
}
818+
819+
return Promise.all(loadingPromises)
820+
.then(responses => responses.reduce((accumulator, res) => {
821+
// Let's loop through the responses to build a single response object.
822+
Object.entries(res).forEach(([k, v]) => {
823+
if (accumulator.hasOwnProperty(k)) {
824+
if (Array.isArray(accumulator[k])) {
825+
accumulator[k].push(...v);
826+
} else if (typeof accumulator[k] === "object") {
827+
accumulator[k] = Object.assign({}, accumulator[k], v);
828+
}
829+
} else {
830+
accumulator[k] = v;
831+
}
832+
});
833+
return accumulator;
834+
}, {}));
835+
}
836+
765837
function shouldLoadItemIndexedProperties(
766838
item: Node,
767839
loadedProperties: LoadedProperties = new Map()
@@ -878,6 +950,7 @@ module.exports = {
878950
nodeNeedsNumericalBuckets,
879951
nodeSupportsNumericalBucketing,
880952
setNodeChildren,
953+
loadItemProperties,
881954
shouldLoadItemEntries,
882955
shouldLoadItemIndexedProperties,
883956
shouldLoadItemNonIndexedProperties,

0 commit comments

Comments
 (0)