jq79 0.7.13 → 0.7.14

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jq79",
3
- "version": "0.7.13",
3
+ "version": "0.7.14",
4
4
  "description": "Mini reactive component library: single-file .html components, Svelte-style setup scripts, fine-grained proxy reactivity. No compiler, no bundler, no dependencies — works from any static host.",
5
5
  "keywords": [
6
6
  "reactive",
package/src/jq79.ts CHANGED
@@ -2583,7 +2583,20 @@ const openWindow = (
2583
2583
  range: listLength => {
2584
2584
  length = listLength
2585
2585
  const height = Number(evalExpr(windowExpr, scope))
2586
+ const previous = rowHeight
2586
2587
  rowHeight = height > 0 ? height : 0
2588
+ // a new row height (or a window opening again after 0) moves every
2589
+ // offset: the window kept from before names rows for a geometry that is
2590
+ // gone - the old scroll position, or the old height's row count - and
2591
+ // would render them until the next scroll. Measured here, in this pass,
2592
+ // and written past the store: this effect is the one reading it, and a
2593
+ // tracked write would only wake it for a second pass over the same rows
2594
+ if (rowHeight && rowHeight !== previous && connected && top.isConnected) {
2595
+ const { listTop, scrollTop, height: viewport } = metrics()
2596
+ const raw = $toRaw(view)
2597
+ raw.count = rowsFor(viewport)
2598
+ raw.start = Math.max(0, Math.floor((scrollTop - listTop) / rowHeight) - OVERSCAN)
2599
+ }
2587
2600
  if (!rowHeight) {
2588
2601
  ;(top as HTMLElement).style.height = (bottom as HTMLElement).style.height = "0px"
2589
2602
  return [0, listLength]
package/src/reactive.ts CHANGED
@@ -257,18 +257,26 @@ let effectsCreated = 0
257
257
  // See RECORD/2026-11-12.batch.md
258
258
  let batchDepth = 0
259
259
  let batched: Map<Effect, Set<Effect>> | null = null
260
+ // the batch is dispatch's, not an explicit $batch (see openDispatch)
261
+ let implicitBatch = false
260
262
 
261
263
  const queueBatched = (matched: Iterable<Effect>, effects: Set<Effect>) => {
262
- for (const effect of matched) if (!batched!.has(effect)) batched!.set(effect, effects)
264
+ const queue = (batched ??= new Map())
265
+ for (const effect of matched) {
266
+ // an effect writing what it reads re-runs inside its own loop, as it does
267
+ // outside a batch, and is cut off there with its own message
268
+ if (implicitBatch && effect === activeEffect) effect.run()
269
+ else if (!queue.has(effect)) queue.set(effect, effects)
270
+ }
263
271
  }
264
272
 
265
273
  // oldest first, like runMatched. Runs with the batch still open, so what a
266
274
  // flushed effect writes queues into the next round of this same flush rather
267
275
  // than running mid-round - the batch settles before $batch returns
268
276
  const flushBatched = () => {
269
- for (let rounds = 0; batched!.size; rounds++) {
277
+ for (let rounds = 0; batched?.size; rounds++) {
270
278
  if (rounds === 100) {
271
- console.error("jq79: a $batch kept waking effects for 100 rounds (an effect writes what it reads); giving up on it settling")
279
+ 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
280
  batched!.clear()
273
281
  return
274
282
  }
@@ -284,7 +292,7 @@ const flushBatched = () => {
284
292
  // Nested batches flush at the outermost. A throw still flushes - the writes it
285
293
  // made did land - and is rethrown
286
294
  export const $batch = <T>(fn: () => T): T => {
287
- if (batchDepth++ === 0) batched = new Map()
295
+ if (batchDepth++ === 0) batched = null
288
296
  try {
289
297
  return fn()
290
298
  } finally {
@@ -299,6 +307,34 @@ export const $batch = <T>(fn: () => T): T => {
299
307
  }
300
308
  }
301
309
 
310
+ // A write outside any $batch runs the effects it woke right away, as it always
311
+ // has - but what *their* writes wake waits until all of them have run, and then
312
+ // runs once, as a $batch would. Without it a write that two $computed derive
313
+ // from is a glitch: the first one's new value wakes a binding that reads both,
314
+ // which runs against the second one's OLD value before that one recomputes. A
315
+ // windowed list whose row height came from one and its items from the other
316
+ // rendered every row of the old list, unwindowed, for one pass
317
+ // (RECORD/2026-11-15.glitch-free-propagation.md). Still synchronous: all of
318
+ // it has run before the write returns. `batched` is allocated only when
319
+ // something is actually queued, so a write that wakes plain bindings pays a
320
+ // counter and nothing else
321
+ //
322
+ // Open and close rather than a wrapper taking a callback: this is every write's
323
+ // path, and a closure per write was measurable on update10k_x16
324
+ const openDispatch = () => {
325
+ batchDepth = 1
326
+ implicitBatch = true
327
+ }
328
+ const closeDispatch = () => {
329
+ try {
330
+ if (batched) flushBatched()
331
+ } finally {
332
+ batchDepth = 0
333
+ batched = null
334
+ implicitBatch = false
335
+ }
336
+ }
337
+
302
338
  // the deps an effect has before its first run, and what indexEffect compares
303
339
  // its first run against. Never written to - every run installs a fresh set -
304
340
  // so one frozen instance stands in for the two empty sets each effect used to
@@ -648,7 +684,12 @@ export const $reactive = <T extends Record<string, any>>(data: T): ReactiveDeepD
648
684
  let sorted = true
649
685
  for (let i = 1; sorted && i < ordered.length; i++) sorted = ordered[i - 1].order < ordered[i].order
650
686
  if (!sorted) ordered.sort((a, b) => a.order - b.order)
651
- ordered.forEach(effect => { if (effects.has(effect)) effect.run() })
687
+ openDispatch()
688
+ try {
689
+ for (const effect of ordered) if (effects.has(effect)) effect.run()
690
+ } finally {
691
+ closeDispatch()
692
+ }
652
693
  }
653
694
 
654
695
  const notify = (dotKey: string, value: any, isNewKey = false) => {
@@ -659,7 +700,14 @@ export const $reactive = <T extends Record<string, any>>(data: T): ReactiveDeepD
659
700
  // store entirely), so dep matching would never wake it up
660
701
  if (isNewKey) {
661
702
  if (batchDepth) queueBatched(effects, effects)
662
- else effects.forEach(effect => effect.run())
703
+ else {
704
+ openDispatch()
705
+ try {
706
+ effects.forEach(effect => effect.run())
707
+ } finally {
708
+ closeDispatch()
709
+ }
710
+ }
663
711
  }
664
712
  // `effects.has` stands in for the membership check `effects.forEach` used
665
713
  // to give for free: an effect disposed by an earlier effect in this same