@tschk/moonshine-dom 0.4.4 → 0.4.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/store.ts CHANGED
@@ -46,6 +46,37 @@ function raw<T>(value: T): T {
46
46
  : value
47
47
  ) as T;
48
48
  }
49
+ /** Return raw stores and unwrap embedded proxies; direct raw writes bypass tracking. */
50
+ export function unwrap<T>(value: T): T {
51
+ const initial = raw(value);
52
+ if (initial !== value || !wrappable(value)) return initial;
53
+ const seen = new WeakMap<object, unknown>();
54
+ function visit(item: unknown): unknown {
55
+ const target = raw(item);
56
+ if (target !== item || !wrappable(item)) return target;
57
+ if (seen.has(item)) return seen.get(item);
58
+ const result: Record<PropertyKey, any> = Object.isFrozen(item)
59
+ ? Array.isArray(item)
60
+ ? item.slice()
61
+ : { ...item }
62
+ : item;
63
+ seen.set(item, result);
64
+ const replace = (key: string | number) => {
65
+ const previous = result[key];
66
+ const next = visit(previous);
67
+ if (next !== previous) result[key] = next;
68
+ };
69
+ if (Array.isArray(result)) {
70
+ for (let index = 0; index < result.length; index++) replace(index);
71
+ } else {
72
+ for (const key of Object.keys(result))
73
+ if (!Object.getOwnPropertyDescriptor(result, key)?.get) replace(key);
74
+ }
75
+ return result;
76
+ }
77
+ return visit(value) as T;
78
+ }
79
+
49
80
  function wrappable(value: unknown): value is Record<PropertyKey, any> {
50
81
  if (value === null || typeof value !== "object") return false;
51
82
  const prototype = Object.getPrototypeOf(value);