@abide/abide 0.110.4 → 0.112.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/docs/REGISTRY.md +30 -29
  3. package/docs/RULEBOOK.md +98 -22
  4. package/docs/abide.md +46 -32
  5. package/package.json +1 -1
  6. package/src/compiler/Lowering.ts +5 -0
  7. package/src/compiler/classifyTemplate.ts +129 -34
  8. package/src/compiler/compileApp.ts +2 -1
  9. package/src/compiler/emitClient.ts +128 -14
  10. package/src/compiler/renderMarkup.ts +14 -8
  11. package/src/server/BRIDGE_VERSION.ts +1 -1
  12. package/src/server/HEARTBEAT_MILLISECONDS.ts +2 -0
  13. package/src/server/IDLE_TIMEOUT_SECONDS.ts +6 -0
  14. package/src/server/answerRequest.ts +44 -3
  15. package/src/server/answerRpc.ts +14 -41
  16. package/src/server/answerSocket.ts +2 -2
  17. package/src/server/answerValue.ts +15 -3
  18. package/src/server/answerWorker.ts +195 -0
  19. package/src/server/appManifest.ts +48 -0
  20. package/src/server/assistant.ts +1 -1
  21. package/src/server/bridgeRelay.ts +4 -50
  22. package/src/server/claudeApi.ts +1 -1
  23. package/src/server/claudeCode.ts +3 -3
  24. package/src/server/clientBundle.ts +7 -0
  25. package/src/server/config.ts +14 -0
  26. package/src/server/createApp.ts +28 -4
  27. package/src/server/devWorker.ts +4 -1
  28. package/src/server/render.ts +2 -2
  29. package/src/server/requestScope.ts +31 -11
  30. package/src/server/responseRelay.ts +7 -7
  31. package/src/server/serveBuild.ts +2 -0
  32. package/src/server/serveDev.ts +4 -1
  33. package/src/server/streamEvent.ts +60 -1
  34. package/src/shared/ADDRESSES.ts +11 -0
  35. package/src/shared/ASCII_WHITESPACE.ts +6 -0
  36. package/src/shared/LONGEST_DELAY.ts +2 -0
  37. package/src/shared/OFFLINE_CACHES.ts +12 -0
  38. package/src/shared/OFFLINE_META.ts +7 -0
  39. package/src/shared/answerOf.ts +29 -4
  40. package/src/shared/attributeText.ts +1 -1
  41. package/src/shared/reactive/Expiry.ts +13 -1
  42. package/src/shared/reactive/Reactive.ts +13 -3
  43. package/src/shared/safeUrl.ts +8 -1
  44. package/src/ui/Level.ts +43 -95
  45. package/src/ui/Router.ts +37 -13
  46. package/src/ui/bindGroup.ts +41 -0
  47. package/src/ui/bindProperty.ts +1 -21
  48. package/src/ui/index.ts +5 -3
  49. package/src/ui/reconcileHead.ts +215 -0
  50. package/src/ui/registerWorker.ts +34 -0
  51. package/src/ui/routing.ts +1 -0
  52. package/src/ui/setClassGroup.ts +79 -0
  53. package/src/ui/setStyleGroup.ts +130 -0
  54. package/src/ui/socketWire.ts +33 -2
  55. package/src/ui/applyHead.ts +0 -214
package/src/ui/Router.ts CHANGED
@@ -4,12 +4,12 @@ import { ERROR_META } from '#shared/ERROR_META.ts'
4
4
  import { firstMatch, type Params, partsOf } from '#shared/matchRoute.ts'
5
5
  import { unmounted } from '#shared/mount.ts'
6
6
  import { NAVIGATION_HEADER } from '#shared/NAVIGATION_HEADER.ts'
7
+ import { OFFLINE_SAVED } from '#shared/OFFLINE_CACHES.ts'
7
8
  import type { ShareScope } from '#shared/reactive/share.ts'
8
9
  import { arrive, route } from '#shared/route.ts'
9
10
  import { rankRoutes, type Segment } from '#shared/routePattern.ts'
10
11
  import { setUpLevels } from '#shared/setUpLevels.ts'
11
12
  import { paintedCallsLanded } from '#shared/takeEarlyCall.ts'
12
- import { withdrawHead } from './applyHead.ts'
13
13
  import { LAYER_MOUNT, LAYER_NAVIGATE } from './LAYER_NAMES.ts'
14
14
  import { Level } from './Level.ts'
15
15
  import { beginLayer, endLayer } from './measureLayer.ts'
@@ -255,6 +255,20 @@ export class Router {
255
255
  // whole reason the ticket is checked here rather than after the mount — and
256
256
  // `navigating` stays the newer navigation's to clear.
257
257
  if (ticket.signal.aborted) return
258
+ // 30.32 — OFFLINE, THE DOCUMENT LOAD BELOW CAN ONLY PAINT THE BROWSER'S ERROR PAGE over a
259
+ // page that works. A history step has already moved the bar, so it is left to the
260
+ // document.
261
+ if (
262
+ answer === undefined &&
263
+ !navigator.onLine &&
264
+ step.direction === 'push'
265
+ ) {
266
+ // 45.12 — UNLESS THE SERVICE WORKER SAVED THE PAGE, which the document load is
267
+ // then answered with.
268
+ if ((await savedOffline(target)) && !ticket.signal.aborted)
269
+ location.href = target.href
270
+ return
271
+ }
258
272
  // 30.18 — A REFUSAL, A RESPONSE MIDDLEWARE ANSWERED ITSELF, or no answer at all: the
259
273
  // document asks again and paints whatever the server says. A fetch that failed —
260
274
  // offline, or a redirect to another origin that CORS will not let it follow — goes to
@@ -454,12 +468,9 @@ export class Router {
454
468
  // table written by hand may answer with a fresh record each call. The function a module
455
469
  // exports is the same function either way.
456
470
  const shared = this.sharedLevels(arriving)
457
- // READ BEFORE THE STOP, while the leaving levels' heads read what they read on screen.
458
- const leaving = Level.headsOf(
459
- this.levels,
460
- shared,
461
- this.levels.length,
462
- )
471
+ // RECONCILED BEFORE THE STOP, while the leaving levels' heads read what they read on
472
+ // screen: what the server painted for them is adopted as theirs, so it leaves with them.
473
+ Level.reconcile(this.levels)
463
474
  const left: Scroll = [scrollX, scrollY]
464
475
  this.stopFrom(shared)
465
476
  // The region the arriving part goes into: the page region where nothing is kept, and
@@ -480,11 +491,8 @@ export class Router {
480
491
  // outermost first; a kept layout is not mounted again, so neither is its head.
481
492
  this.mountChain(target, found, arriving, shared, into)
482
493
  // 39.14 — what the leaving levels keyed and the arriving ones did not goes back to the
483
- // nearest kept layout's, else leaves.
484
- withdrawHead(leaving, () => [
485
- Level.headsOf(this.levels, 0, shared),
486
- Level.headsOf(this.levels, shared, this.levels.length),
487
- ])
494
+ // nearest kept layout's, else leaves; their keyless nodes leave with them (39.16).
495
+ Level.reconcile(this.levels)
488
496
  // 30.29 — where the entry was left, on a history step; else the top of the page, or —
489
497
  // as the browser does on a load — the fragment's element.
490
498
  if (step.scroll !== undefined)
@@ -527,11 +535,13 @@ export class Router {
527
535
  private stopFrom(at: number): void {
528
536
  for (let index = this.levels.length - 1; index >= at; index -= 1) {
529
537
  const level = this.levels[index] as Level
538
+ // OUT OF THE CHAIN BEFORE IT STOPS, so a slot stop it fires on the way finds it gone
539
+ // (`release`) and leaves the head to whoever stopped it.
540
+ this.levels.length = index
530
541
  // The level's head leaves whole, so none of its components withdraws its own.
531
542
  level.contributed.clear()
532
543
  level.scope?.stop()
533
544
  }
534
- this.levels.length = at
535
545
  }
536
546
 
537
547
  /**
@@ -614,7 +624,11 @@ export class Router {
614
624
  private release(level: Level, at: number): void {
615
625
  if (this.levels[at] !== level) return
616
626
  level.slot = undefined
627
+ // 39.14, 39.16 — THE LEVELS BELOW LEAVE WITH THE SLOT'S REGION, and their head with them:
628
+ // reconciled first, so what the server painted for them is adopted as theirs.
629
+ Level.reconcile(this.levels)
617
630
  this.stopFrom(at + 1)
631
+ Level.reconcile(this.levels)
618
632
  }
619
633
 
620
634
  /**
@@ -696,6 +710,16 @@ function loadOrMiss(route: Compiled): Promise<PageModule[] | undefined> {
696
710
  return load(route).catch(() => undefined)
697
711
  }
698
712
 
713
+ /** Whether a service worker controls the page and saved `target`'s page (45.12). */
714
+ async function savedOffline(target: URL): Promise<boolean> {
715
+ if (navigator.serviceWorker?.controller == null) return false
716
+ const saved = await caches.match(target.href, {
717
+ cacheName: OFFLINE_SAVED,
718
+ ignoreVary: true,
719
+ })
720
+ return saved !== undefined
721
+ }
722
+
699
723
  /**
700
724
  * Asks the server's `middleware` about an address; `undefined` where the fetch failed.
701
725
  *
@@ -0,0 +1,41 @@
1
+ import {
2
+ boundValue,
3
+ groupHolds,
4
+ heldValue,
5
+ writeBound,
6
+ } from '#shared/boundValue.ts'
7
+ import type { Scope } from './Scope.ts'
8
+
9
+ /**
10
+ * Keeps a radio or checkbox's `checked` in step with a `bind:group` value, both ways.
11
+ *
12
+ * 32.8 — COMPARED AGAINST THE INPUT'S OWN VALUE, read through `own` — the `value` attribute's
13
+ * expression, or its static text, or `on` where it has none — rather than off the element: a
14
+ * `value={option.id}` that moves reruns the compare, and a number stays a number in the group
15
+ * where the element's `value` property would have handed back its text.
16
+ */
17
+ export function bindGroup(
18
+ element: Element,
19
+ operand: unknown,
20
+ own: () => unknown,
21
+ scope: Scope,
22
+ ): void {
23
+ const bound = element as HTMLInputElement
24
+ scope.watch(() => {
25
+ const checked = groupHolds(boundValue(operand), own())
26
+ bound.checked = checked
27
+ bound.toggleAttribute('checked', checked)
28
+ })
29
+ bound.addEventListener('change', () => {
30
+ const value = own()
31
+ const standing = heldValue(operand)
32
+ // A checkbox group holds a list, and keeps its order; a radio group holds the one value.
33
+ if (Array.isArray(standing)) {
34
+ const next: unknown[] = []
35
+ for (const member of standing)
36
+ if (member !== value) next.push(member)
37
+ if (bound.checked) next.push(value)
38
+ writeBound(operand, next)
39
+ } else if (bound.checked) writeBound(operand, value)
40
+ })
41
+ }
@@ -8,7 +8,6 @@
8
8
 
9
9
  import {
10
10
  boundValue,
11
- groupHolds,
12
11
  heldValue,
13
12
  selectHolds,
14
13
  writeBound,
@@ -20,7 +19,7 @@ type Bound = HTMLInputElement & HTMLDetailsElement
20
19
 
21
20
  export function bindProperty(
22
21
  element: Element,
23
- kind: 'value' | 'checked' | 'open' | 'group',
22
+ kind: 'value' | 'checked' | 'open',
24
23
  operand: unknown,
25
24
  scope: Scope,
26
25
  ): void {
@@ -29,25 +28,6 @@ export function bindProperty(
29
28
  bindValue(bound, operand, scope)
30
29
  return
31
30
  }
32
- if (kind === 'group') {
33
- scope.watch(() => {
34
- const checked = groupHolds(boundValue(operand), bound.value)
35
- bound.checked = checked
36
- bound.toggleAttribute('checked', checked)
37
- })
38
- bound.addEventListener('change', () => {
39
- const standing = heldValue(operand)
40
- // A checkbox group holds a list, and keeps its order; a radio group holds the one value.
41
- if (Array.isArray(standing)) {
42
- const next: unknown[] = []
43
- for (const member of standing)
44
- if (member !== bound.value) next.push(member)
45
- if (bound.checked) next.push(bound.value)
46
- writeBound(operand, next)
47
- } else if (bound.checked) writeBound(operand, bound.value)
48
- })
49
- return
50
- }
51
31
  // `checked` on an input, `open` on a `<details>`.
52
32
  const property = kind
53
33
  scope.watch(() => {
package/src/ui/index.ts CHANGED
@@ -15,6 +15,8 @@
15
15
  // and every `class:name` on an element is merged by the server into the single `style` or `class`
16
16
  // attribute beside the author's own, so neither of those two attributes can be written back whole
17
17
  // — the CSSOM and `classList` are what touch the one declaration or the one token a binding owns.
18
+ // A `class={expr}` or `style={expr}` beside its directives is the same answer once more
19
+ // (`setClassGroup`, `setStyleGroup`, 32.71): the group writes what it owns, and nothing else.
18
20
  // A list is reached through `listAt` and `updateList` alone, so its rows' shape is no caller's.
19
21
  // Nothing that finds a node by something added to the document, because a compiler may not invent
20
22
  // structure an author's CSS or queries can see.
@@ -37,10 +39,8 @@ export {
37
39
  runHere,
38
40
  textHere,
39
41
  } from './adoptWalk.ts'
40
- // 39.6 — a module mounted into an empty region applies its own head, from inside its instance
41
- // (`emitClient`); only a module that carries its own markup and a `<head>` imports it.
42
- export { applyHead } from './applyHead.ts'
43
42
  export { bindElement } from './bindElement.ts'
43
+ export { bindGroup } from './bindGroup.ts'
44
44
  export { bindProperty } from './bindProperty.ts'
45
45
  export { bindSpread } from './bindSpread.ts'
46
46
  export { escapeless } from './escapeless.ts'
@@ -60,7 +60,9 @@ export { itemOf, noUpdate, replaceRow, rowCell } from './rows.ts'
60
60
  export { EMPTY_SCOPE, Scope } from './Scope.ts'
61
61
  export { setAttribute } from './setAttribute.ts'
62
62
  export { setClass } from './setClass.ts'
63
+ export { setClassGroup } from './setClassGroup.ts'
63
64
  export { setMarkup } from './setMarkup.ts'
65
+ export { setStyleGroup } from './setStyleGroup.ts'
64
66
  export { setStyleProperty } from './setStyleProperty.ts'
65
67
  export { setText } from './setText.ts'
66
68
  export { TryRegion } from './TryRegion.ts'
@@ -0,0 +1,215 @@
1
+ // 39.4–39.6, 39.13–39.17 — THE DOCUMENT HEAD HAS ONE WRITER. Every routed level and every component
2
+ // with a `<head>` is a contributor, and the head is reconciled from all of them at once, in the
3
+ // chain's order — each level's own head, then its components' in the order they mounted — rather
4
+ // than patched by each with what it can learn of the others.
5
+ //
6
+ // A KEY IS WON BY THE LAST CONTRIBUTOR HOLDING IT (39.4), and one nobody holds any more leaves or
7
+ // goes back to whoever still does (39.14) because that is what the chain now says. A KEYLESS NODE
8
+ // IS ITS CONTRIBUTOR'S, held by reference (39.5, 39.15, 39.16): two contributors writing the same
9
+ // `<style>` each own their own, and one leaving takes its own with it — told apart by where they
10
+ // stand, never by being equal.
11
+ //
12
+ // NOTHING IS ADDED TO MARK OWNERSHIP, and nothing a contributor did not write is touched: a node the
13
+ // server painted is ADOPTED the first time its contributor is reconciled, matched in chain order.
14
+
15
+ import { headKeyOf } from '#shared/headKeyOf.ts'
16
+ import { markupNodes } from './parseMarkup.ts'
17
+
18
+ /** One contributor's head, parsed once per text it reads. */
19
+ type Parsed = { text: string; keyed: Map<string, Element>; keyless: Node[] }
20
+
21
+ const parsedBy = new WeakMap<object, Parsed>()
22
+ /** The keyless nodes standing in the head for each contributor, in its order. */
23
+ const ownedBy = new Map<object, Node[]>()
24
+ /**
25
+ * The element standing for each key the chain held at the last reconcile, so one it drops is
26
+ * withdrawn — that element, and never another holding the same key that nothing here wrote.
27
+ */
28
+ const keyedNodes = new Map<string, Element>()
29
+
30
+ /** Whether `who` has been reconciled — adopted or written — since it joined. */
31
+ export function reconciled(who: object): boolean {
32
+ return ownedBy.has(who)
33
+ }
34
+
35
+ /**
36
+ * Writes the head the chain contributes: `chain` is every contributor and the head it reads now,
37
+ * outermost first.
38
+ *
39
+ * Only what moved is written. A key standing as it is wanted, and a keyless node equal to the one
40
+ * its contributor wants there, are left standing — a `<link rel=stylesheet>` put back as itself is
41
+ * refetched and flashes the page unstyled, and a `<script>` would run again.
42
+ */
43
+ export function reconcileHead(
44
+ chain: readonly (readonly [who: object, text: string])[],
45
+ ): void {
46
+ const head = document.head
47
+ const present = new Set<object>()
48
+ const wanted = new Map<string, Element>()
49
+ for (const [who, text] of chain) {
50
+ present.add(who)
51
+ for (const [key, element] of parsedOf(head, who, text).keyed)
52
+ wanted.set(key, element)
53
+ }
54
+ const standing = standingKeys(head)
55
+ for (const [key, node] of keyedNodes)
56
+ if (!wanted.has(key)) {
57
+ node.remove()
58
+ keyedNodes.delete(key)
59
+ }
60
+ for (const [key, element] of wanted) {
61
+ let at = standing.get(key)
62
+ if (at === undefined) {
63
+ at = element.cloneNode(true) as Element
64
+ head.appendChild(at)
65
+ } else if (at.outerHTML !== element.outerHTML) {
66
+ const next = element.cloneNode(true) as Element
67
+ head.replaceChild(next, at)
68
+ at = next
69
+ }
70
+ keyedNodes.set(key, at)
71
+ }
72
+
73
+ for (const [who, nodes] of ownedBy)
74
+ if (!present.has(who)) {
75
+ for (const node of nodes) node.parentNode?.removeChild(node)
76
+ ownedBy.delete(who)
77
+ }
78
+ const claimed = new Set<Node>()
79
+ for (const nodes of ownedBy.values())
80
+ for (const node of nodes) claimed.add(node)
81
+ let previous: Node | null = null
82
+ for (let at = 0; at < chain.length; at += 1) {
83
+ const who = (chain[at] as readonly [object, string])[0]
84
+ const wants = (parsedBy.get(who) as Parsed).keyless
85
+ const owned = ownedBy.get(who) ?? adopt(head, wants, claimed)
86
+ const next: Node[] = []
87
+ for (let index = 0; index < wants.length; index += 1) {
88
+ const want = wants[index] as Node
89
+ const have = owned[index]
90
+ let node: Node
91
+ if (have?.isEqualNode(want) === true) node = have as Node
92
+ else {
93
+ node = runnable(want.cloneNode(true))
94
+ if (have !== undefined) head.replaceChild(node, have)
95
+ else
96
+ head.insertBefore(
97
+ node,
98
+ previous === null
99
+ ? firstOwnedAfter(chain, at)
100
+ : previous.nextSibling,
101
+ )
102
+ }
103
+ next.push(node)
104
+ previous = node
105
+ }
106
+ for (let index = wants.length; index < owned.length; index += 1)
107
+ (owned[index] as Node).parentNode?.removeChild(owned[index] as Node)
108
+ ownedBy.set(who, next)
109
+ }
110
+ }
111
+
112
+ /** `who`'s head parsed, from the cache while it reads the same text. */
113
+ function parsedOf(head: HTMLHeadElement, who: object, text: string): Parsed {
114
+ const cached = parsedBy.get(who)
115
+ if (cached !== undefined && cached.text === text) return cached
116
+ const parsed: Parsed = { text, keyed: new Map(), keyless: [] }
117
+ for (const node of markupNodes(head, text)) {
118
+ const key = node.nodeType === 1 ? headKey(node as Element) : undefined
119
+ if (key === undefined) parsed.keyless.push(node)
120
+ else parsed.keyed.set(key, node as Element)
121
+ }
122
+ parsedBy.set(who, parsed)
123
+ return parsed
124
+ }
125
+
126
+ /**
127
+ * The nodes the server painted for a contributor reconciled for the first time: each one it wants,
128
+ * the first equal node nobody has claimed, in order. Contributors are adopted in the chain's
129
+ * order, which is the order the server painted them in, so an earlier one claims the earlier of
130
+ * two equal nodes.
131
+ */
132
+ function adopt(
133
+ head: HTMLHeadElement,
134
+ wants: Node[],
135
+ claimed: Set<Node>,
136
+ ): Node[] {
137
+ const adopted: Node[] = []
138
+ let from = head.firstChild
139
+ for (const want of wants) {
140
+ let found: Node | null = null
141
+ for (let at = from; at !== null; at = at.nextSibling)
142
+ if (!claimed.has(at) && at.isEqualNode(want)) {
143
+ found = at
144
+ break
145
+ }
146
+ if (found === null) break
147
+ claimed.add(found)
148
+ adopted.push(found)
149
+ from = found.nextSibling
150
+ }
151
+ return adopted
152
+ }
153
+
154
+ /** The first keyless node a contributor after `at` owns, which a new one goes before. */
155
+ function firstOwnedAfter(
156
+ chain: readonly (readonly [object, string])[],
157
+ at: number,
158
+ ): Node | null {
159
+ for (let later = at + 1; later < chain.length; later += 1) {
160
+ const owned = ownedBy.get(
161
+ (chain[later] as readonly [object, string])[0],
162
+ )
163
+ if (owned !== undefined && owned.length > 0) return owned[0] as Node
164
+ }
165
+ return null
166
+ }
167
+
168
+ /**
169
+ * `node`, or — for a `<script>` — the same script made by the document rather than the parser.
170
+ *
171
+ * 39.17 — A PARSED SCRIPT NEVER RUNS. The fragment parser marks every `<script>` it makes "already
172
+ * started", and a clone keeps the mark, so a script it made and then moved into the head is inert
173
+ * where the server's, made by the document's own parser, ran. One made by `createElement` starts
174
+ * clear and runs on insertion. Keyless always (39.11 keys no `<script>`).
175
+ */
176
+ function runnable(node: Node): Node {
177
+ if (node.nodeName !== 'SCRIPT') return node
178
+ const parsed = node as HTMLScriptElement
179
+ const script = document.createElement('script')
180
+ const attributes = parsed.attributes
181
+ for (let index = 0; index < attributes.length; index += 1) {
182
+ const attribute = attributes[index] as Attr
183
+ script.setAttribute(attribute.name, attribute.value)
184
+ }
185
+ script.text = parsed.text
186
+ return script
187
+ }
188
+
189
+ /**
190
+ * The head's keyed elements by key, the first holding each — 39.12 within the standing head: the
191
+ * FIRST element holding a key is the one a contribution replaces.
192
+ */
193
+ function standingKeys(head: HTMLHeadElement): Map<string, Element> {
194
+ const standingByKey = new Map<string, Element>()
195
+ for (
196
+ let node = head.firstElementChild;
197
+ node !== null;
198
+ node = node.nextElementSibling
199
+ ) {
200
+ const key = headKey(node)
201
+ if (key !== undefined && !standingByKey.has(key))
202
+ standingByKey.set(key, node)
203
+ }
204
+ return standingByKey
205
+ }
206
+
207
+ /**
208
+ * The key a head element replaces by, or `undefined` for one that appends — 39.11 through the one
209
+ * grammar (`headKeyOf`), asked with a resolved attribute.
210
+ */
211
+ function headKey(element: Element): string | undefined {
212
+ return headKeyOf(element.tagName.toLowerCase(), (name) =>
213
+ element.getAttribute(name),
214
+ )
215
+ }
@@ -0,0 +1,34 @@
1
+ // 45.1 — THE SERVICE WORKER, REGISTERED where the document carries the tag `ABIDE_OFFLINE` has the
2
+ // server write. The route table calls this in every app, so what the server says decides.
3
+
4
+ import { WORKER_ADDRESS } from '#shared/ADDRESSES.ts'
5
+ import { MOUNT, mounted } from '#shared/mount.ts'
6
+ import { OFFLINE_META } from '#shared/OFFLINE_META.ts'
7
+ import { onPrincipalRotated } from '#shared/principalRotated.ts'
8
+ import { HYDRATE_LOG } from './Mismatch.ts'
9
+
10
+ /** Registers the app's service worker once the page has loaded, over the mount's whole scope. */
11
+ export function registerWorker(): void {
12
+ if (document.querySelector(`meta[name="${OFFLINE_META}"]`) === null) return
13
+ if (!('serviceWorker' in navigator)) return
14
+ const workers = navigator.serviceWorker
15
+ const register = (): void => {
16
+ workers
17
+ .register(mounted(WORKER_ADDRESS), {
18
+ scope: `${MOUNT.current()}/`,
19
+ })
20
+ // 45.10
21
+ .catch((error: unknown) =>
22
+ HYDRATE_LOG.warning(
23
+ 'the service worker did not register',
24
+ error,
25
+ ),
26
+ )
27
+ }
28
+ // AFTER THE LOAD, so installing it — every file of the bundle fetched — does not compete with
29
+ // the page it is installing for.
30
+ if (document.readyState === 'complete') register()
31
+ else addEventListener('load', register, { once: true })
32
+ // 45.7 — a sign-in or sign-out in any tab: the pages it saved were another caller's.
33
+ onPrincipalRotated(() => workers.controller?.postMessage('rotated'), true)
34
+ }
package/src/ui/routing.ts CHANGED
@@ -15,4 +15,5 @@
15
15
  // `ROUTING_SPECIFIER`. An app with no page never resolves this entry.
16
16
 
17
17
  export { listenForRoutes } from './listenForRoutes.ts'
18
+ export { registerWorker } from './registerWorker.ts'
18
19
  export { type PageMount, type Route, Router, startRouter } from './Router.ts'
@@ -0,0 +1,79 @@
1
+ import { ASCII_WHITESPACE } from '#shared/ASCII_WHITESPACE.ts'
2
+ import { textOf } from '#shared/Raw.ts'
3
+ import { WORK } from '#shared/WORK.ts'
4
+
5
+ /**
6
+ * 32.71 — what a `class={expr}` and the `class:name` directives beside it last wrote, as one
7
+ * group: the attribute's text and tokens, and the directives' conditions in the order `names`
8
+ * gives them.
9
+ *
10
+ * KEYED BY ELEMENT rather than declared by the mount, because a row's `update` writes the same
11
+ * element from a function of its own and both have to read one record.
12
+ */
13
+ const owned = new WeakMap<
14
+ Element,
15
+ {
16
+ text: string | undefined
17
+ tokens: ReadonlySet<string>
18
+ conditions: readonly unknown[] | undefined
19
+ names: string[]
20
+ }
21
+ >()
22
+
23
+ // Every record's tokens until its `class` is first written, which replaces the set.
24
+ const NO_TOKENS: ReadonlySet<string> = new Set()
25
+
26
+ /**
27
+ * Writes a `class={expr}` and the `class:name` directives beside it as one group, through
28
+ * `classList`: `plain` is the attribute's value, and `conditions` each directive's, in the order
29
+ * of `names`, its classes space-separated.
30
+ *
31
+ * 32.71 — ONLY ITS OWN CLASSES, and only what moved: the attribute's tokens are re-split only when
32
+ * its text changed, a token it dropped comes off unless a directive holds it, and a directive is
33
+ * written only when its condition flipped, kept on while the attribute holds its class. Every
34
+ * write is guarded by `contains`, so the first run over a served page writes nothing.
35
+ */
36
+ export function setClassGroup(
37
+ element: Element,
38
+ names: string,
39
+ plain: unknown,
40
+ conditions: readonly unknown[],
41
+ ): void {
42
+ WORK.bindingRuns += 1
43
+ let held = owned.get(element)
44
+ if (held === undefined) {
45
+ held = {
46
+ text: undefined,
47
+ tokens: NO_TOKENS,
48
+ conditions: undefined,
49
+ names: names.split(' '),
50
+ }
51
+ owned.set(element, held)
52
+ }
53
+ const classes = element.classList
54
+ const text = textOf(plain)
55
+ const was = held.conditions
56
+ if (text !== held.text) {
57
+ const next = new Set<string>()
58
+ for (const token of text.split(ASCII_WHITESPACE))
59
+ if (token !== '') next.add(token)
60
+ for (const token of held.tokens) {
61
+ if (next.has(token)) continue
62
+ const at = held.names.indexOf(token)
63
+ if (at !== -1 && conditions[at]) continue
64
+ if (classes.contains(token)) classes.remove(token)
65
+ }
66
+ for (const token of next)
67
+ if (!classes.contains(token)) classes.add(token)
68
+ held.text = text
69
+ held.tokens = next
70
+ }
71
+ for (let at = 0; at < held.names.length; at += 1) {
72
+ const on = Boolean(conditions[at])
73
+ if (was !== undefined && Boolean(was[at]) === on) continue
74
+ const name = held.names[at] as string
75
+ const wanted = on || held.tokens.has(name)
76
+ if (classes.contains(name) !== wanted) classes.toggle(name, wanted)
77
+ }
78
+ held.conditions = conditions
79
+ }