@tschk/moonshine-dom 0.0.0-stage → 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/LICENSE +15 -0
- package/README.md +467 -2
- package/package.json +62 -4
- package/src/attributes.ts +56 -0
- package/src/binding.ts +430 -0
- package/src/cleanup-order.ts +16 -0
- package/src/component-instance.ts +291 -0
- package/src/component.ts +187 -0
- package/src/control.ts +469 -0
- package/src/custom-element.ts +54 -0
- package/src/dom.ts +870 -0
- package/src/error.ts +59 -0
- package/src/framework-lazy.ts +20 -0
- package/src/framework-native.ts +141 -0
- package/src/framework-source.ts +28 -0
- package/src/head-ssr.ts +108 -0
- package/src/head.ts +34 -0
- package/src/html.ts +54 -0
- package/src/jsx-types.d.ts +19 -0
- package/src/platform.d.ts +4 -0
- package/src/platform.js +4 -0
- package/src/reactive.ts +693 -0
- package/src/render-scheduler.ts +17 -0
- package/src/resource-ssr.ts +73 -0
- package/src/resource.ts +195 -0
- package/src/rotating-text.ts +38 -0
- package/src/ssr-attempt.ts +64 -0
- package/src/ssr-owner.ts +38 -0
- package/src/ssr.ts +874 -0
- package/src/store.ts +527 -0
- package/src/style.ts +30 -0
- package/src/template-retention.ts +4 -0
package/src/store.ts
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
/** Independent structured state; shares only Moonshine's reactive graph. */
|
|
2
|
+
import { batch, createSignal, untrack } from "./reactive";
|
|
3
|
+
|
|
4
|
+
type Key = string | number | symbol;
|
|
5
|
+
type Range = { from?: number; to?: number; by?: number };
|
|
6
|
+
type Selector =
|
|
7
|
+
Key | readonly Key[] | Range | ((value: any, index: number) => boolean);
|
|
8
|
+
type Update<T> =
|
|
9
|
+
T | Partial<T> | ((previous: T, path: Key[]) => T | Partial<T> | void);
|
|
10
|
+
export interface SetStoreFunction<T> {
|
|
11
|
+
(value: Update<T>): void;
|
|
12
|
+
<K extends keyof T>(key: K, value: Update<T[K]>): void;
|
|
13
|
+
(...path: [...Selector[], unknown]): void;
|
|
14
|
+
}
|
|
15
|
+
type Cell = ReturnType<typeof createSignal<number>>;
|
|
16
|
+
type State = {
|
|
17
|
+
reads: Map<PropertyKey, Cell>;
|
|
18
|
+
has: Map<PropertyKey, Cell>;
|
|
19
|
+
keys?: Cell;
|
|
20
|
+
};
|
|
21
|
+
const states = new WeakMap<object, State>();
|
|
22
|
+
const readonlyProxies = new WeakMap<object, object>();
|
|
23
|
+
const drafts = new WeakMap<object, object>();
|
|
24
|
+
const mutableProxies = new WeakMap<object, object>();
|
|
25
|
+
type Equality = (previous: unknown, next: unknown) => boolean;
|
|
26
|
+
const strictEqual: Equality = (previous, next) => previous === next;
|
|
27
|
+
const mutableEqualities = new WeakMap<Equality, WeakMap<object, object>>();
|
|
28
|
+
const arrayMutations = new Set([
|
|
29
|
+
"push",
|
|
30
|
+
"pop",
|
|
31
|
+
"shift",
|
|
32
|
+
"unshift",
|
|
33
|
+
"splice",
|
|
34
|
+
"sort",
|
|
35
|
+
"reverse",
|
|
36
|
+
"fill",
|
|
37
|
+
"copyWithin",
|
|
38
|
+
]);
|
|
39
|
+
const rawValues = new WeakMap<object, object>();
|
|
40
|
+
const unsafe = (key: PropertyKey) =>
|
|
41
|
+
key === "__proto__" || key === "constructor" || key === "prototype";
|
|
42
|
+
function raw<T>(value: T): T {
|
|
43
|
+
return (
|
|
44
|
+
value !== null && typeof value === "object"
|
|
45
|
+
? (rawValues.get(value) ?? value)
|
|
46
|
+
: value
|
|
47
|
+
) as T;
|
|
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
|
+
|
|
80
|
+
function wrappable(value: unknown): value is Record<PropertyKey, any> {
|
|
81
|
+
if (value === null || typeof value !== "object") return false;
|
|
82
|
+
const prototype = Object.getPrototypeOf(value);
|
|
83
|
+
return (
|
|
84
|
+
Array.isArray(value) || prototype === Object.prototype || prototype === null
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
function state(target: object): State {
|
|
88
|
+
let node = states.get(target);
|
|
89
|
+
if (!node) states.set(target, (node = { reads: new Map(), has: new Map() }));
|
|
90
|
+
return node;
|
|
91
|
+
}
|
|
92
|
+
function track(map: Map<PropertyKey, Cell>, key: PropertyKey): void {
|
|
93
|
+
let cell = map.get(key);
|
|
94
|
+
if (!cell) map.set(key, (cell = createSignal(0, { equals: false })));
|
|
95
|
+
cell[0]();
|
|
96
|
+
}
|
|
97
|
+
function notify(cell: Cell | undefined): void {
|
|
98
|
+
cell?.[1]((version) => version + 1);
|
|
99
|
+
}
|
|
100
|
+
function write(
|
|
101
|
+
target: Record<PropertyKey, any>,
|
|
102
|
+
key: PropertyKey,
|
|
103
|
+
value: unknown,
|
|
104
|
+
assignment = false,
|
|
105
|
+
equals: Equality = strictEqual,
|
|
106
|
+
): void {
|
|
107
|
+
if (unsafe(key)) throw new TypeError(`Unsafe store path: ${String(key)}`);
|
|
108
|
+
value = raw(value);
|
|
109
|
+
const previous = target[key],
|
|
110
|
+
had = key in target,
|
|
111
|
+
own = Object.hasOwn(target, key);
|
|
112
|
+
const deleting = value === undefined && !assignment;
|
|
113
|
+
const setter = Object.getOwnPropertyDescriptor(target, key)?.set;
|
|
114
|
+
if (!deleting && own && equals(previous, value) && !(assignment && setter))
|
|
115
|
+
return;
|
|
116
|
+
if (deleting && !own) return;
|
|
117
|
+
const length = Array.isArray(target) ? target.length : undefined;
|
|
118
|
+
if (
|
|
119
|
+
!(deleting
|
|
120
|
+
? Reflect.deleteProperty(target, key)
|
|
121
|
+
: Reflect.set(
|
|
122
|
+
target,
|
|
123
|
+
key,
|
|
124
|
+
value,
|
|
125
|
+
setter
|
|
126
|
+
? wrap(target, assignment ? "mutable" : "draft", equals)
|
|
127
|
+
: target,
|
|
128
|
+
))
|
|
129
|
+
)
|
|
130
|
+
throw new TypeError(`Cannot update store property: ${String(key)}`);
|
|
131
|
+
const node = states.get(target);
|
|
132
|
+
if (!node) return;
|
|
133
|
+
notify(node.reads.get(key));
|
|
134
|
+
if (had !== key in target) notify(node.has.get(key));
|
|
135
|
+
if (length !== undefined && target.length !== length) {
|
|
136
|
+
for (const [property, cell] of node.reads) {
|
|
137
|
+
if (
|
|
138
|
+
typeof property === "string" &&
|
|
139
|
+
/^(0|[1-9]\d*)$/.test(property) &&
|
|
140
|
+
Number(property) >= target.length &&
|
|
141
|
+
Number(property) < length
|
|
142
|
+
)
|
|
143
|
+
notify(cell);
|
|
144
|
+
}
|
|
145
|
+
for (const [property, cell] of node.has) {
|
|
146
|
+
if (
|
|
147
|
+
typeof property === "string" &&
|
|
148
|
+
/^(0|[1-9]\d*)$/.test(property) &&
|
|
149
|
+
Number(property) >= target.length &&
|
|
150
|
+
Number(property) < length
|
|
151
|
+
)
|
|
152
|
+
notify(cell);
|
|
153
|
+
}
|
|
154
|
+
if (key !== "length") notify(node.reads.get("length"));
|
|
155
|
+
}
|
|
156
|
+
notify(node.keys);
|
|
157
|
+
}
|
|
158
|
+
function wrap<T>(
|
|
159
|
+
value: T,
|
|
160
|
+
mode: "readonly" | "draft" | "mutable" = "readonly",
|
|
161
|
+
equals: Equality = strictEqual,
|
|
162
|
+
): T {
|
|
163
|
+
const writable = mode !== "readonly";
|
|
164
|
+
const tracked = mode !== "draft";
|
|
165
|
+
value = raw(value);
|
|
166
|
+
if (!wrappable(value)) return value;
|
|
167
|
+
if (!Object.isExtensible(value))
|
|
168
|
+
throw new TypeError("Store objects must be extensible");
|
|
169
|
+
let cache =
|
|
170
|
+
mode === "draft"
|
|
171
|
+
? drafts
|
|
172
|
+
: mode === "mutable"
|
|
173
|
+
? mutableProxies
|
|
174
|
+
: readonlyProxies;
|
|
175
|
+
if (mode === "mutable" && equals !== strictEqual) {
|
|
176
|
+
let custom = mutableEqualities.get(equals);
|
|
177
|
+
if (!custom) mutableEqualities.set(equals, (custom = new WeakMap()));
|
|
178
|
+
cache = custom;
|
|
179
|
+
}
|
|
180
|
+
const cached = cache.get(value);
|
|
181
|
+
if (cached) return cached as T;
|
|
182
|
+
const methods = new Map<PropertyKey, Function>();
|
|
183
|
+
const proxy: object = new Proxy(value, {
|
|
184
|
+
get(target, key, receiver) {
|
|
185
|
+
// Undefined constructor selects ordinary arrays without exposing prototypes.
|
|
186
|
+
if (writable && key === "constructor" && Array.isArray(target))
|
|
187
|
+
return undefined;
|
|
188
|
+
if (writable && unsafe(key))
|
|
189
|
+
throw new TypeError(`Unsafe store ${mode} access: ${String(key)}`);
|
|
190
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, key);
|
|
191
|
+
if (
|
|
192
|
+
tracked &&
|
|
193
|
+
!descriptor?.get &&
|
|
194
|
+
(Object.hasOwn(target, key) || typeof target[key] !== "function")
|
|
195
|
+
)
|
|
196
|
+
track(state(target).reads, key);
|
|
197
|
+
const result = Reflect.get(target, key, receiver);
|
|
198
|
+
if (
|
|
199
|
+
mode === "mutable" &&
|
|
200
|
+
Array.isArray(target) &&
|
|
201
|
+
typeof key === "string" &&
|
|
202
|
+
arrayMutations.has(key) &&
|
|
203
|
+
result === Reflect.get(Array.prototype, key)
|
|
204
|
+
) {
|
|
205
|
+
let method = methods.get(key);
|
|
206
|
+
if (!method) {
|
|
207
|
+
method = function (this: unknown, ...args: unknown[]) {
|
|
208
|
+
return batch(() =>
|
|
209
|
+
untrack(() => Reflect.apply(result, this, args)),
|
|
210
|
+
);
|
|
211
|
+
};
|
|
212
|
+
methods.set(key, method);
|
|
213
|
+
}
|
|
214
|
+
return method;
|
|
215
|
+
}
|
|
216
|
+
return wrap(result, mode, equals);
|
|
217
|
+
},
|
|
218
|
+
has(target, key) {
|
|
219
|
+
if (tracked) track(state(target).has, key);
|
|
220
|
+
return key in target;
|
|
221
|
+
},
|
|
222
|
+
ownKeys(target) {
|
|
223
|
+
if (tracked) {
|
|
224
|
+
const node = state(target);
|
|
225
|
+
(node.keys ??= createSignal(0, { equals: false }))[0]();
|
|
226
|
+
}
|
|
227
|
+
return Reflect.ownKeys(target);
|
|
228
|
+
},
|
|
229
|
+
getOwnPropertyDescriptor(target, key) {
|
|
230
|
+
const descriptor = Reflect.getOwnPropertyDescriptor(target, key);
|
|
231
|
+
if (
|
|
232
|
+
!descriptor ||
|
|
233
|
+
!descriptor.configurable ||
|
|
234
|
+
descriptor.get ||
|
|
235
|
+
descriptor.set
|
|
236
|
+
)
|
|
237
|
+
return descriptor;
|
|
238
|
+
return {
|
|
239
|
+
configurable: true,
|
|
240
|
+
enumerable: descriptor.enumerable,
|
|
241
|
+
get: () => Reflect.get(proxy, key),
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
set(target, key, next) {
|
|
245
|
+
if (mode === "mutable")
|
|
246
|
+
batch(() => untrack(() => write(target, key, next, true, equals)));
|
|
247
|
+
else if (writable) write(target, key, next);
|
|
248
|
+
return true;
|
|
249
|
+
},
|
|
250
|
+
deleteProperty(target, key) {
|
|
251
|
+
if (mode === "mutable")
|
|
252
|
+
batch(() =>
|
|
253
|
+
untrack(() => write(target, key, undefined, false, equals)),
|
|
254
|
+
);
|
|
255
|
+
else if (writable) write(target, key, undefined);
|
|
256
|
+
return true;
|
|
257
|
+
},
|
|
258
|
+
defineProperty() {
|
|
259
|
+
throw new TypeError("Store property descriptors cannot be changed");
|
|
260
|
+
},
|
|
261
|
+
setPrototypeOf() {
|
|
262
|
+
throw new TypeError("Store prototypes cannot be changed");
|
|
263
|
+
},
|
|
264
|
+
preventExtensions() {
|
|
265
|
+
throw new TypeError("Store extensibility cannot be changed");
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
cache.set(value, proxy);
|
|
269
|
+
rawValues.set(proxy, value);
|
|
270
|
+
return proxy as T;
|
|
271
|
+
}
|
|
272
|
+
function merge(target: Record<PropertyKey, any>, value: unknown): void {
|
|
273
|
+
if (!wrappable(value))
|
|
274
|
+
throw new TypeError("Store merge requires an object or array");
|
|
275
|
+
for (const key of Object.keys(value)) write(target, key, value[key]);
|
|
276
|
+
}
|
|
277
|
+
function update(
|
|
278
|
+
target: Record<PropertyKey, any>,
|
|
279
|
+
path: unknown[],
|
|
280
|
+
traversed: Key[] = [],
|
|
281
|
+
): void {
|
|
282
|
+
if (path.length > 1) {
|
|
283
|
+
const [selector, ...rest] = path;
|
|
284
|
+
if (Array.isArray(selector)) {
|
|
285
|
+
for (const key of selector) update(target, [key, ...rest], traversed);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (Array.isArray(target) && typeof selector === "function") {
|
|
289
|
+
for (let index = 0; index < target.length; index++)
|
|
290
|
+
if (selector(wrap(target[index]), index))
|
|
291
|
+
update(target, [index, ...rest], traversed);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
if (
|
|
295
|
+
Array.isArray(target) &&
|
|
296
|
+
selector !== null &&
|
|
297
|
+
typeof selector === "object"
|
|
298
|
+
) {
|
|
299
|
+
const { from = 0, to = target.length - 1, by = 1 } = selector as Range;
|
|
300
|
+
if (
|
|
301
|
+
![from, to, by].every(Number.isSafeInteger) ||
|
|
302
|
+
from < 0 ||
|
|
303
|
+
to < -1 ||
|
|
304
|
+
by <= 0
|
|
305
|
+
)
|
|
306
|
+
throw new TypeError(
|
|
307
|
+
"Store ranges require nonnegative integer bounds and a positive integer step",
|
|
308
|
+
);
|
|
309
|
+
for (let index = from; index <= to; index += by)
|
|
310
|
+
update(target, [index, ...rest], traversed);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (!["string", "number", "symbol"].includes(typeof selector))
|
|
314
|
+
throw new TypeError("Invalid store path selector");
|
|
315
|
+
const key =
|
|
316
|
+
typeof selector === "number"
|
|
317
|
+
? String(selector)
|
|
318
|
+
: (selector as PropertyKey);
|
|
319
|
+
if (unsafe(key)) throw new TypeError(`Unsafe store path: ${String(key)}`);
|
|
320
|
+
if (rest.length > 1) {
|
|
321
|
+
if (!wrappable(target[key]))
|
|
322
|
+
throw new TypeError(`Store path is not an object: ${String(key)}`);
|
|
323
|
+
update(target[key], rest, [key, ...traversed]);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const previous = target[key];
|
|
327
|
+
const value =
|
|
328
|
+
typeof rest[0] === "function"
|
|
329
|
+
? rest[0](wrap(previous), [key, ...traversed])
|
|
330
|
+
: rest[0];
|
|
331
|
+
if (raw(value) === previous) return;
|
|
332
|
+
if (wrappable(previous) && wrappable(value) && !Array.isArray(value))
|
|
333
|
+
merge(previous, value);
|
|
334
|
+
else write(target, key, value);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
let value = path[0];
|
|
338
|
+
if (typeof value === "function") value = value(wrap(target), traversed);
|
|
339
|
+
value = raw(value);
|
|
340
|
+
if (value == null || value === target) return;
|
|
341
|
+
if (Array.isArray(target) && Array.isArray(value)) {
|
|
342
|
+
for (let index = 0; index < value.length; index++)
|
|
343
|
+
write(target, String(index), value[index]);
|
|
344
|
+
write(target, "length", value.length);
|
|
345
|
+
} else merge(target, value);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function createStore<T extends object = {}>(
|
|
349
|
+
initial: T = {} as T,
|
|
350
|
+
_options?: { name?: string },
|
|
351
|
+
): [T, SetStoreFunction<T>] {
|
|
352
|
+
const target = raw(initial);
|
|
353
|
+
if (!wrappable(target))
|
|
354
|
+
throw new TypeError("Store root must be a plain object or array");
|
|
355
|
+
if (!Object.isExtensible(target))
|
|
356
|
+
throw new TypeError("Store root must be extensible");
|
|
357
|
+
const setter = (...path: unknown[]) =>
|
|
358
|
+
batch(() =>
|
|
359
|
+
untrack(() => {
|
|
360
|
+
if (!path.length) throw new TypeError("Store setter requires a value");
|
|
361
|
+
update(target, path);
|
|
362
|
+
}),
|
|
363
|
+
);
|
|
364
|
+
return [wrap(target), setter as SetStoreFunction<T>];
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Tracked writable view sharing identity and updates with readonly stores. */
|
|
368
|
+
export function createMutable<T extends object>(
|
|
369
|
+
initial: T,
|
|
370
|
+
options: { equals?: Equality } = {},
|
|
371
|
+
): T {
|
|
372
|
+
const target = raw(initial);
|
|
373
|
+
if (!wrappable(target))
|
|
374
|
+
throw new TypeError("Store root must be a plain object or array");
|
|
375
|
+
return wrap(target, "mutable", options.equals ?? strictEqual);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export function produce<T>(fn: (state: T) => void): (state: T) => T {
|
|
379
|
+
return (value) => {
|
|
380
|
+
if (wrappable(raw(value)))
|
|
381
|
+
batch(() => untrack(() => fn(wrap(value, "draft"))));
|
|
382
|
+
return value;
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
export function reconcile<T>(
|
|
387
|
+
snapshot: T,
|
|
388
|
+
options: { key?: string | null; merge?: boolean } = {},
|
|
389
|
+
): (state: T) => T {
|
|
390
|
+
const key = options.key === undefined ? "id" : options.key;
|
|
391
|
+
function collect(value: unknown, nodes: WeakSet<object>): void {
|
|
392
|
+
value = raw(value);
|
|
393
|
+
if (!wrappable(value) || nodes.has(value)) return;
|
|
394
|
+
nodes.add(value);
|
|
395
|
+
for (const descriptor of Object.values(
|
|
396
|
+
Object.getOwnPropertyDescriptors(value),
|
|
397
|
+
))
|
|
398
|
+
if ("value" in descriptor) collect(descriptor.value, nodes);
|
|
399
|
+
}
|
|
400
|
+
// Context is per invocation: reused modifiers must observe the latest graph.
|
|
401
|
+
type Context = {
|
|
402
|
+
old: WeakSet<object>;
|
|
403
|
+
incoming: WeakSet<object>;
|
|
404
|
+
results: WeakMap<object, object>;
|
|
405
|
+
used: WeakMap<object, object>;
|
|
406
|
+
};
|
|
407
|
+
function apply(
|
|
408
|
+
previous: any,
|
|
409
|
+
next: any,
|
|
410
|
+
root: boolean,
|
|
411
|
+
context: Context,
|
|
412
|
+
): any {
|
|
413
|
+
next = raw(next);
|
|
414
|
+
previous = raw(previous);
|
|
415
|
+
if (!wrappable(next)) return next;
|
|
416
|
+
const mapped = context.results.get(next);
|
|
417
|
+
if (mapped) return mapped;
|
|
418
|
+
// Live snapshot references retain their identity and must never be mutated
|
|
419
|
+
// while reconciling a different branch or an array reorder.
|
|
420
|
+
if (context.old.has(next)) {
|
|
421
|
+
context.results.set(next, next);
|
|
422
|
+
return next;
|
|
423
|
+
}
|
|
424
|
+
if (
|
|
425
|
+
!wrappable(previous) ||
|
|
426
|
+
Array.isArray(previous) !== Array.isArray(next) ||
|
|
427
|
+
(!root && key && previous[key] !== next[key]) ||
|
|
428
|
+
context.incoming.has(previous) ||
|
|
429
|
+
(context.used.has(previous) && context.used.get(previous) !== next)
|
|
430
|
+
) {
|
|
431
|
+
// Reserve a result before descending so new branches can refer back to
|
|
432
|
+
// an already reconciled ancestor. Keep the original snapshot object when
|
|
433
|
+
// none of its data references needed remapping; never mutate that object.
|
|
434
|
+
const replacement = Array.isArray(next)
|
|
435
|
+
? []
|
|
436
|
+
: Object.create(Object.getPrototypeOf(next));
|
|
437
|
+
context.results.set(next, replacement);
|
|
438
|
+
const descriptors = Object.getOwnPropertyDescriptors(next);
|
|
439
|
+
let changed = false;
|
|
440
|
+
for (const property of Reflect.ownKeys(descriptors)) {
|
|
441
|
+
if (unsafe(property))
|
|
442
|
+
throw new TypeError(`Unsafe store path: ${String(property)}`);
|
|
443
|
+
const descriptor = Reflect.get(
|
|
444
|
+
descriptors,
|
|
445
|
+
property,
|
|
446
|
+
) as PropertyDescriptor;
|
|
447
|
+
if ("value" in descriptor) {
|
|
448
|
+
const resolved = apply(undefined, descriptor.value, false, context);
|
|
449
|
+
if (resolved !== raw(descriptor.value)) changed = true;
|
|
450
|
+
descriptor.value = resolved;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (!changed) {
|
|
454
|
+
context.results.set(next, next);
|
|
455
|
+
return next;
|
|
456
|
+
}
|
|
457
|
+
Object.defineProperties(replacement, descriptors);
|
|
458
|
+
return replacement;
|
|
459
|
+
}
|
|
460
|
+
context.results.set(next, previous);
|
|
461
|
+
context.used.set(previous, next);
|
|
462
|
+
if (Array.isArray(previous) && Array.isArray(next)) {
|
|
463
|
+
const keyed =
|
|
464
|
+
next.length &&
|
|
465
|
+
previous.length &&
|
|
466
|
+
(!options.merge || (key && next[0]?.[key] != null));
|
|
467
|
+
if (keyed) {
|
|
468
|
+
const available = new Map<unknown, any[]>();
|
|
469
|
+
const identity = (item: any) =>
|
|
470
|
+
key && item != null ? item[key] : item;
|
|
471
|
+
for (let index = previous.length - 1; index >= 0; index--) {
|
|
472
|
+
const item = previous[index];
|
|
473
|
+
const id = identity(item);
|
|
474
|
+
const list = available.get(id);
|
|
475
|
+
if (list) list.push(item);
|
|
476
|
+
else available.set(id, [item]);
|
|
477
|
+
}
|
|
478
|
+
const values = next.map((item) => {
|
|
479
|
+
const list = available.get(identity(item));
|
|
480
|
+
return apply(list?.pop(), item, false, context);
|
|
481
|
+
});
|
|
482
|
+
for (let index = 0; index < values.length; index++)
|
|
483
|
+
write(previous, String(index), values[index]);
|
|
484
|
+
} else
|
|
485
|
+
for (let index = 0; index < next.length; index++)
|
|
486
|
+
write(
|
|
487
|
+
previous,
|
|
488
|
+
String(index),
|
|
489
|
+
apply(previous[index], next[index], false, context),
|
|
490
|
+
);
|
|
491
|
+
write(previous, "length", next.length);
|
|
492
|
+
} else {
|
|
493
|
+
for (const property of Object.keys(next)) {
|
|
494
|
+
if (unsafe(property))
|
|
495
|
+
throw new TypeError(`Unsafe store path: ${property}`);
|
|
496
|
+
write(
|
|
497
|
+
previous,
|
|
498
|
+
property,
|
|
499
|
+
apply(
|
|
500
|
+
(previous as Record<string, unknown>)[property],
|
|
501
|
+
next[property],
|
|
502
|
+
false,
|
|
503
|
+
context,
|
|
504
|
+
),
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
for (const property of Object.keys(previous))
|
|
508
|
+
if (!Object.hasOwn(next, property) || next[property] === undefined)
|
|
509
|
+
write(previous, property, undefined);
|
|
510
|
+
}
|
|
511
|
+
return previous;
|
|
512
|
+
}
|
|
513
|
+
return (value) =>
|
|
514
|
+
batch(() =>
|
|
515
|
+
untrack(() => {
|
|
516
|
+
const context: Context = {
|
|
517
|
+
old: new WeakSet(),
|
|
518
|
+
incoming: new WeakSet(),
|
|
519
|
+
results: new WeakMap(),
|
|
520
|
+
used: new WeakMap(),
|
|
521
|
+
};
|
|
522
|
+
collect(value, context.old);
|
|
523
|
+
collect(snapshot, context.incoming);
|
|
524
|
+
return wrap(apply(value, snapshot, true, context));
|
|
525
|
+
}),
|
|
526
|
+
);
|
|
527
|
+
}
|
package/src/style.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** Narrow object styles; use a CSS string for delimiters, comments or priorities. */
|
|
2
|
+
export function styleEntries(value: object): Array<[string, string | null]> {
|
|
3
|
+
const prototype = Object.getPrototypeOf(value);
|
|
4
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
5
|
+
throw new TypeError("Style objects must be plain records");
|
|
6
|
+
}
|
|
7
|
+
return Object.entries(value).map(([name, entry]) => {
|
|
8
|
+
if (!/^(?:--[A-Za-z0-9_-]+|-?[a-z][a-z0-9]*(?:-[a-z0-9]+)*)$/.test(name)) {
|
|
9
|
+
throw new TypeError(
|
|
10
|
+
"Style object names must be lowercase CSS names or custom properties",
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
if (entry == null) return [name, null];
|
|
14
|
+
if (
|
|
15
|
+
(typeof entry !== "string" && typeof entry !== "number") ||
|
|
16
|
+
(typeof entry === "number" && !Number.isFinite(entry))
|
|
17
|
+
) {
|
|
18
|
+
throw new TypeError(
|
|
19
|
+
"Style object values must be strings, finite numbers or nullish",
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
const text = String(entry);
|
|
23
|
+
if (/[;{}!]|\/\*|\*\//.test(text)) {
|
|
24
|
+
throw new TypeError(
|
|
25
|
+
"Style object delimiters, comments and priorities require a CSS string",
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return [name, text];
|
|
29
|
+
});
|
|
30
|
+
}
|