personal web client for Bluesky
typescript solidjs bluesky atcute
at trunk 42 lines 817 B view raw
1import { $RAW } from 'solid-js/store'; 2 3export const snapshot = <T>(value: T, map: WeakMap<any, any> = new WeakMap()): T => { 4 const v = value as any; 5 6 if (v == null || typeof v !== 'object') { 7 return v; 8 } 9 10 if (!v[$RAW]) { 11 return v; 12 } 13 14 const cached = map.get(v); 15 if (cached) { 16 return cached; 17 } 18 19 const proto = Object.getPrototypeOf(v); 20 switch (proto) { 21 case Array.prototype: { 22 const result: unknown[] = new Array(v.length); 23 for (let idx = 0, len = v.length; idx < len; idx++) { 24 result[idx] = snapshot(v[idx], map); 25 } 26 27 map.set(v, result); 28 return result as T; 29 } 30 case Object.prototype: { 31 const result: Record<PropertyKey, unknown> = {}; 32 for (const key in v) { 33 result[key] = snapshot(v[key], map); 34 } 35 36 map.set(v, result); 37 return result as T; 38 } 39 } 40 41 return v; 42};