jq79 0.7.10 → 0.7.12

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/reactive.ts CHANGED
@@ -249,6 +249,56 @@ const releaseAll = (effect: Effect) => {
249
249
  // than left to whatever the index happens to yield
250
250
  let effectsCreated = 0
251
251
 
252
+ // $batch: while it is above 0, a woken effect is queued here instead of run -
253
+ // once, however many writes woke it - against the effect set of the store that
254
+ // woke it, which is how the flush still skips one disposed in the meantime.
255
+ // Module-level because one change often spans two stores (a store and the one
256
+ // it is bridged into), and only one queue can order them.
257
+ // See RECORD/2026-11-12.batch.md
258
+ let batchDepth = 0
259
+ let batched: Map<Effect, Set<Effect>> | null = null
260
+
261
+ const queueBatched = (matched: Iterable<Effect>, effects: Set<Effect>) => {
262
+ for (const effect of matched) if (!batched!.has(effect)) batched!.set(effect, effects)
263
+ }
264
+
265
+ // oldest first, like runMatched. Runs with the batch still open, so what a
266
+ // flushed effect writes queues into the next round of this same flush rather
267
+ // than running mid-round - the batch settles before $batch returns
268
+ const flushBatched = () => {
269
+ for (let rounds = 0; batched!.size; rounds++) {
270
+ 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")
272
+ batched!.clear()
273
+ return
274
+ }
275
+ const woken = batched!
276
+ batched = new Map()
277
+ const ordered = Array.from(woken.keys()).sort((a, b) => a.order - b.order)
278
+ ordered.forEach(effect => { if (woken.get(effect)!.has(effect)) effect.run() })
279
+ }
280
+ }
281
+
282
+ // runs `fn`, letting its writes land and notify listeners as they happen, and
283
+ // runs every effect they woke once, after it returns, against the final state.
284
+ // Nested batches flush at the outermost. A throw still flushes - the writes it
285
+ // made did land - and is rethrown
286
+ export const $batch = <T>(fn: () => T): T => {
287
+ if (batchDepth++ === 0) batched = new Map()
288
+ try {
289
+ return fn()
290
+ } finally {
291
+ if (batchDepth === 1) {
292
+ try {
293
+ flushBatched()
294
+ } finally {
295
+ batchDepth = 0
296
+ batched = null
297
+ }
298
+ } else batchDepth--
299
+ }
300
+ }
301
+
252
302
  // the deps an effect has before its first run, and what indexEffect compares
253
303
  // its first run against. Never written to - every run installs a fresh set -
254
304
  // so one frozen instance stands in for the two empty sets each effect used to
@@ -589,6 +639,7 @@ export const $reactive = <T extends Record<string, any>>(data: T): ReactiveDeepD
589
639
  // an earlier one in this same pass (a list diff tearing down the rows it just
590
640
  // woke) must not run
591
641
  const runMatched = (matched: Set<Effect>) => {
642
+ if (batchDepth) return queueBatched(matched, effects)
592
643
  const ordered = Array.from(matched)
593
644
  // effects are usually collected in creation order already - one node's set
594
645
  // is filled as its effects are made, and a subtree sweep of a freshly-built
@@ -606,7 +657,10 @@ export const $reactive = <T extends Record<string, any>>(data: T): ReactiveDeepD
606
657
  // a newly-created key re-runs every effect: an effect that read the
607
658
  // name while it didn't exist couldn't track it (`with` skipped the
608
659
  // store entirely), so dep matching would never wake it up
609
- if (isNewKey) effects.forEach(effect => effect.run())
660
+ if (isNewKey) {
661
+ if (batchDepth) queueBatched(effects, effects)
662
+ else effects.forEach(effect => effect.run())
663
+ }
610
664
  // `effects.has` stands in for the membership check `effects.forEach` used
611
665
  // to give for free: an effect disposed by an earlier effect in this same
612
666
  // notify (a list diff tearing down the rows it just woke) must not run
package/src/source.ts CHANGED
@@ -268,7 +268,7 @@ export const splitText = (template: string): TextPart[] => {
268
268
  return parts
269
269
  }
270
270
 
271
- export const CONTROL_ATTRS = new Set([":class", ":value", ":checked", ":selected", ":if", ":elseif", ":else", ":each", ":key", ":with", ":text", ":html", ":html.allowed", ":props"])
271
+ export const CONTROL_ATTRS = new Set([":class", ":value", ":checked", ":selected", ":if", ":elseif", ":else", ":each", ":key", ":window", ":with", ":text", ":html", ":html.allowed", ":props"])
272
272
 
273
273
  // a control attribute is one the static-attr loop and nested-component prop
274
274
  // collection must skip. The set holds the fixed names; `:class.<name>` (the
@@ -369,7 +369,7 @@ export const declaredPropNames = (scripts: TagBlock[], signature: (script: TagBl
369
369
  // $emit, $updateModel and $slots, then $importAll. They are positional parameters, so the order is part of what
370
370
  // precompile has to reproduce; change one side and precompiled scripts stop
371
371
  // matching, which tests/precompile.test.ts and `npm run check:precompile` catch
372
- export const SETUP_HELPER_NAMES = ["$", "$$", "$create", "$reactive", "$toRaw", "Component79", "C79"]
372
+ export const SETUP_HELPER_NAMES = ["$", "$$", "$create", "$reactive", "$toRaw", "$batch", "Component79", "C79"]
373
373
  export const INSTANCE_HELPER_NAMES = ["$mounted", "$destroyed", "$attached", "$detached", "$computed", "$self", "$$self", "$emit", "$updateModel", "$slots", "$importAll"]
374
374
 
375
375
  // the global a precompiled script leaves its functions on, for the runtime to
package/src/transform.ts CHANGED
@@ -899,3 +899,103 @@ export const freeIdentifiers = (expr: string): string[] | null => {
899
899
  if (shortCircuits && names.size > 1) return null
900
900
  return [...names]
901
901
  }
902
+
903
+ // ---------------------------------------------------------------------------
904
+ // the preload scanner (RECORD/2026-11-05.a-preload-scanner.md): the literal
905
+ // specifiers a script asks for in top-level statements that run whatever
906
+ // happens, so the runtime can start every download before the script's first
907
+ // line and let the script take them in its own order. What might not run is
908
+ // left alone - a function's body, a block, a branch, a loop, a handler - and
909
+ // so is anything the scanner can't read whole: a missed preload costs what it
910
+ // costs today, a wrong one an extra download of a file
911
+ // ---------------------------------------------------------------------------
912
+
913
+ // how a specifier resolves - the way the call that names it does:
914
+ // - "component": import(), $importAll and a static import, against the
915
+ // component's file and through its modules map; .html is a component,
916
+ // anything else a module
917
+ // - "page": C79.importAll, the same against the page
918
+ // - "fetch": C79.fetch / fetchAll, always a component, against the page
919
+ // (no kind is spelled "import": dist/jq79.js is held to having no import
920
+ // statement in it, and the test can't tell one from a string that ends so)
921
+ export type Preload = { spec: string; kind: "component" | "page" | "fetch" }
922
+
923
+ // where a statement or an initializer starts with a call that downloads:
924
+ // an optional await, then the call up to its opening bracket(s)
925
+ const PRELOAD_CALL_RE = /(?:await\s+)?(?:import\s*\(|(\$importAll)\s*\(\s*\[|(C79|Component79)\s*\.\s*(fetch|fetchAll|importAll)\s*\(\s*(\[)?)/y
926
+ const PRELOAD_LITERAL_RE = /(["'])([^"'\n\\]+)\1/y
927
+ // a statement that guards the next one without braces - `if (x)`, a loop's
928
+ // header, `else`, `do` - so that one may not run
929
+ const GUARD_RE = /^(?:(?:if|for|while|with)\b[\s\S]*\)|[\s\S]*\b(?:else|do))$/
930
+
931
+ // the literal specifiers of the call `text` starts with: an array's entries
932
+ // up to the first that isn't a literal on its own (`"./a" + x` is not one)
933
+ const preloadCall = (text: string, own: Set<string>): Preload[] => {
934
+ PRELOAD_CALL_RE.lastIndex = 0
935
+ const call = PRELOAD_CALL_RE.exec(text)
936
+ if (!call) return []
937
+ const [, importAll, library, method, bracket] = call
938
+ if (own.has(importAll ?? library ?? "")) return [] // the script's own, not jq79's
939
+ const list = importAll !== undefined || bracket !== undefined
940
+ // fetch takes one URL, its plurals an array: anything else throws
941
+ if (method !== undefined && (method === "fetch") === list) return []
942
+ const kind: Preload["kind"] = library === undefined ? "component" : method === "importAll" ? "page" : "fetch"
943
+
944
+ const found: Preload[] = []
945
+ let i = skipToToken(text, PRELOAD_CALL_RE.lastIndex)
946
+ for (;;) {
947
+ PRELOAD_LITERAL_RE.lastIndex = i
948
+ const literal = PRELOAD_LITERAL_RE.exec(text)
949
+ if (!literal) return found
950
+ const end = skipToToken(text, PRELOAD_LITERAL_RE.lastIndex)
951
+ const next = text[end]
952
+ if (!list) return next === ")" || next === "," ? [{ spec: literal[2], kind }] : []
953
+ if (next !== "," && next !== "]") return found
954
+ found.push({ spec: literal[2], kind })
955
+ if (next === "]") return found
956
+ i = skipToToken(text, end + 1)
957
+ }
958
+ }
959
+
960
+ export const preloadSpecifiers = (src: string): Preload[] => {
961
+ const statements: string[] = []
962
+ for (let i = skipToToken(src, 0); i < src.length; i = skipToToken(src, i + 1)) {
963
+ const end = findStatementEnd(src, i)
964
+ statements.push(src.slice(i, end).trim().replace(/^export\s+(?!default\b)/, ""))
965
+ i = end
966
+ }
967
+
968
+ // a declaration's initializers, or null for a statement that isn't one
969
+ const initializers = (statement: string): { target: string; init: string }[] | null => {
970
+ DECLARATION_START_RE.lastIndex = 0
971
+ const declaration = DECLARATION_START_RE.exec(statement)
972
+ if (!declaration) return null
973
+ return splitDeclarators(statement.slice(declaration[0].length)).map(({ raw }) => {
974
+ const body = raw.trim()
975
+ const assign = defaultAssignIndex(body)
976
+ return assign === -1 ? { target: body, init: "" } : { target: body.slice(0, assign).trim(), init: body.slice(assign + 1).trim() }
977
+ })
978
+ }
979
+
980
+ // a script that declares its own C79, Component79 or $importAll calls
981
+ // that, not the library's
982
+ const own = new Set<string>()
983
+ statements.forEach(statement => initializers(statement)?.forEach(({ target }) => patternBindings(target).forEach(name => own.add(name))))
984
+
985
+ const found: Preload[] = []
986
+ let guarded = false
987
+ for (const statement of statements) {
988
+ if (!guarded) {
989
+ STATIC_IMPORT_RE.lastIndex = 0
990
+ const staticImport = STATIC_IMPORT_RE.exec(statement)
991
+ if (staticImport) {
992
+ if (!/^type\s/.test(staticImport[1] ?? "")) found.push({ spec: staticImport[3], kind: "component" })
993
+ } else {
994
+ const declared = initializers(statement)
995
+ for (const text of declared ? declared.map(({ init }) => init) : [statement]) found.push(...preloadCall(text, own))
996
+ }
997
+ }
998
+ guarded = GUARD_RE.test(statement)
999
+ }
1000
+ return found
1001
+ }