Skip to content

Commit 56154fb

Browse files
committed
fix(serialization): handle large objects and p5.Font serialization
1 parent 17c045e commit 56154fb

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

client/modules/IDE/hooks/useHandleMessageEvent.js

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export default function useHandleMessageEvent() {
1414
) => {
1515
if (typeof obj !== 'object' || obj === null) return obj;
1616

17+
if (obj && obj.constructor && obj.constructor.name === 'p5.Font') {
18+
return {
19+
_type: 'p5.Font',
20+
name: obj.name || 'Font',
21+
};
22+
}
23+
1724
if (depth >= maxDepth) {
1825
if (seen.has(obj)) return '[Circular Reference]';
1926
}

client/utils/dispatcher.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function notifyListener(message) {
2828
}
2929

3030
function notifyFrames(message) {
31-
const rawMessage = JSON.parse(JSON.stringify(message));
31+
const rawMessage = processMessage(message);
3232
Object.keys(frames).forEach((frameId) => {
3333
const { frame, origin } = frames[frameId];
3434
if (frame && frame.postMessage) {
@@ -37,12 +37,35 @@ function notifyFrames(message) {
3737
});
3838
}
3939

40+
// Helper function to handle p5.Font objects
41+
const handleP5Font = (obj) => {
42+
if (obj && obj.constructor && obj.constructor.name === 'p5.Font') {
43+
return {
44+
_type: 'p5.Font',
45+
name: obj.name || 'Font',
46+
};
47+
}
48+
return obj;
49+
};
50+
51+
// Message processing logic to handle nested objects and arrays
52+
const processMessage = (message) => {
53+
if (typeof message === 'object' && message !== null) {
54+
if (Array.isArray(message)) {
55+
return message.map(processMessage);
56+
}
57+
return Object.fromEntries(
58+
Object.entries(message).map(([key, value]) => [
59+
key,
60+
processMessage(handleP5Font(value))
61+
])
62+
);
63+
}
64+
return message;
65+
};
66+
4067
export function dispatchMessage(message) {
4168
if (!message) return;
42-
43-
// maybe one day i will understand why in the codesandbox
44-
// code they leave notifyListeners in here?
45-
// notifyListener(message);
4669
notifyFrames(message);
4770
}
4871

client/utils/previewEntry.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@ window.objectPaths[blobPath] = 'index.html';
1717

1818
window.loopProtect = loopProtect;
1919

20+
// Helper function to handle p5.Font objects
21+
const handleP5Font = (obj) => {
22+
if (obj && obj.constructor && obj.constructor.name === 'p5.Font') {
23+
return {
24+
_type: 'p5.Font',
25+
name: obj.name || 'Font',
26+
};
27+
}
28+
return obj;
29+
};
30+
2031
const consoleBuffer = [];
2132
const LOGWAIT = 500;
2233
Hook(window.console, (log) => {
34+
// Process p5.Font objects in logs
35+
const processedLog = {
36+
...log,
37+
data: log.data ? log.data.map(handleP5Font) : log.data
38+
};
2339
consoleBuffer.push({
24-
log
40+
log: processedLog
2541
});
2642
});
2743
setInterval(() => {

0 commit comments

Comments
 (0)