From c74938e1f3eed916e42fb0c26988f580ae2419f5 Mon Sep 17 00:00:00 2001 From: JATIN Date: Sat, 29 Mar 2025 19:28:45 +0530 Subject: [PATCH 1/2] Fixing Cirular refernce Issue & Some Enhancment --- .../IDE/hooks/useHandleMessageEvent.js | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/client/modules/IDE/hooks/useHandleMessageEvent.js b/client/modules/IDE/hooks/useHandleMessageEvent.js index 68852c6c19..0e78fde566 100644 --- a/client/modules/IDE/hooks/useHandleMessageEvent.js +++ b/client/modules/IDE/hooks/useHandleMessageEvent.js @@ -6,30 +6,49 @@ import { stopSketch, expandConsole } from '../actions/ide'; export default function useHandleMessageEvent() { const dispatch = useDispatch(); + // Function to safely convert objects to strings (handles circular references) + const safeStringify = (obj) => { + const seen = new WeakSet(); + return JSON.stringify(obj, (key, value) => { + if (typeof value === 'object' && value !== null) { + if (seen.has(value)) return '[Circular Reference]'; + seen.add(value); + } + return value; + }); + }; + const handleMessageEvent = (data) => { + if (!data || typeof data !== 'object') return; const { source, messages } = data; - if (source === 'sketch' && Array.isArray(messages)) { - const decodedMessages = messages.map((message) => Decode(message.log)); - decodedMessages.every((message, index, arr) => { - const { data: args } = message; - let hasInfiniteLoop = false; - Object.keys(args).forEach((key) => { - if ( - typeof args[key] === 'string' && - args[key].includes('Exiting potential infinite loop') - ) { - dispatch(stopSketch()); - dispatch(expandConsole()); - hasInfiniteLoop = true; - } - }); - if (hasInfiniteLoop) { - return false; - } - return true; - }); - dispatch(dispatchConsoleEvent(decodedMessages)); + if (source !== 'sketch' || !Array.isArray(messages)) return; + const decodedMessages = messages.map((message) => { + try { + return JSON.parse(safeStringify(Decode(message.log))); + } catch (error) { + console.error('Error decoding message:', error); + return { error: 'Failed to decode message' }; + } + }); + + const hasInfiniteLoop = decodedMessages.some( + (message) => + message?.data && + Object.values(message.data).some( + (arg) => + typeof arg === 'string' && + arg.includes('Exiting potential infinite loop') + ) + ); + + if (hasInfiniteLoop) { + dispatch(stopSketch()); + dispatch(expandConsole()); + return; } + + dispatch(dispatchConsoleEvent(decodedMessages)); }; + return handleMessageEvent; } From b0d4a4b4fe1a9ee45de529c146172f6cda8dfad1 Mon Sep 17 00:00:00 2001 From: JATIN Date: Mon, 31 Mar 2025 18:14:17 +0530 Subject: [PATCH 2/2] Passing Object rather than string '[ circular Refernce]' --- .../IDE/hooks/useHandleMessageEvent.js | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/client/modules/IDE/hooks/useHandleMessageEvent.js b/client/modules/IDE/hooks/useHandleMessageEvent.js index 0e78fde566..5603c1d697 100644 --- a/client/modules/IDE/hooks/useHandleMessageEvent.js +++ b/client/modules/IDE/hooks/useHandleMessageEvent.js @@ -6,31 +6,46 @@ import { stopSketch, expandConsole } from '../actions/ide'; export default function useHandleMessageEvent() { const dispatch = useDispatch(); - // Function to safely convert objects to strings (handles circular references) - const safeStringify = (obj) => { - const seen = new WeakSet(); - return JSON.stringify(obj, (key, value) => { - if (typeof value === 'object' && value !== null) { - if (seen.has(value)) return '[Circular Reference]'; - seen.add(value); - } - return value; - }); + const safeStringify = ( + obj, + depth = 0, + maxDepth = 10, + seen = new WeakMap() + ) => { + if (typeof obj !== 'object' || obj === null) return obj; + + if (depth >= maxDepth) { + if (seen.has(obj)) return '[Circular Reference]'; + } + + seen.set(obj, true); + + return Array.isArray(obj) + ? obj.map((item) => safeStringify(item, depth + 1, maxDepth, seen)) + : Object.fromEntries( + Object.entries(obj).map(([key, value]) => [ + key, + safeStringify(value, depth + 1, maxDepth, seen) + ]) + ); }; const handleMessageEvent = (data) => { if (!data || typeof data !== 'object') return; const { source, messages } = data; if (source !== 'sketch' || !Array.isArray(messages)) return; + const decodedMessages = messages.map((message) => { try { - return JSON.parse(safeStringify(Decode(message.log))); + const decoded = Decode(message.log) ?? '[Unknown Message]'; // Ensure decoding works + return safeStringify(decoded); } catch (error) { console.error('Error decoding message:', error); return { error: 'Failed to decode message' }; } }); + // Detect infinite loop warnings const hasInfiniteLoop = decodedMessages.some( (message) => message?.data &&