@tschk/moonshine-dom 0.4.4 → 0.4.80

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
@@ -2,9 +2,23 @@
2
2
  import { batch, createSignal, untrack } from "./reactive";
3
3
 
4
4
  type Key = string | number | symbol;
5
- type Range = { from?: number; to?: number; by?: number };
5
+ export type StorePathRange = { from?: number; to?: number; by?: number };
6
+ type Range = StorePathRange;
6
7
  type Selector =
7
8
  Key | readonly Key[] | Range | ((value: any, index: number) => boolean);
9
+ export type Store<T> = T;
10
+ export type NotWrappable =
11
+ string | number | bigint | symbol | boolean | Function | null | undefined;
12
+ export type StoreSetter<T, U extends PropertyKey[] = []> =
13
+ T | Partial<T> | ((prevState: T, traversed: U) => T | Partial<T> | void);
14
+ export interface ReconcileOptions {
15
+ key?: string | null;
16
+ merge?: boolean;
17
+ }
18
+ export const $RAW: unique symbol = Symbol("store-raw");
19
+ export const DEV: { readonly $RAW: typeof $RAW } | undefined = Object.freeze({
20
+ $RAW,
21
+ });
8
22
  type Update<T> =
9
23
  T | Partial<T> | ((previous: T, path: Key[]) => T | Partial<T> | void);
10
24
  export interface SetStoreFunction<T> {
@@ -46,6 +60,37 @@ function raw<T>(value: T): T {
46
60
  : value
47
61
  ) as T;
48
62
  }
63
+ /** Return raw stores and unwrap embedded proxies; direct raw writes bypass tracking. */
64
+ export function unwrap<T>(value: T): T {
65
+ const initial = raw(value);
66
+ if (initial !== value || !wrappable(value)) return initial;
67
+ const seen = new WeakMap<object, unknown>();
68
+ function visit(item: unknown): unknown {
69
+ const target = raw(item);
70
+ if (target !== item || !wrappable(item)) return target;
71
+ if (seen.has(item)) return seen.get(item);
72
+ const result: Record<PropertyKey, any> = Object.isFrozen(item)
73
+ ? Array.isArray(item)
74
+ ? item.slice()
75
+ : { ...item }
76
+ : item;
77
+ seen.set(item, result);
78
+ const replace = (key: string | number) => {
79
+ const previous = result[key];
80
+ const next = visit(previous);
81
+ if (next !== previous) result[key] = next;
82
+ };
83
+ if (Array.isArray(result)) {
84
+ for (let index = 0; index < result.length; index++) replace(index);
85
+ } else {
86
+ for (const key of Object.keys(result))
87
+ if (!Object.getOwnPropertyDescriptor(result, key)?.get) replace(key);
88
+ }
89
+ return result;
90
+ }
91
+ return visit(value) as T;
92
+ }
93
+
49
94
  function wrappable(value: unknown): value is Record<PropertyKey, any> {
50
95
  if (value === null || typeof value !== "object") return false;
51
96
  const prototype = Object.getPrototypeOf(value);
@@ -151,6 +196,7 @@ function wrap<T>(
151
196
  const methods = new Map<PropertyKey, Function>();
152
197
  const proxy: object = new Proxy(value, {
153
198
  get(target, key, receiver) {
199
+ if (key === $RAW) return target;
154
200
  // Undefined constructor selects ordinary arrays without exposing prototypes.
155
201
  if (writable && key === "constructor" && Array.isArray(target))
156
202
  return undefined;
@@ -185,6 +231,7 @@ function wrap<T>(
185
231
  return wrap(result, mode, equals);
186
232
  },
187
233
  has(target, key) {
234
+ if (key === $RAW) return true;
188
235
  if (tracked) track(state(target).has, key);
189
236
  return key in target;
190
237
  },
@@ -344,6 +391,11 @@ export function createMutable<T extends object>(
344
391
  return wrap(target, "mutable", options.equals ?? strictEqual);
345
392
  }
346
393
 
394
+ /** Apply a store modifier (such as produce or reconcile) to a mutable store inside a batched untracked scope. */
395
+ export function modifyMutable<T>(state: T, modifier: (state: T) => T): void {
396
+ batch(() => untrack(() => modifier(raw(state))));
397
+ }
398
+
347
399
  export function produce<T>(fn: (state: T) => void): (state: T) => T {
348
400
  return (value) => {
349
401
  if (wrappable(raw(value)))