Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

πŸ¦– Adds small util for json deep clone

Alicia Sykes4d22e2cd89af
diff --git a/src/utils/safeClone.js b/src/utils/safeClone.js
new file mode 100644
index 00000000..71e7cf60
--- /dev/null
+++ b/src/utils/safeClone.js
@@ -0,0 +1,18 @@
+/* Deep-clone that tolerates reactive proxies. `structuredClone` throws
+ * `DataCloneError` on Vue's proxies in some browsers (Firefox); JSON
+ * round-trip is a safe fallback for plain JSON-shaped data. */
+import ErrorHandler from '@/utils/ErrorHandler';
+
+export default function safeClone(val, fallback = null) {
+ if (val == null) return fallback;
+ try {
+ return structuredClone(val);
+ } catch {
+ try {
+ return JSON.parse(JSON.stringify(val));
+ } catch (e) {
+ ErrorHandler('safeClone failed', e);
+ return fallback ?? (Array.isArray(val) ? [] : {});
+ }
+ }
+}