jq79 0.7.12 → 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/dist/source.d.ts CHANGED
@@ -57,7 +57,7 @@ export declare const setupBody: (code: string) => string;
57
57
  export declare const factoryParams: (helperNames: string[]) => string[];
58
58
  export declare const factoryBody: (code: string) => string;
59
59
  export declare const readSetupSignature: (script: TagBlock) => PropDecl[] | null;
60
- export declare const declaredPropNames: (scripts: TagBlock[], signature: (script: TagBlock) => PropDecl[] | null) => Set<string>;
60
+ export declare const declaredPropNames: (scripts: TagBlock[], signature: (script: TagBlock) => PropDecl[] | null, factoryProps?: (src: string) => PropDecl[] | null) => Set<string>;
61
61
  export declare const SETUP_HELPER_NAMES: string[];
62
62
  export declare const INSTANCE_HELPER_NAMES: string[];
63
63
  export declare const PRECOMPILED_QUEUE = "__jq79precompiled";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jq79",
3
- "version": "0.7.12",
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]
@@ -3887,8 +3900,40 @@ const declareProps = (store: Record<string, any>, props: PropDecl[] | null) => {
3887
3900
  // Only the empty *value* is closed. An absent attribute (a factory <script>
3888
3901
  // with no :setup at all) stays `null`, so its signature is still read from the
3889
3902
  // factory's first parameter
3903
+ // What a script's text compiles to, memoized by that text. Every instance of a
3904
+ // definition runs the same scripts, and these passes are pure functions of the
3905
+ // string - re-running them per instance was most of a list's render time. The
3906
+ // values are shared, so the arrays are frozen on the way in. A throw (the
3907
+ // pre-0.4 factory signature) is not cached: it rethrows on every render, as it
3908
+ // always did. Kept here, not in transform.ts, because the editor's language
3909
+ // server re-reads a script on every keystroke and would grow a map per edit.
3910
+ // See RECORD/2026-11-15.cache-script-transforms.md
3911
+ const memoByText = <T>(fn: (src: string) => T): ((src: string) => T) => {
3912
+ const cache = new Map<string, T>()
3913
+ return src => {
3914
+ if (cache.has(src)) return cache.get(src)!
3915
+ const value = fn(src)
3916
+ cache.set(src, value)
3917
+ return value
3918
+ }
3919
+ }
3920
+ const freezeProps = (props: PropDecl[] | null): PropDecl[] | null => {
3921
+ props?.forEach(prop => Object.freeze(prop))
3922
+ if (props) Object.freeze(props)
3923
+ return props
3924
+ }
3925
+ const factoryCodeOf = memoByText(transformFactoryScript)
3926
+ const factoryPropsOf = memoByText(src => freezeProps(parseFactoryProps(src)))
3927
+ const setupTransformOf = memoByText(src => {
3928
+ const transform = transformSetupScript(src)
3929
+ Object.freeze(transform.vars)
3930
+ return Object.freeze(transform)
3931
+ })
3932
+ const signatureOf = memoByText(src => freezeProps(readSetupSignature({ attrs: { ":setup": src }, content: "" })))
3933
+
3890
3934
  const setupSignature = (script: TagBlock): PropDecl[] | null => {
3891
- const props = readSetupSignature(script)
3935
+ const pattern = script.attrs[":setup"]
3936
+ const props = pattern === undefined ? null : signatureOf(pattern)
3892
3937
  if (!props && script.attrs[":setup"] !== undefined) warnUnreadableSignature(script, script.attrs[":setup"])
3893
3938
  return props
3894
3939
  }
@@ -3926,7 +3971,7 @@ const warnUnreadableSignature = (script: TagBlock, pattern: string) => {
3926
3971
  const declaredPropSet = (scripts: TagBlock[]): Set<string> | null => {
3927
3972
  let names: Set<string> | null = null
3928
3973
  scripts.forEach(script => {
3929
- const declarations = parseFactoryProps(script.content) ?? setupSignature(script)
3974
+ const declarations = factoryPropsOf(script.content) ?? setupSignature(script)
3930
3975
  if (!declarations) return
3931
3976
  const into = (names ??= new Set())
3932
3977
  declarations.forEach(({ name }) => into.add(name))
@@ -4781,7 +4826,7 @@ export class Component79 {
4781
4826
  // what this component can see of its file's other components, and which of
4782
4827
  // its declared props arrived empty - both decided by the signature, before
4783
4828
  // the store exists (see siblingsInScope / UNFILLED_PROPS)
4784
- const declared = declaredPropNames(this.scripts, setupSignature)
4829
+ const declared = declaredPropNames(this.scripts, setupSignature, factoryPropsOf)
4785
4830
  const siblingScope = siblingsInScope(this.siblings, declared)
4786
4831
  const raw: Record<string, any> = siblingScope
4787
4832
  ? Object.assign(Object.create(siblingScope), data)
@@ -5017,7 +5062,7 @@ export class Component79 {
5017
5062
  const instanceHelpers = { $mounted, $destroyed, $attached, $detached, $computed: $instanceComputed, $self, $$self, ...injected, $importAll, ...siblingScope }
5018
5063
  const at: ScriptLocation = { filename: this.filename, index }
5019
5064
  const deferred = ":mounted" in script.attrs
5020
- const factoryCode = transformFactoryScript(script.content)
5065
+ const factoryCode = factoryCodeOf(script.content)
5021
5066
  const run = ((): ScriptRun => {
5022
5067
  if (factoryCode !== null) {
5023
5068
  // a factory publishes its names by returning them, so one that yields
@@ -5032,11 +5077,11 @@ export class Component79 {
5032
5077
  "so none of its bindings exist yet - await $mounted() inside the factory instead."
5033
5078
  )
5034
5079
  }
5035
- declareProps(store, parseFactoryProps(script.content))
5080
+ declareProps(store, factoryPropsOf(script.content))
5036
5081
  const body = deferred ? defer(factoryCode) : factoryCode
5037
5082
  return runFactoryScript(body, store, run => fx.effect(run), instanceHelpers, $import, at)
5038
5083
  }
5039
- const { vars, code } = transformSetupScript(script.content)
5084
+ const { vars, code } = setupTransformOf(script.content)
5040
5085
  declareProps(store, setupSignature(script))
5041
5086
  // pre-declare script vars on the store so `with` resolves assignments
5042
5087
  // to them (and reads of them) through the reactive proxy
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
package/src/source.ts CHANGED
@@ -352,10 +352,15 @@ export const readSetupSignature = (script: TagBlock): PropDecl[] | null => {
352
352
  // says it comes from the parent, so the file's own definition of that name is
353
353
  // deliberately not in this component's scope. The runtime reads the setup
354
354
  // signatures with setupSignature, which warns; precompile, with readSetupSignature
355
- export const declaredPropNames = (scripts: TagBlock[], signature: (script: TagBlock) => PropDecl[] | null): Set<string> => {
355
+ // `factoryProps` lets the runtime pass its memoized reader (see factoryPropsOf)
356
+ export const declaredPropNames = (
357
+ scripts: TagBlock[],
358
+ signature: (script: TagBlock) => PropDecl[] | null,
359
+ factoryProps: (src: string) => PropDecl[] | null = parseFactoryProps,
360
+ ): Set<string> => {
356
361
  const names = new Set<string>()
357
362
  scripts.forEach(script => {
358
- const declarations = parseFactoryProps(script.content) ?? signature(script)
363
+ const declarations = factoryProps(script.content) ?? signature(script)
359
364
  declarations?.forEach(({ name }) => names.add(name))
360
365
  })
361
366
  return names