Skip to content

Commit c7c3218

Browse files
authored
Fix complex updates (#761)
* Fix complex updates * Add changeset
1 parent 3d3fc5e commit c7c3218

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

.changeset/tough-windows-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@preact/signals-debug": patch
3+
---
4+
5+
Fix reporting complex values to the extension

packages/debug/src/devtools.ts

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,23 @@ class DevToolsCommunicator {
127127
payload: {
128128
updates: updateInfoList.map(({ signal, ...info }) => {
129129
const owners = this.getSignalOwners(signal);
130-
return {
131-
...info,
132-
signalType:
133-
info.type === "effect"
134-
? "effect"
135-
: "_fn" in signal
136-
? "computed"
137-
: "signal",
138-
signalName: this.getSignalName(signal),
139-
signalId: this.getSignalId(signal),
140-
componentNames: owners.length > 0 ? owners : undefined,
141-
};
130+
return info.type === "value"
131+
? {
132+
...info,
133+
newValue: deeplyRemoveFunctions(info.newValue),
134+
prevValue: deeplyRemoveFunctions(info.prevValue),
135+
signalType: "_fn" in signal ? "computed" : "signal",
136+
signalName: this.getSignalName(signal),
137+
signalId: this.getSignalId(signal),
138+
componentNames: owners.length > 0 ? owners : undefined,
139+
}
140+
: {
141+
...info,
142+
signalType: "effect",
143+
signalName: this.getSignalName(signal),
144+
signalId: this.getSignalId(signal),
145+
componentNames: owners.length > 0 ? owners : undefined,
146+
};
142147
}),
143148
},
144149
timestamp: Date.now(),
@@ -220,6 +225,24 @@ class DevToolsCommunicator {
220225
}
221226
}
222227

228+
function deeplyRemoveFunctions(obj: any): any {
229+
if (obj === null || obj === undefined) return obj;
230+
if (typeof obj === "function") return "[Function]";
231+
if (typeof obj !== "object") return obj;
232+
233+
if (Array.isArray(obj)) {
234+
return obj.map(deeplyRemoveFunctions);
235+
}
236+
237+
const result: any = {};
238+
for (const key in obj) {
239+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
240+
result[key] = deeplyRemoveFunctions(obj[key]);
241+
}
242+
}
243+
return result;
244+
}
245+
223246
// Global instance
224247
let devToolsCommunicator: DevToolsCommunicator | null = null;
225248

0 commit comments

Comments
 (0)