Add deepToRaw method for recursively converting proxy objects to raw

This commit is contained in:
Jordon Leach 2024-10-31 10:55:00 -04:00
parent e2731ebb08
commit 2b55944791
No known key found for this signature in database
GPG Key ID: D68D1B56891F19FC
2 changed files with 37 additions and 1 deletions

View File

@ -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 });
}
}
},

View File

@ -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;
}
}