From 2b5594479164f8d73747b8b6623d59d6f6c5b3af Mon Sep 17 00:00:00 2001 From: Jordon Leach Date: Thu, 31 Oct 2024 10:55:00 -0400 Subject: [PATCH] Add deepToRaw method for recursively converting proxy objects to raw --- shell/plugins/steve/mutations.js | 5 ++++- shell/utils/object.js | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/shell/plugins/steve/mutations.js b/shell/plugins/steve/mutations.js index 444671bff5..7e9b73e54c 100644 --- a/shell/plugins/steve/mutations.js +++ b/shell/plugins/steve/mutations.js @@ -13,6 +13,7 @@ import { import { perfLoadAll } from '@shell/plugins/steve/performanceTesting'; import { classify } from '@shell/plugins/dashboard-store/classify'; import SteveSchema from '@shell/models/steve-schema'; +import { deepToRaw } from '@shell/utils/object'; function registerNamespace(state, namespace) { let cache = state.podsByNamespace[namespace]; @@ -145,7 +146,9 @@ export default { if (worker) { // Store raw json objects, not the proxies - worker.postMessage({ loadSchemas: data }); + const rawData = deepToRaw(data); + + worker.postMessage({ loadSchemas: rawData }); } } }, diff --git a/shell/utils/object.js b/shell/utils/object.js index dd01ead465..19ac938baa 100644 --- a/shell/utils/object.js +++ b/shell/utils/object.js @@ -1,3 +1,4 @@ +import { toRaw } from 'vue'; import cloneDeep from 'lodash/cloneDeep'; import flattenDeep from 'lodash/flattenDeep'; import compact from 'lodash/compact'; @@ -434,3 +435,35 @@ export function dropKeys(obj, keys) { delete obj[k]; } } + +/** + * Recursively convert a reactive object to a raw object + * @param {*} obj + * @param {*} cache + * @returns + */ +export function deepToRaw(obj, cache = new WeakSet()) { + if (obj === null || typeof obj !== 'object') { + // If obj is null or a primitive, return it as is + return obj; + } + + // If the object has already been processed, return it to prevent circular references + if (cache.has(obj)) { + return obj; + } + cache.add(obj); + + if (Array.isArray(obj)) { + return obj.map((item) => deepToRaw(item, cache)); + } else { + const rawObj = toRaw(obj); + const result = {}; + + for (const key in rawObj) { + result[key] = deepToRaw(rawObj[key], cache); + } + + return result; + } +}