jq79 0.7.13 → 0.8.0
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/dist/jq79-sw.js +1 -1
- package/dist/jq79.cjs +17 -15
- package/dist/jq79.cjs.map +1 -1
- package/dist/jq79.d.ts +4 -1
- package/dist/jq79.global.js +17 -15
- package/dist/jq79.global.js.map +1 -1
- package/dist/jq79.js +17 -15
- package/dist/jq79.js.map +1 -1
- package/dist/precompile.cjs +1 -1
- package/dist/precompile.cjs.map +1 -1
- package/dist/precompile.js +1 -1
- package/dist/precompile.js.map +1 -1
- package/dist/reactive.d.ts +2 -0
- package/package.json +1 -1
- package/src/jq79.ts +350 -48
- package/src/reactive.ts +102 -7
- package/src/source.ts +1 -1
package/src/reactive.ts
CHANGED
|
@@ -37,10 +37,28 @@ export type ReactiveDeepData<T> = T & {
|
|
|
37
37
|
const getByPath = (obj: Record<string, any>, dotKey: string): any =>
|
|
38
38
|
dotKey.split(".").reduce((acc, key) => (acc == null ? undefined : acc[key]), obj)
|
|
39
39
|
|
|
40
|
+
// what $raw marked: plain data the store holds as it is, unwrapped - so the
|
|
41
|
+
// same as a class instance, below. Keyed by the object itself, so the mark
|
|
42
|
+
// travels with it into every store it is handed to (a prop, a child's store)
|
|
43
|
+
const RAW_VALUES = new WeakSet<object>()
|
|
44
|
+
|
|
45
|
+
// holds `value` as plain data no store will ever wrap: reading it, or anything
|
|
46
|
+
// inside it, records no dependency and goes through no proxy, and changing it
|
|
47
|
+
// in place notifies nobody. What stays reactive is the variable or key holding
|
|
48
|
+
// it - assigning a new value there notifies, and the new value is held raw too
|
|
49
|
+
// when it replaces a raw one, so `data = data.map(...)` keeps the list raw.
|
|
50
|
+
// Svelte's $state.raw (RECORD/2026-11-17.closing-the-gap-with-svelte.md, D)
|
|
51
|
+
export const $raw = <T>(value: T): T => {
|
|
52
|
+
const raw = $toRaw(value)
|
|
53
|
+
if (raw !== null && typeof raw === "object" && !isStore(raw)) RAW_VALUES.add(raw as object)
|
|
54
|
+
return raw
|
|
55
|
+
}
|
|
56
|
+
|
|
40
57
|
// only plain objects and arrays get deep-wrapped by the reactive store;
|
|
41
58
|
// class instances (Component79, Date, DOM nodes, ...) pass through untouched
|
|
42
59
|
// so their identity, prototypes and internals stay intact
|
|
43
60
|
const isPlainData = (value: object): boolean => {
|
|
61
|
+
if (RAW_VALUES.has(value)) return false
|
|
44
62
|
if (Array.isArray(value)) return true
|
|
45
63
|
const proto = Object.getPrototypeOf(value)
|
|
46
64
|
return proto === Object.prototype || proto === null
|
|
@@ -257,18 +275,26 @@ let effectsCreated = 0
|
|
|
257
275
|
// See RECORD/2026-11-12.batch.md
|
|
258
276
|
let batchDepth = 0
|
|
259
277
|
let batched: Map<Effect, Set<Effect>> | null = null
|
|
278
|
+
// the batch is dispatch's, not an explicit $batch (see openDispatch)
|
|
279
|
+
let implicitBatch = false
|
|
260
280
|
|
|
261
281
|
const queueBatched = (matched: Iterable<Effect>, effects: Set<Effect>) => {
|
|
262
|
-
|
|
282
|
+
const queue = (batched ??= new Map())
|
|
283
|
+
for (const effect of matched) {
|
|
284
|
+
// an effect writing what it reads re-runs inside its own loop, as it does
|
|
285
|
+
// outside a batch, and is cut off there with its own message
|
|
286
|
+
if (implicitBatch && effect === activeEffect) effect.run()
|
|
287
|
+
else if (!queue.has(effect)) queue.set(effect, effects)
|
|
288
|
+
}
|
|
263
289
|
}
|
|
264
290
|
|
|
265
291
|
// oldest first, like runMatched. Runs with the batch still open, so what a
|
|
266
292
|
// flushed effect writes queues into the next round of this same flush rather
|
|
267
293
|
// than running mid-round - the batch settles before $batch returns
|
|
268
294
|
const flushBatched = () => {
|
|
269
|
-
for (let rounds = 0; batched
|
|
295
|
+
for (let rounds = 0; batched?.size; rounds++) {
|
|
270
296
|
if (rounds === 100) {
|
|
271
|
-
console.error(
|
|
297
|
+
console.error(`jq79: ${implicitBatch ? "a write" : "a $batch"} kept waking effects for 100 rounds (an effect writes what it reads); giving up on it settling`)
|
|
272
298
|
batched!.clear()
|
|
273
299
|
return
|
|
274
300
|
}
|
|
@@ -284,7 +310,7 @@ const flushBatched = () => {
|
|
|
284
310
|
// Nested batches flush at the outermost. A throw still flushes - the writes it
|
|
285
311
|
// made did land - and is rethrown
|
|
286
312
|
export const $batch = <T>(fn: () => T): T => {
|
|
287
|
-
if (batchDepth++ === 0) batched =
|
|
313
|
+
if (batchDepth++ === 0) batched = null
|
|
288
314
|
try {
|
|
289
315
|
return fn()
|
|
290
316
|
} finally {
|
|
@@ -299,6 +325,42 @@ export const $batch = <T>(fn: () => T): T => {
|
|
|
299
325
|
}
|
|
300
326
|
}
|
|
301
327
|
|
|
328
|
+
// resolves once every effect pending when it was called has run. A batch -
|
|
329
|
+
// explicit, or the one every event handler runs in - is synchronous: it opens
|
|
330
|
+
// and flushes inside one call stack, so by the time any promise callback runs
|
|
331
|
+
// it has flushed. A resolved promise is therefore already "after the flush",
|
|
332
|
+
// and `await $tick()` is the documented way to say it rather than an
|
|
333
|
+
// accident of `await` (RECORD/2026-11-17.closing-the-gap-with-svelte.md, A)
|
|
334
|
+
export const $tick = (): Promise<void> => Promise.resolve()
|
|
335
|
+
|
|
336
|
+
// A write outside any $batch runs the effects it woke right away, as it always
|
|
337
|
+
// has - but what *their* writes wake waits until all of them have run, and then
|
|
338
|
+
// runs once, as a $batch would. Without it a write that two $computed derive
|
|
339
|
+
// from is a glitch: the first one's new value wakes a binding that reads both,
|
|
340
|
+
// which runs against the second one's OLD value before that one recomputes. A
|
|
341
|
+
// windowed list whose row height came from one and its items from the other
|
|
342
|
+
// rendered every row of the old list, unwindowed, for one pass
|
|
343
|
+
// (RECORD/2026-11-15.glitch-free-propagation.md). Still synchronous: all of
|
|
344
|
+
// it has run before the write returns. `batched` is allocated only when
|
|
345
|
+
// something is actually queued, so a write that wakes plain bindings pays a
|
|
346
|
+
// counter and nothing else
|
|
347
|
+
//
|
|
348
|
+
// Open and close rather than a wrapper taking a callback: this is every write's
|
|
349
|
+
// path, and a closure per write was measurable on update10k_x16
|
|
350
|
+
const openDispatch = () => {
|
|
351
|
+
batchDepth = 1
|
|
352
|
+
implicitBatch = true
|
|
353
|
+
}
|
|
354
|
+
const closeDispatch = () => {
|
|
355
|
+
try {
|
|
356
|
+
if (batched) flushBatched()
|
|
357
|
+
} finally {
|
|
358
|
+
batchDepth = 0
|
|
359
|
+
batched = null
|
|
360
|
+
implicitBatch = false
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
302
364
|
// the deps an effect has before its first run, and what indexEffect compares
|
|
303
365
|
// its first run against. Never written to - every run installs a fresh set -
|
|
304
366
|
// so one frozen instance stands in for the two empty sets each effect used to
|
|
@@ -648,7 +710,12 @@ export const $reactive = <T extends Record<string, any>>(data: T): ReactiveDeepD
|
|
|
648
710
|
let sorted = true
|
|
649
711
|
for (let i = 1; sorted && i < ordered.length; i++) sorted = ordered[i - 1].order < ordered[i].order
|
|
650
712
|
if (!sorted) ordered.sort((a, b) => a.order - b.order)
|
|
651
|
-
|
|
713
|
+
openDispatch()
|
|
714
|
+
try {
|
|
715
|
+
for (const effect of ordered) if (effects.has(effect)) effect.run()
|
|
716
|
+
} finally {
|
|
717
|
+
closeDispatch()
|
|
718
|
+
}
|
|
652
719
|
}
|
|
653
720
|
|
|
654
721
|
const notify = (dotKey: string, value: any, isNewKey = false) => {
|
|
@@ -659,7 +726,14 @@ export const $reactive = <T extends Record<string, any>>(data: T): ReactiveDeepD
|
|
|
659
726
|
// store entirely), so dep matching would never wake it up
|
|
660
727
|
if (isNewKey) {
|
|
661
728
|
if (batchDepth) queueBatched(effects, effects)
|
|
662
|
-
else
|
|
729
|
+
else {
|
|
730
|
+
openDispatch()
|
|
731
|
+
try {
|
|
732
|
+
effects.forEach(effect => effect.run())
|
|
733
|
+
} finally {
|
|
734
|
+
closeDispatch()
|
|
735
|
+
}
|
|
736
|
+
}
|
|
663
737
|
}
|
|
664
738
|
// `effects.has` stands in for the membership check `effects.forEach` used
|
|
665
739
|
// to give for free: an effect disposed by an earlier effect in this same
|
|
@@ -945,6 +1019,10 @@ export const $reactive = <T extends Record<string, any>>(data: T): ReactiveDeepD
|
|
|
945
1019
|
// always announces itself - the sweep is its whole point
|
|
946
1020
|
if (!isNewKey && Object.is(target[key], stored) && (stored === null || typeof stored !== "object")) return true
|
|
947
1021
|
const previous = target[key]
|
|
1022
|
+
// a raw value's replacement is raw: the mark is on the key's contents,
|
|
1023
|
+
// the way the author wrote it (`let data = $raw([])`), not on one array
|
|
1024
|
+
if (previous !== null && typeof previous === "object" && RAW_VALUES.has(previous) &&
|
|
1025
|
+
stored !== null && typeof stored === "object" && !isStore(stored)) RAW_VALUES.add(stored)
|
|
948
1026
|
target[key] = stored
|
|
949
1027
|
tombstones?.delete(key) // the key exists again: no claim needed
|
|
950
1028
|
if (isStore(stored)) bridge(stored, dotKey)
|
|
@@ -1265,7 +1343,9 @@ export type Computed<T> = ReactiveDeepData<{ readonly value: T }>
|
|
|
1265
1343
|
|
|
1266
1344
|
export const $computed = <T>(get: () => T): Computed<T> => {
|
|
1267
1345
|
const box = $reactive({ value: undefined as T })
|
|
1346
|
+
let self: Effect | null = null
|
|
1268
1347
|
box.$effect(() => {
|
|
1348
|
+
self ??= activeEffect
|
|
1269
1349
|
let value: T
|
|
1270
1350
|
try {
|
|
1271
1351
|
value = get()
|
|
@@ -1281,5 +1361,20 @@ export const $computed = <T>(get: () => T): Computed<T> => {
|
|
|
1281
1361
|
console.warn(`jq79: a $computed is read-only - the write to ${String(key)} was ignored`)
|
|
1282
1362
|
return true
|
|
1283
1363
|
}
|
|
1284
|
-
|
|
1364
|
+
// read while a batch holds it pending - something it depends on was written
|
|
1365
|
+
// and it has not recomputed yet - it recomputes now, rather than hand back
|
|
1366
|
+
// the value from before the write. Without this, every event handler (which
|
|
1367
|
+
// runs in a batch) that writes and then reads what it derives would read the
|
|
1368
|
+
// old value. Its own run reading itself is left alone, as is a computed
|
|
1369
|
+
// nobody woke: `batched` is null or lacks it, and the read is a Map lookup
|
|
1370
|
+
const read = (target: typeof box, key: string | symbol) => {
|
|
1371
|
+
const owner = key === "value" && self && activeEffect !== self ? batched?.get(self) : undefined
|
|
1372
|
+
if (owner) {
|
|
1373
|
+
batched!.delete(self!)
|
|
1374
|
+
// as the flush would: not if it was disposed while it waited
|
|
1375
|
+
if (owner.has(self!)) self!.run()
|
|
1376
|
+
}
|
|
1377
|
+
return (target as any)[key]
|
|
1378
|
+
}
|
|
1379
|
+
return new Proxy(box, { get: read, set: refuse, deleteProperty: refuse, defineProperty: refuse }) as Computed<T>
|
|
1285
1380
|
}
|
package/src/source.ts
CHANGED
|
@@ -374,7 +374,7 @@ export const declaredPropNames = (
|
|
|
374
374
|
// $emit, $updateModel and $slots, then $importAll. They are positional parameters, so the order is part of what
|
|
375
375
|
// precompile has to reproduce; change one side and precompiled scripts stop
|
|
376
376
|
// matching, which tests/precompile.test.ts and `npm run check:precompile` catch
|
|
377
|
-
export const SETUP_HELPER_NAMES = ["$", "$$", "$create", "$reactive", "$toRaw", "$batch", "Component79", "C79"]
|
|
377
|
+
export const SETUP_HELPER_NAMES = ["$", "$$", "$create", "$reactive", "$toRaw", "$raw", "$batch", "$tick", "Component79", "C79"]
|
|
378
378
|
export const INSTANCE_HELPER_NAMES = ["$mounted", "$destroyed", "$attached", "$detached", "$computed", "$self", "$$self", "$emit", "$updateModel", "$slots", "$importAll"]
|
|
379
379
|
|
|
380
380
|
// the global a precompiled script leaves its functions on, for the runtime to
|