gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit043624b2043624b2theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre043624b2/plugins/web/client.hl

185.4 KB

  1. // The framework's client half — a file-class constructed ONCE in the browser by
  2. // the page's module script, with the seed the server rendered from. The
  3. // components are NOT preloaded: each mount carries the url its module is served
  4. // at and `hlLoad` fetches it when the mount happens. This file is the client
  5. // realm's entrypoint, and the page script sets that realm before constructing
  6. // it.
  7. //
  8. // Its whole job: CLAIM the DOM the server rendered for the mounted instances,
  9. // remember which elements read which member (the SITES), repaint a site when a
  10. // member is written, and turn a same-origin link into a navigation that fetches
  11. // the next route's tree and state and swaps the slot — the shell stays.
  12. import HlwView from './view.hl'
  13. import HlwRouter from './router.hl'
  14. seedText = '' // the server's seed, as JSON text (the one construction argument)
  15. seed = null // { page: { key module params state view }, shell: … | null }
  16. view = new HlwView
  17. router = null // the client's route table, from the seed
  18. shell = null // { key, instance, view, sites, root }
  19. page = null
  20. appHead = null // the app's manifest head defaults, off the seed: what a page
  21. // member that is null falls back to (title, description, image,
  22. // favicon, and the app's own meta list)
  23. slotEl = null // the shell element whose children are the route component
  24. // WHERE IN IT (ticket #81's finding): the shell's slot may stand beside other elements
  25. // of the shell — `body { header { … } slot }` — and the page's nodes are then not all
  26. // of slotEl's children. `slotAt` is the index of the page's first element among them,
  27. // and `slotStart` the open mark of the region the page is (a page in a shell stands
  28. // between two marks like every region), so the page is claimed from the right element
  29. // and a navigation replaces what stands between the marks and nothing of the shell's.
  30. slotAt = 0
  31. rowKidBound = {} // component key | for site → the host members its row children are bound to
  32. reseatGen = 0 // (#71) the number of the running reseat: a node a nested changed row shares with its outer one is read once
  33. kidMoved = false // (#200) a row's composed child took a new bound value in this paint's rebinding: reseatRows rebuilds the row's `if`s that hold a child
  34. rowKidless = {} // component key | for site → true when its row body composes no child
  35. slotStart = null
  36. regionTable = {} // NAMED REGIONS (`slot { name = 'side' … }`) the shell's levels hold: name → { host start at desc kind }
  37. detachedSlot = null // the slot element a shell region's rebuild took out of the DOM (see paint)
  38. socket = null // the websocket carrier, while it is open
  39. socketOpened = false // it has opened (a socket that closes without opening is a failed reconnect)
  40. retryMs = 500 // the wait before the next reconnect: doubles per failure up to 4 s, reset on open
  41. probing = false // the probe of `/__hl/up` runs while a reconnect waits (ticket #153, probe())
  42. probeFailed = false // a probe found no server since the last socket opened
  43. // THE TAB ACROSS ITS SOCKETS (ticket #143): this document's id, the `n` of the last push it
  44. // dispatched, and whether a socket opened before — all three ride every `hello`, and the
  45. // server answers a reopened socket with what it missed
  46. tabId = null
  47. lastSeq = 0
  48. seenAbove = {} // 'n<n>' → true for a push dispatched past lastSeq (one a POST's ack carried)
  49. everOpened = false
  50. navigating = false // a navigation is running (show); navNext is the one asked for meanwhile
  51. navNext = null
  52. pinging = false // the ping interval is started once, with the first socket
  53. pongDue = null // the timer that takes the socket for dead when its pong is late
  54. nextId = 0 // the pairing id an emit's ack comes back on
  55. pending = {} // 'k<i>' → the resolver waiting for that ack
  56. // AN ANSWER THAT ARRIVES BEFORE ITS PAGE IS MOUNTED (ticket #80). A component's root
  57. // runs while `instantiate` constructs it, and an `emit server` there goes out at once
  58. // (over REST: the socket opens after boot). What the face raised for this tab comes
  59. // back in the ack, before `page` — or, on a navigation, the NEXT page — is the mount
  60. // that holds the listener; delivered then, it reached the old mounts or none and was
  61. // lost. While a mount is being built the frames wait here, and go out in arrival
  62. // order once it stands.
  63. mounting = false
  64. held = []
  65. forwarding = 0 // upward() is dispatching at the levels above a sender: their own listeners stay silent
  66. lateInner = null // the parent levels an arriving page brought below the shell (arrive), taken by the slot that opened
  67. delivering = 0 // outward() is dispatching a frame at the mounts: a page's forwarder (wireUp) stays silent then
  68. blueprints = {} // component key → { module, view }: every tree the page can
  69. // build, taken from each seed and never dropped (takeBlueprints)
  70. strict = false // the gates' client: a repaint that finds a stale site throws (staleSite)
  71. paintSet = null // the members the running repaintAll moved (a `for` over a row with a child steps over unchanged rows only if all are its own)
  72. painting = 0 // repaints running: 0 when the page stands still (the gates wait for it)
  73. turning = false // a patch is running (patch): the ones asked for meanwhile wait in patchQueue
  74. patchQueue = []
  75. sidNext = 0 // every site's own number, so a repaint can tell one that has left
  76. dropGen = 0 // counts the drops of sites (dropSitesIn): a repaint's list is checked again after one
  77. pendingDrops = [] // ranges that left holding a host's fill: that host's sites and children in them
  78. // go in settle(), once every mount on the walk has been written back
  79. pendingFills = [] // fills a region rebuild anchored but could not build in place:
  80. // the host is named by address and re-entered in settle() below
  81. // ownerId -> { key, chain, root } of the mount that owns that instance, and the reverse
  82. // (address -> ownerId, the check that an entry still names a live mount). A MOUNT RECORD
  83. // IS A VALUE, so finding a mount by walking the tree with `let kid = m.kids[k]` deep-copies
  84. // every subtree on every handler event; the index is filled where a mount is made
  85. // (`instantiateNode`, `mintFrom`) and emptied where one is replaced or removed
  86. // (`purgeMounts`), so a lookup is O(1) at any depth.
  87. mountIndex = {}
  88. mountAddr = {}
  89. lateJobs = [] // late patches (#98, #99): see latePatch
  90. lateDue = false
  91. // THE EVENTS A REFERENCE OVERWROTE ON THE COMPOSITION'S ROOT, while the walk is inside
  92. // that composition and has not yet reached an element. A composed child's View may be a
  93. // bare reference to another component (routger's dialog-form.hl is one), so the element
  94. // that FINALLY stands as the child's root can be any number of references deeper — this
  95. // rides down through them and is spent on the first elements the walk claims or builds,
  96. // which are exactly those roots.
  97. rootOverwrites = null
  98. // IS THE PAGE ON THE DOCUMENT? A shell decides per request whether its render contains
  99. // a `slot` (COMPONENTS §7, "THE PARENT IS THE GATE"): `if (loggedIn) { main { slot } }`
  100. // standing false renders no page at all, and there is then nothing to claim. False here
  101. // means there IS no page: the server did not construct it and the seed carries none
  102. // (creator, 2026-08-26 — the parent is the gate). It is asked for and constructed when
  103. // the region holding the slot turns true, and dropped again when it turns false.
  104. pageShown = false
  105. pageArrived = false // a region's rebuild constructed the page: its held frames go out after it
  106. // THE OFFLINE LAYER (COMPONENTS §10), live only when the seed says the app keeps pages
  107. // offline. `store` is the IndexedDB database, opened at boot; `unacked` holds what a
  108. // queueable emit sent on the socket carried until its ack, so a socket that closes
  109. // under it queues the emit instead of losing it; `flushing` is true while the queue
  110. // is replayed; `retryTimer` is the reconnect that is waiting.
  111. store = null
  112. unacked = {}
  113. replays = {} // 'k<i>' → what ends a replayed entry's wait when its socket closes
  114. // AN EMIT IN FLIGHT OUTLIVES ITS SOCKET (ticket #107). Every emit that went out on the
  115. // socket is here until its ack, as the frame it was: a socket that closes under it
  116. // hands it on to the POST fallback or the next socket, with the same `q`, and the
  117. // server's exactly-once path answers the repeat instead of running the face again.
  118. // `post` is true while the POST fallback carries it, `lost` once a POST failed.
  119. inflight = {} // 'k<i>' → { text; event; post; lost; }
  120. // A PAGE BEING LEFT starts nothing. Firefox closes the page's socket the moment a
  121. // document navigation starts, right after `beforeunload` and long before `pagehide`;
  122. // a reconnect or a POST made then, and an answer that loads a document, would cancel
  123. // the navigation the user started. `beforeunload` is only a maybe (a download, a
  124. // navigation the server answers with 204), so after it the page takes itself for
  125. // staying once `stayMs` have passed and it is still here.
  126. leaving = false
  127. stayTimer = null
  128. static stayMs = 3000
  129. flushing = false
  130. retryTimer = null
  131. owner = '' // whose the kept copy is (settleWho): the last user logged in
  132. onlineBound = false // the online/offline listeners are on the window (syncOnline)
  133. holding = false // an anonymous page over a user's copy: nothing is kept or laid over it
  134. heldBack = false // the server said a queued entry is for another user than the session's
  135. queueRetryTimer = null // a round that did not fully drain tries again after queueRetryWait
  136. // ---- THE PACKAGE'S BROWSER SURFACE: what `import { … } from 'hl:web'` binds ------
  137. //
  138. // A file's STATICS are what a braced import may take (the language's one rule for
  139. // `import { db } from './store.hl'`, and the same rule across a package's url),
  140. // so everything this half offers an app's component is declared here and nowhere
  141. // else. A static belongs to the CLASS, which is why these two reach each other
  142. // and why they are the SAME pair for every component module the page imports —
  143. // the browser loads this file once.
  144. //
  145. // `live` IS THE PAGE'S ONE CLIENT, written by the root below. The statics cannot
  146. // see an instance (they are evaluated at file load, before there is one), and a
  147. // static that is a FUNCTION does not need to: its body runs at the CALL, long
  148. // after the root has put the constructed client here.
  149. static live = { client = null }
  150. // PROGRAMMATIC NAVIGATION — the same act a click on a same-origin `<a>` performs,
  151. // asked for by code instead of by a pointer: a new history entry, then the next
  152. // route's tree and state over the boundary and into the shell's slot. A handler
  153. // that has just been told an id and must go to that thing's page has no link to
  154. // click, and this is what it calls instead. Everything about what a navigation IS
  155. // stays in `goTo` below; this is only the way in from outside the file.
  156. static navigate = (path) => { return live.client.goTo(path) }
  157. // A NOTIFICATION, now or at a set time (COMPONENTS §10, ticket #95). `at` is epoch
  158. // milliseconds. The browser is asked for permission the first time. It is shown by
  159. // the service worker when the app has one (so an installed app shows it as the app),
  160. // else by the page. A notification AT A SET TIME is a timer of this page: it fires
  161. // while the page is open — a browser offers no way to wake a closed app at a time
  162. // without a server push, and this does not pretend otherwise.
  163. static notify = (title, options) => { return live.client.showNotice(title, options) }
  164. static notifyAt = (at, title, options) => { return live.client.noticeAt(at, title, options) }
  165. // …or after `ms` milliseconds
  166. static notifyIn = (ms, title, options) => { return live.client.noticeIn(ms, title, options) }
  167. // FORGET WHAT THIS BROWSER KEPT OFFLINE (ticket #125): the kept state, the queue and the
  168. // worker's kept documents. hl:web does it itself when the session's user changes (a log-out
  169. // included) at the next page it is told about; an app that logs out in place calls this.
  170. // WEB PUSH (ticket #173): ask for permission, subscribe with the app's `pushKey`, and answer the
  171. // subscription as `{ endpoint, expirationTime, keys = { p256dh, auth } }` — or null when the browser
  172. // has no push, the key is not set or permission is refused. The app hands it on:
  173. // `let s = subscribePush() emit server savePush(s)`.
  174. static subscribePush = () => { return live.client.pushSubscribe() }
  175. // …and the server's half (hl:web's server.hl): a component that imports `sendPush` from 'hl:web' binds this
  176. // name in the browser, so the module loads; the call belongs in an `on server` handler, and here it only says so.
  177. static sendPush = (subscription, payload, opts) => { console.error('hl:web: sendPush sends from the server — call it in an `on server` handler or a route function') return null }
  178. static forgetOffline = () => { return live.client.forgetOffline() }
  179. // ---- boot: construct the instances the server rendered, claim their DOM ----------
  180. // THE PAGE'S CLIENT, PUBLISHED TO THE FILE'S OWN CLASS, before anything else the
  181. // root does: `boot()` below constructs the components, and a component's module
  182. // may reach `navigate` from the moment it is loaded.
  183. Client.live.client = this
  184. // A VIEW EXPRESSION in a module's code is evaluated by view.hl's one `evalExpr`, so the
  185. // language's own operators answer in the browser as they do on the server
  186. window.__hlX = (x, inst, rows) => { return view.evalExpr(x, inst, rows) }
  187. seed = resolveValues(JSON.parse(seedText))
  188. router = new HlwRouter(routes = seed.routes)
  189. boot()
  190. // THE VALUES TABLE (#180): the server writes a large value that stands in several places once,
  191. // in `seed.values[id]`, and a place holds `{ "$hlv": id }`. Every reference is resolved HERE,
  192. // as the seed is read (a navigation's answer too, in showOne) and each place gets a copy of its
  193. // own, built fresh: a child that changes its `items` does not change the page's `home`.
  194. resolveValues(s) {
  195. if (s == null || s.values == null) { return s }
  196. let table = s.values
  197. s.values = null
  198. if (s.page != null) { s.page = unshare(&table, s.page) }
  199. if (s.shell != null) { s.shell = unshare(&table, s.shell) }
  200. return s
  201. }
  202. unshare(&table, v) {
  203. if (hlTypeName(v) != 'Hybrid') { return v }
  204. if (v['$hlv'] != null) { return unshare(&table, table[v['$hlv']]) }
  205. let out = null
  206. if (Array.isArray(v)) {
  207. out = []
  208. for (x of v) { out.push(unshare(&table, x)) }
  209. } else {
  210. out = {}
  211. for (k of v.keys()) { out[k] = unshare(&table, v[k]) }
  212. }
  213. return out
  214. }
  215. boot() {
  216. mounting = true
  217. strict = seed.strict == true
  218. takeBlueprints(seed.blueprints)
  219. if (seed.shell != null) {
  220. shell = instantiate(seed.shell, [], 'shell')
  221. shell.isShell = true
  222. // (the BODY ELEMENT, nothing to do with `rootKind` below — a mount's `rootKind`
  223. // says which of the two roots it hangs from, 'page' or 'shell'. This field held
  224. // the DOM node under the same name until 2026-09-14, and the shell's address
  225. // therefore read as an element: every lift and every fill re-entry from a
  226. // composition IN THE SHELL resolved against the page instead, so a composed
  227. // control there wrote nothing — creator, W15.)
  228. shell.root = document.body
  229. // the body node's children are claimed against document.body, so the body
  230. // element takes the node itself here: a region hosted directly on it (an `if`
  231. // at the shell's root) rebuilds from it (measured 2026-09-13: a lone child
  232. // under `body` stayed on screen after its condition turned false)
  233. claim(&shell, document.body)
  234. }
  235. page = seed.page != null ? instantiate(seed.page, [], 'page') : null
  236. // no shell: the page IS the document's body content
  237. if (shell == null) { slotEl = document.body }
  238. // THE SHELL RENDERED NO SLOT — its region stood false for this request, so the
  239. // document carries no page. Claiming one anyway walked the page's tree against the
  240. // SHELL's own elements and silently attached the page's nodes to them ("claim: no
  241. // element for … under …" as soon as the two shapes differ), and the page never
  242. // appeared when the region later turned true. It waits instead.
  243. if (slotEl != null && page != null) {
  244. slotEl.__hlCur = slotStart
  245. claim(&page, slotEl, slotAt)
  246. pageShown = true
  247. }
  248. // the shell's named regions take their content: the filling level's, or their own default
  249. if (shell != null) { placeRegions(true) }
  250. // the head members are sites of this mount from here on. They are NOT painted
  251. // now: the server's `document()` already wrote them into this document, and a
  252. // write to one of them repaints it the way every other member's write does.
  253. appHead = seed.head
  254. if (page != null) { headSites(&page) }
  255. release()
  256. listen()
  257. syncOnline()
  258. if (seed.offline != null) { standIn() }
  259. if (seed.offline != null) { offlineBoot() }
  260. connect()
  261. }
  262. // A DOCUMENT THAT STANDS IN FOR THIS URL (#124): the worker answered an unvisited param page
  263. // with a kept document of the same component, so the seed's parameters are another url's.
  264. // This url's are the truth: they go into the instance, and what they feed is derived again
  265. // here (what only the server can derive stays as that document had it, until a push).
  266. standIn() {
  267. if (page == null || seed.page == null) { return null }
  268. let m = router.match(location.pathname)
  269. if (m == null || m.route.component != true) { return null }
  270. let mine = JSON.stringify(m.params)
  271. if (mine == JSON.stringify(seed.page.params)) { return null }
  272. for (k of m.params.keys()) { page.instance[k] = m.params[k] }
  273. seed.page.params = m.params
  274. patch(page.chain, 'page', moved(&page, m.params.keys()), [])
  275. return null
  276. }
  277. // THE NEXT PAGE INTO THE SLOT, and nothing else of the shell's: the slot's element may
  278. // hold the shell's own elements beside the page (`body { header { … } slot }`), which
  279. // emptying it took with the old page. The new page is built where the old one stood.
  280. placePage(&m) {
  281. if (!standing(slotEl, slotStart)) {
  282. // the page is all the slot's element holds: empty it, as it always was
  283. slotEl.replaceChildren()
  284. create(&m, slotEl)
  285. return null
  286. }
  287. for (n of between(slotStart)) { n.remove() }
  288. let frag = document.createDocumentFragment()
  289. let before = sidNext
  290. create(&m, frag)
  291. slotEl.insertBefore(frag, slotStart.__hlEnd)
  292. // the sites the walk registered on the fragment (a region at the page's root, a composed
  293. // child's) belong to the slot's element it landed in (#191: they stayed on the empty fragment,
  294. // and the first click on a child's button after a navigation did nothing)
  295. rehome(&m, frag, slotEl, before)
  296. return null
  297. }
  298. // the frames that waited for the mounts (above), now that they stand
  299. release() {
  300. mounting = false
  301. let frames = held
  302. held = []
  303. for (f of frames) { outward(f) }
  304. return null
  305. }
  306. // a mount's instance: the module LOADED (hlLoad — the loader primitive, which
  307. // on this target is the dynamic import of the url the server compiled the
  308. // component to), the class constructed, then the server's state laid over it.
  309. // The import is the browser's own cache: a second mount of the same component
  310. // fetches nothing.
  311. // The route's params and the server's state are the construction's NAMED
  312. // ARGUMENTS, pinned before the root runs — so a member the server evaluated
  313. // (a seeded one, declared without its initializer in this realm's projection)
  314. // is already there when a later line reads it, and a client-evaluable
  315. // initializer keeps the server's value instead of recomputing it.
  316. // THE BLUEPRINT TABLE, read by component key: the url the component's module is
  317. // served at and its View tree, one copy for the whole page. Every mount's tree is
  318. // read from here — the page's, the shell's, and every child's — and so is every
  319. // child the browser has to build itself.
  320. //
  321. // IT ONLY GROWS. Each seed carries the table for ITS route, and a navigation takes
  322. // the new entries WITHOUT dropping the old: a key is a component and a component is
  323. // one tree, so an entry is never wrong, and constructing a mount awaits its module —
  324. // long enough for a second navigation to have replaced the seed underneath it. A
  325. // table that was replaced left that half-built page reading the wrong route's
  326. // entries ("no blueprint for components/post.hl", on `/`, measured in the reference
  327. // gate 2026-09-13).
  328. takeBlueprints(table) {
  329. for (k of table.keys()) {
  330. if (blueprints[k] == null) { blueprints[k] = table[k] }
  331. }
  332. return null
  333. }
  334. blueprintOf(key) {
  335. return blueprints[key]
  336. }
  337. treeOf(key) {
  338. let bp = blueprintOf(key)
  339. // the table covers every component the page can reach, so this is a torn seed
  340. // and not a missing feature — say so loudly and paint nothing for that mount
  341. if (bp == null) { console.error('framework: the seed carries no blueprint for ' + key)
  342. return [] }
  343. return bp.view
  344. }
  345. // `chain` and `root` are the mount's ADDRESS: the kid keys from the page (or the
  346. // shell) down to it. A mount record is a value, so a site cannot hold the mount that
  347. // owns its fill — it holds this address, and `fillAt` re-enters the mount by
  348. // reference from the root when the fill has to be rebuilt (see settle below).
  349. instantiate(m, chain, root) {
  350. purgeMounts(chain, root)
  351. return instantiateNode(m, chain, root)
  352. }
  353. // a mount made at (chain, root) replaces whatever was there: forget it and everything under it
  354. purgeMounts(chain, root) {
  355. let base = addressOf(chain, root)
  356. let sep = chain.length == 0 ? '' : '/'
  357. let gone = []
  358. for (a of mountAddr.keys()) {
  359. if (a == base || a.startsWith(base + sep)) { gone.push(a) }
  360. }
  361. for (a of gone) {
  362. delete mountIndex['' + mountAddr[a]]
  363. delete mountAddr[a]
  364. }
  365. return null
  366. }
  367. registerMount(inst, key, chain, root) {
  368. let addr = addressOf(chain, root)
  369. if (mountAddr[addr] != null) { purgeMounts(chain, root) }
  370. let id = inst.__hlOwnerId
  371. if (id == null) { return null }
  372. mountIndex['' + id] = { key = key; chain = chain; root = root; }
  373. mountAddr[addr] = id
  374. if (root == 'page' && chain.length == 0) { wireUp(inst, key, levelCount() + 1) }
  375. if (root == 'shell' && chain.length > 0 && chain[chain.length - 1] == '^') { wireUp(inst, key, chain.length) }
  376. return null
  377. }
  378. // A PAGE'S EMIT REACHES ITS PARENT SHELL'S `on` (ticket #193). The levels stand in the
  379. // browser, so the page is its own instance and `emit shellSession(x)` in its View finds
  380. // no handler on it: the handler is the shell's. Every event the shell declares gets a
  381. // listener on the PAGE INSTANCE's own bus (the language's instance-scoped handler, the one
  382. // `on page.ev` registers, which every dispatch at that instance fires last) that hands the
  383. // emit to the shell's instance through the dispatch `outward` uses (`hlEmitArgs`) and
  384. // repaints what the shell's handler wrote. Server cost: none, nothing leaves the browser.
  385. wireUp(inst, key, depth) {
  386. if (seed.shell == null || window.__hlC == null) { return null }
  387. if (window.__hlC[key] == null || window.__hlC[key].on == null) { return null }
  388. // every event any level above this mount declares (a level's table is its own compiled `ev`)
  389. let seen = {}
  390. let at = seed.shell
  391. let i = 0
  392. while (at != null && i < depth) {
  393. let tbl = window.__hlC[at.key]
  394. if (tbl != null && tbl.ev != null) {
  395. for (ev of tbl.ev.keys()) {
  396. if (seen[ev] == null) { seen[ev] = true wireEvent(&inst, key, ev, depth) }
  397. }
  398. }
  399. at = at.kids['^']
  400. i = i + 1
  401. }
  402. return null
  403. }
  404. // how many parent levels stand above the page: the shell and every '^' kid below it
  405. levelCount() {
  406. let n = 0
  407. let at = shell
  408. while (at != null) { n = n + 1 at = at.kids['^'] }
  409. return n
  410. }
  411. wireEvent(&inst, key, ev, depth) {
  412. window.__hlC[key].on(&inst, ev, (a, b, c, d, e) => { return upward(ev, [a, b, c, d, e], depth) })
  413. return null
  414. }
  415. // The emit goes to the `depth` levels above its sender, nearest first, each level's handlers
  416. // once. `forwarding` keeps the dispatch AT a level (the levels' own listeners) silent.
  417. upward(ev, args, depth) {
  418. // a frame the server pushed reaches the shell on its own (outward), not through the page
  419. if (shell == null || delivering > 0 || forwarding > 0) { return null }
  420. let payload = args
  421. while (payload.length > 0 && payload[payload.length - 1] == null) { payload = payload.slice(0, payload.length - 1) }
  422. let lv = []
  423. let at = shell
  424. while (at != null && lv.length < depth) { lv.push(at) at = at.kids['^'] }
  425. forwarding = forwarding + 1
  426. let j = lv.length - 1
  427. while (j >= 0) {
  428. let m = lv[j]
  429. let tbl = window.__hlC[m.key]
  430. if (tbl != null && tbl.ev != null && tbl.ev[ev] != null) {
  431. let inst = m.instance
  432. hlEmitArgs(&inst, ev, payload)
  433. patch(m.chain, m.rootKind, eventWrites(m.key, ev), [])
  434. }
  435. j = j - 1
  436. }
  437. forwarding = forwarding - 1
  438. return null
  439. }
  440. instantiateNode(m, chain, root) {
  441. let args = {}
  442. for (k of m.state.keys()) { args[k] = m.state[k] }
  443. for (k of m.params.keys()) { args[k] = m.params[k] }
  444. let inst = hlLoad(m.module, args)
  445. // THE TREE COMES FROM THE TABLE, by this mount's key: a View belongs to the
  446. // COMPONENT, and the seed carries one copy of it however many mounts share it.
  447. let out = { key = m.key; instance = inst; view = treeOf(m.key); sites = []; isShell = chain.length > 0 && chain[chain.length - 1] == '^'; kids = {}; bindings = []; chain = chain; rootKind = root; }
  448. registerMount(inst, m.key, chain, root)
  449. // THE MIRROR: the components this one composes are instances here too, exactly
  450. // as the server stacked them — one per component node, constructed with the
  451. // bindings the server evaluated; a binding to a host MEMBER is remembered, so a
  452. // host write reaches the child's member and repaints the child's sites
  453. for (k of m.kids.keys()) { out.kids[k] = instantiateNode(m.kids[k], childChain(chain, k), root) }
  454. bindKids(&out, out.view)
  455. // A BOUND MEMBER'S VALUE IS THE HOST'S, HERE. The seed carried the server's
  456. // value, which is right for data and null for a FUNCTION (JSON has no form for
  457. // one) — and a function the host hands its child (`PostForm { onCancel = cancel
  458. // }`) is the way a child talks upward without knowing its host: it calls what it
  459. // was given, and the function writes the host's members through the instance
  460. // that wrote it. So every member binding is applied from the host instance now.
  461. for (b of out.bindings) {
  462. if (out.kids[b.kid] != null) {
  463. out.kids[b.kid].instance[b.target] = bindingValue(b, &out)
  464. }
  465. }
  466. return out
  467. }
  468. bindKids(&m, nodes) {
  469. for (n of nodes) {
  470. if (n.k == 'component') {
  471. // THE EDGE STANDS WHETHER OR NOT THE SERVER MOUNTED THE CHILD: a branch that does
  472. // not stand is not mounted, and the browser builds the child when it comes to
  473. // stand — which must then follow the host's member (every reader of `m.bindings`
  474. // passes over an edge whose child is not there)
  475. {
  476. // A BINDING IS A READ at the reference site: a member (`count = likes`)
  477. // or a field path off one (`postId = row.id`). Both follow the host's
  478. // write — the field path under the name it starts at.
  479. for (b of n.bindings) {
  480. // A MEMBER THAT ONLY FEEDS A BINDING still follows a write: `repaint`
  481. // walks the bindings after the sites, so a host member no element of the
  482. // host shows still reaches the child that was bound to it (measured
  483. // 2026-09-13 on the social app: `FollowButton { following =
  484. // authorFollowed }` stayed false). Under the digest that needed the
  485. // member to be in the compared map; under the write sets it needs
  486. // nothing but the edge itself.
  487. if (b.member != null) { m.bindings.push({ name = b.member; kid = view.kidKey(n.path); target = b.name; ref = null; }) }
  488. else if (b.ref != null) { m.bindings.push({ name = b.ref.name; kid = view.kidKey(n.path); target = b.name; ref = b.ref; }) }
  489. }
  490. }
  491. // the fill is THIS mount's fragment: a reference inside it binds against this instance
  492. if (n.fill != null) { bindKids(&m, n.fill) }
  493. } else if (n.k == 'el') {
  494. bindKids(&m, n.children)
  495. } else if (n.k == 'if') {
  496. bindKids(&m, n.then)
  497. bindKids(&m, n.other)
  498. }
  499. }
  500. return null
  501. }
  502. // a body-rooted View's children are the body's children
  503. childrenOf(nodes) {
  504. if (nodes.length == 1 && nodes[0].k == 'el' && nodes[0].tag == 'body') { return nodes[0].children }
  505. return nodes
  506. }
  507. // ---- WHAT THE COMPILE STEP ANSWERED, asked by name ---------------------------------
  508. // A local event's write set stands at its own listener and a region's names at its own
  509. // region: the compile step wrote them into the statements. These three are asked for by
  510. // NAME at run time and cannot be — an inbound frame names its event, a routine a host
  511. // handed down names the member that holds it, and a write set names the derivations
  512. // that follow it — so the component's compiled half carries them beside its walks.
  513. // Nothing here reduces a table: the answers are already the answers.
  514. eventWrites(key, event) {
  515. let names = compiledEntry(key, 'ev', event)
  516. return names == null ? [] : names
  517. }
  518. memberWrites(key, name) {
  519. let names = compiledEntry(key, 'mw', name)
  520. return names == null ? [] : names
  521. }
  522. derivationsOf(key) {
  523. if (window.__hlC == null || window.__hlC[key] == null) { return [] }
  524. return window.__hlC[key].dv
  525. }
  526. // one entry of one table of a compiled component, read in place: `compiledOf` hands a
  527. // copy of the whole compiled half, which a push to every mount cannot afford (#138)
  528. compiledEntry(key, table, name) {
  529. if (window.__hlC == null || window.__hlC[key] == null || window.__hlC[key][table] == null) { return null }
  530. return window.__hlC[key][table][name]
  531. }
  532. // ---- THE COMPILED COMPONENT: this framework's own output for this file ------------
  533. // The old hl:web walked a tree the seed carries — it pairs an element with a node, asks a table
  534. // what the element shows, and asks `view.value` what each bound spot holds. hl:web
  535. // ships the answers as the component's own code (plugins/web/compile.hl), and the
  536. // functions below are the FIXED runtime that code calls: the mount records, the keyed
  537. // region diff, the binding edges, the derivations, the socket and navigation stay here,
  538. // where they belong to the framework and not to any one component.
  539. //
  540. // EVERY CALL INTO GENERATED CODE PASSES BY REFERENCE. A plain call copies its
  541. // arguments (the language's value semantics), which for an instance holding a 2000-row
  542. // list would copy the list on every paint; `&` at the call site hands the value itself,
  543. // the way every walk in this file already hands a mount record.
  544. compiledOf(key) {
  545. let all = window.__hlC
  546. if (all == null) { return null }
  547. return all[key]
  548. }
  549. // one paint entry of a compiled component, read in place (compiledOf hands a copy of
  550. // the whole table, which a row repaint cannot afford per element)
  551. paintEntry(key, site) {
  552. if (window.__hlC == null || window.__hlC[key] == null) { return null }
  553. return window.__hlC[key].p[site]
  554. }
  555. // the walk of a component's whole View, of one `for` body, of an `if` branch, or of
  556. // the fragment a reference fills — each under its own site
  557. walkOf(key, slot, site) {
  558. // each slot is read in place off the global table (the `paintEntry` way): `compiledOf`
  559. // would copy the whole compiled component for every `if` site and every `for` row
  560. if (window.__hlC == null || window.__hlC[key] == null) { return null }
  561. if (slot == 'v') { return window.__hlC[key].v }
  562. if (slot == 'b') { return window.__hlC[key].b[site] }
  563. if (slot == 't') { return window.__hlC[key].t[site] }
  564. if (slot == 'e') { return window.__hlC[key].e[site] }
  565. if (slot == 'f') { return window.__hlC[key].f[site] }
  566. if (slot == 'd') { return window.__hlC[key].d[site] }
  567. if (slot == 'r') { return window.__hlC[key].r[site] }
  568. return null
  569. }
  570. // ONE ENTRY INTO GENERATED CODE. `f` is the walk, `m` the mount it runs for, `o` the
  571. // mount whose fragment a `slot` inside it would place.
  572. run(f, &m, &o, host, i, rows, rk, fill, cr, opt) {
  573. if (f == null) { return i }
  574. let c = this
  575. return f(&c, &m, &o, &host, i, &rows, rk, &fill, cr, &opt)
  576. }
  577. acqFail(tag, host) {
  578. console.error('claim: no element for', tag, 'under', host == null ? null : host.tagName)
  579. return null
  580. }
  581. // the classes a reference's `#Child` rule put on the composition root, beside whatever
  582. // class the root itself carries (view.hl withRootClasses, on the build walk)
  583. rootClasses(el, opt) {
  584. if (opt == null || opt.classes == null || opt.classes.length == 0) { return null }
  585. let add = ''
  586. for (c of opt.classes) { add = add == '' ? c : add + ' ' + c }
  587. let have = el.getAttribute('class')
  588. el.setAttribute('class', have == null || have == '' ? add : have + ' ' + add)
  589. return null
  590. }
  591. // AN ELEMENT THAT SHOWS SOMETHING is a site of this mount. The names come from the
  592. // generated call — the compile step read them off the View — and the two-way `value`
  593. // bind is applied where the element is a form control the member paints.
  594. siteAt(&m, el, names, site, valueMember, isSelect, rows) {
  595. let keep = []
  596. for (name of names) {
  597. // the SHELL's `slot` is where the route component hangs: swapped by navigation,
  598. // never painted (the compile step already drops `Style`, folded at build)
  599. if (!(name == 'slot' && m.isShell)) { keep.push(name) }
  600. }
  601. if (valueMember != null && rows[valueMember] == null && el.value != null) {
  602. bindValue(&m, valueMember, el, isSelect)
  603. }
  604. if (keep.length > 0) { let sid = nextSid() m.sites.push({ sid = sid; el = el; site = site; names = keep; region = false; rows = rows; }) if (el.__hlSid == null) { el.__hlSid = sid } else { el.__hlSidMore = (el.__hlSidMore == null ? '' : el.__hlSidMore) + sid + ',' } }
  605. return null
  606. }
  607. // (#71) a site's node carries the site's number (`__hlSid`, the first, and `__hlSidMore`, set inline where the site registers: a call with the node
  608. // as argument is a copy per site), so a reseat finds the sites of the rows it touches from their nodes. A region is tagged
  609. // on its open mark, the node the reseat walk starts from.
  610. applySelectAt(&m, el, v) {
  611. el.value = v == null ? '' : '' + v
  612. return null
  613. }
  614. // A HANDLER, WIRED AT CREATION. The literal is the element's own — its named entries
  615. // are the generated `__hlLit`, the `for` rows in scope ride on it, and the owner is the
  616. // instance BY REFERENCE (a handler writing a copy would repaint nothing).
  617. onAt(&m, el, event, site, rows, wrote, touched) {
  618. if (el.__hlLits == null) { el.__hlLits = {} }
  619. el.__hlLits[event] = literalFor(&m, el)
  620. let at = m.chain
  621. let root = m.rootKind
  622. let rowNames = rows == null ? [] : rows.keys()
  623. el.addEventListener(event, (ev) => {
  624. // THE DEFAULT ACTION OF `submit` IS A PAGE NAVIGATION (COMPONENTS.md §6b), so the
  625. // framework suppresses it itself — a handler declared with no parameter has no
  626. // `ev` to call `preventDefault()` on, and one declared with it may call it again,
  627. // harmlessly. This is the FIRST statement, unawaited, for the same reason `onClick`
  628. // claims a link before anything else: called after an `await`, it lands a
  629. // microtask late, once the browser already navigated (mission 132 / ticket submit).
  630. if (event == 'submit') { ev.preventDefault() }
  631. let lit = el.__hlLits[event]
  632. for (rk of rowNames) { lit[rk] = el.__hlRows[rk] }
  633. fire(&lit, event, ev)
  634. el.__hlLits[event] = lit
  635. patch(at, root, wrote, touched)
  636. })
  637. return null
  638. }
  639. // THE SHELL'S SLOT: where the route component hangs. On the build walk the re-created
  640. // element adopts the page's nodes, and a page the shell never rendered is built here.
  641. slotShell(&m, host, i, cr) {
  642. // A PARENT LEVEL BELOW THE SHELL (#194): this level's slot holds the next parent level,
  643. // kid '^' (the page stands in the innermost level's slot). It leaves no mark of its own.
  644. let inner = m.kids['^']
  645. // A SLOT THAT OPENS LATER (#194): the levels below this one were never built (the shell's
  646. // slot stood closed when the document was made). The answer for the route brings the
  647. // page AND every level between, so the level below this one is built from it now
  648. if (inner == null && cr && !pageShown && page == null) {
  649. page = arrive()
  650. if (lateInner != null) {
  651. m.kids['^'] = instantiate(lateInner, childChain(m.chain, '^'), m.rootKind)
  652. if (m.chain.length == 0 && seed.shell != null) { seed.shell.kids['^'] = lateInner }
  653. lateInner = null
  654. inner = m.kids['^']
  655. }
  656. }
  657. if (inner != null) {
  658. inner.chain = childChain(m.chain, '^')
  659. inner.rootKind = m.rootKind
  660. if (cr) { inner.sites = [] }
  661. let f = walkOf(inner.key, 'v', null)
  662. let out = run(f, &inner, &m, host, cr ? 0 : i, {}, '', null, cr, null)
  663. if (!cr) { i = out }
  664. if (cr || inner.bindings.length == 0) { inner.bindings = [] bindKids(&inner, inner.view) }
  665. m.kids['^'] = inner
  666. return i
  667. }
  668. slotEl = host
  669. if (!cr) {
  670. // claiming: the page's elements stand here, and the shell's own elements after
  671. // the slot come after them
  672. slotAt = i
  673. if (seed.page == null) { return i }
  674. // the page's region: its marks, and the elements between them the shell steps over
  675. slotStart = markAt(host, false, true)
  676. slotStart.__hlEnd = closeOf(slotStart)
  677. host.__hlCur = slotStart.__hlEnd
  678. let span = 0
  679. for (n of between(slotStart)) { if (n.nodeType == 1) { span = span + 1 } }
  680. return i + span
  681. }
  682. slotAt = host.children.length
  683. let start = markAt(host, true, true)
  684. if (detachedSlot != null && detachedSlot != host) {
  685. // the page's own nodes: what stood between its marks, or all of the old host's
  686. let moving = standing(detachedSlot, slotStart) ? between(slotStart) : Array.from(detachedSlot.childNodes)
  687. for (node of moving) { host.appendChild(node) }
  688. }
  689. detachedSlot = null
  690. if (!pageShown) {
  691. // THE PAGE ARRIVES WITH ITS SLOT: it was never constructed while the shell
  692. // placed none, so it is asked for now — the route's state, from the server
  693. if (page == null) { page = arrive() }
  694. if (page != null) {
  695. page.sites = []
  696. create(&page, host)
  697. headSites(&page)
  698. wearHead(&page)
  699. pageShown = true
  700. placeRegions(false)
  701. }
  702. }
  703. start.__hlEnd = markAt(host, true, false)
  704. slotStart = start
  705. return i
  706. }
  707. // THE ROUTE'S PAGE, for a slot that just opened: the navigation face answers it for
  708. // this path, told the slot stands here, and the mount is constructed from that. The
  709. // frames it raises wait until the rebuild that placed it is done (pageArrived).
  710. arrive() {
  711. // the answer carries the values table (#180) like a navigation's: resolved before anything reads it
  712. let next = resolveValues(emit server page(location.pathname + location.search, true))
  713. if (next == null || next.page == null) { return null }
  714. takeBlueprints(next.blueprints)
  715. seed.page = next.page
  716. lateInner = next.shell != null ? next.shell.kids['^'] : null
  717. mounting = true
  718. pageArrived = true
  719. return instantiate(next.page, [], 'page')
  720. }
  721. // THE SLOT CLOSED: the page is dropped, not kept aside — the next time the slot opens
  722. // it is constructed again. The head falls back to the app's own.
  723. dropPage(&m) {
  724. // THE LEVELS BELOW THE CLOSED SLOT GO WITH THE PAGE (#194): the level this slot belonged to held
  725. // the next parent level as kid '^' — its sites, listeners and record are dropped, not left stale;
  726. // the slot's next opening builds them again (lateInner)
  727. if (m.kids['^'] != null) {
  728. purgeMounts(childChain(m.chain, '^'), m.rootKind)
  729. delete m.kids['^']
  730. if (m.chain.length == 0 && seed.shell != null && seed.shell.kids != null) { delete seed.shell.kids['^'] }
  731. }
  732. slotEl = null
  733. pageShown = false
  734. page = null
  735. purgeMounts([], 'page')
  736. slotStart = null
  737. seed.page = null
  738. let none = { instance = {}; }
  739. wearHead(&none)
  740. announceMounts()
  741. return null
  742. }
  743. // a rebuild that constructed the page: what waited for it goes out, and the server
  744. // learns the tab mounts it
  745. arrived() {
  746. if (!pageArrived) { return null }
  747. pageArrived = false
  748. release()
  749. announceMounts()
  750. return null
  751. }
  752. // A CHILD'S SLOT: the fragment its HOST wrote, built in the host's frame. The fill names
  753. // that frame by address, and `run` re-enters it when this walk is not it.
  754. slotFill(&m, &o, host, i, fill, cr) {
  755. if (fill == null) { return i }
  756. if (cr && fill.detached) {
  757. let anchor = document.createComment('hl:slot')
  758. host.appendChild(anchor)
  759. pendingFills[] = { key = fill.key; site = fill.site; rows = fill.rows; rowKey = fill.rowKey; outer = fill.outer; chain = fill.chain; rootKind = fill.rootKind; anchor = anchor; }
  760. return i
  761. }
  762. if (ownsFill(o, fill)) {
  763. let f = walkOf(fill.key, 'f', fill.site)
  764. return run(f, &o, &o, host, i, fill.rows, fill.rowKey, fill.outer, cr, null)
  765. }
  766. return fillFromRoot(fill, host, i, cr)
  767. }
  768. // ---- NAMED REGIONS (D-CHAIN addendum) -------------------------------------------------
  769. // A level above declares `slot { name = 'side' … }`: a region between two marks, holding its
  770. // default (built in the declaring level's frame, walk 'd') or the fill a lower level wrote at its
  771. // View root (`side { … }`, built in THAT level's frame, walk 'r'). The region element stands; a
  772. // navigation rebuilds only what stands between the marks.
  773. namedRegionAt(&m, &o, host, i, site, name, rows, rk, fill, cr) {
  774. let desc = { key = m.key; site = site; rows = rows; rowKey = rk; outer = fill; chain = m.chain; rootKind = m.rootKind; slot = 'd'; }
  775. if (!cr) {
  776. // claiming: the region's marks are taken and its content is claimed once the page is (boot)
  777. let start = markAt(host, false, true)
  778. start.__hlEnd = closeOf(start)
  779. host.__hlCur = start.__hlEnd
  780. let span = 0
  781. for (n of between(start)) { if (n.nodeType == 1) { span = span + 1 } }
  782. regionTable[name] = { host = host; start = start; at = i; base = desc; desc = desc; kind = null; }
  783. return i + span
  784. }
  785. let start = markAt(host, true, true)
  786. start.__hlEnd = mark(false)
  787. host.appendChild(start.__hlEnd)
  788. let fd = fillerOf(name)
  789. // (a hybrid read off a table is a copy: the entry is written whole, never through a read of it)
  790. regionTable[name] = { host = host; start = start; at = 0; base = desc; desc = fd == null ? desc : fd; kind = fd == null ? 'd' : 'r'; }
  791. if (fd != null) {
  792. fillFromRoot(fd, host, 0, true)
  793. } else {
  794. run(walkOf(m.key, 'd', site), &m, &o, host, 0, rows, rk, fill, true, null)
  795. }
  796. // built in place before the close mark (the walk appended after it)
  797. host.appendChild(start.__hlEnd)
  798. return i
  799. }
  800. // the level below the declaring one that fills region `name`: the page first, then the levels
  801. // from the innermost out. A descriptor naming the filler's frame, or null (the default stands).
  802. fillerOf(name) {
  803. let cands = []
  804. if (page != null) { cands.push(page) }
  805. let lv = []
  806. let x = shell
  807. while (x != null && lv.length < 64) { lv.push(x) x = x.kids == null ? null : x.kids['^'] }
  808. let k = lv.length - 1
  809. while (k > 0) { cands.push(lv[k]) k = k - 1 }
  810. for (c of cands) {
  811. let site = regionFillSite(c.key, name)
  812. // a conditional fill holds while its condition does (read off the filling instance)
  813. if (site != null && window.__hlC[c.key].rc != null && window.__hlC[c.key].rc[name] != null) {
  814. let cf = window.__hlC[c.key].rc[name]
  815. let nr = {}
  816. if (!cf(&c.instance, &nr)) { site = null }
  817. }
  818. if (site != null) { return { key = c.key; site = site; rows = {}; rowKey = ''; outer = null; chain = c.chain; rootKind = c.rootKind; slot = 'r'; } }
  819. }
  820. return null
  821. }
  822. regionFillSite(key, name) {
  823. if (window.__hlC == null || window.__hlC[key] == null || window.__hlC[key].rn == null) { return null }
  824. return window.__hlC[key].rn[name]
  825. }
  826. // every named region takes what its level or the filler below it holds: after a page is claimed
  827. // (boot, `claim` true: the server's nodes are taken) or placed (navigation: rebuilt)
  828. placeRegions(claimIt) {
  829. if (!claimIt) {
  830. for (name of regionTable.keys()) { placeRegion(name, false) }
  831. return null
  832. }
  833. // claiming a region's default may claim regions inside it (their marks are taken on the way and
  834. // their content waits): pass again until every one that stands has its content
  835. let pass = 0
  836. let pending = true
  837. while (pending && pass < 16) {
  838. pending = false
  839. pass = pass + 1
  840. for (name of regionTable.keys()) {
  841. let r = regionTable[name]
  842. if (r.kind == null && standing(r.host, r.start)) { pending = true placeRegion(name, true) }
  843. }
  844. }
  845. return null
  846. }
  847. placeRegion(name, claimIt) {
  848. let r = regionTable[name]
  849. if (r == null || !standing(r.host, r.start)) { return null }
  850. let fd = fillerOf(name)
  851. let want = fd == null ? 'd' : 'r'
  852. if (claimIt) {
  853. let d = fd == null ? r.base : fd
  854. regionTable[name].kind = want
  855. regionTable[name].desc = d
  856. r.host.__hlCur = r.start
  857. regionClaim(d, r.host, r.at)
  858. return null
  859. }
  860. // the default stands and stays the default: its state and its nodes are not rebuilt
  861. if (want == 'd' && r.kind == 'd') { return null }
  862. let old = between(r.start)
  863. let owner = r.desc
  864. if (owner.rootKind == 'shell' || owner.slot == 'd') { regionDrop(owner, r.host, old) }
  865. for (n of old) { n.remove() }
  866. let d = fd == null ? r.base : fd
  867. regionTable[name].desc = d
  868. regionTable[name].kind = want
  869. regionBuild(d, r.host, r.start.__hlEnd)
  870. return null
  871. }
  872. // claims the server's nodes of a region's content, in the frame its descriptor names
  873. regionClaim(d, host, at) {
  874. fillFromRoot(d, host, at, false)
  875. return null
  876. }
  877. // builds a region's content in a fragment and puts it before the close mark; the sites the walk
  878. // registered on the fragment belong to the region's host (as `placePage` does)
  879. regionBuild(d, host, closeMark) {
  880. let frag = document.createDocumentFragment()
  881. let before = sidNext
  882. fillFromRoot(d, frag, 0, true)
  883. host.insertBefore(frag, closeMark)
  884. regionRehome(d, frag, host, before)
  885. return null
  886. }
  887. regionRehome(d, frag, host, before) {
  888. if (d.rootKind == 'shell') { if (shell != null) { regionRehomeAt(&shell, d, 0, frag, host, before) } }
  889. else if (page != null) { regionRehomeAt(&page, d, 0, frag, host, before) }
  890. return null
  891. }
  892. regionRehomeAt(&m, d, depth, frag, host, before) {
  893. if (depth >= d.chain.length) { rehome(&m, frag, host, before) return null }
  894. if (m.kids[d.chain[depth]] == null) { return null }
  895. regionRehomeAt(&m.kids[d.chain[depth]], d, depth + 1, frag, host, before)
  896. return null
  897. }
  898. // the sites of the nodes that leave belong to the frame that built them
  899. regionDrop(d, host, nodes) {
  900. if (d.rootKind == 'shell') { if (shell != null) { regionDropAt(&shell, d, 0, host, nodes) } }
  901. else if (page != null) { regionDropAt(&page, d, 0, host, nodes) }
  902. return null
  903. }
  904. regionDropAt(&m, d, depth, host, nodes) {
  905. if (depth >= d.chain.length) { dropSitesIn(&m, host, nodes) return null }
  906. if (m.kids[d.chain[depth]] == null) { return null }
  907. regionDropAt(&m.kids[d.chain[depth]], d, depth + 1, host, nodes)
  908. return null
  909. }
  910. // the fill's owner, re-entered from the page or the shell by the address it carries
  911. fillFromRoot(fill, host, at, cr) {
  912. if (fill.rootKind == 'shell') {
  913. if (shell == null) { return at }
  914. return fillInto(&shell, fill, 0, host, at, cr)
  915. }
  916. if (page == null) { return at }
  917. return fillInto(&page, fill, 0, host, at, cr)
  918. }
  919. fillInto(&m, fill, depth, host, at, cr) {
  920. if (depth >= fill.chain.length) {
  921. let f = walkOf(fill.key, fill.slot == null ? 'f' : fill.slot, fill.site)
  922. return run(f, &m, &m, host, at, fill.rows, fill.rowKey, fill.outer, cr, null)
  923. }
  924. if (m.kids[fill.chain[depth]] == null) { return at }
  925. return fillInto(&m.kids[fill.chain[depth]], fill, depth + 1, host, at, cr)
  926. }
  927. // the fill context a reference hands its child: the fragment's owner by address, the
  928. // host's rows and rowKey, and the fill the host itself was standing in
  929. fillFor(&m, spec, rows, rowKey, fill) {
  930. if (!spec.fill) { return null }
  931. return { key = m.key; site = spec.site; rows = rows; rowKey = rowKey; outer = fill; chain = m.chain; rootKind = m.rootKind; detached = false; }
  932. }
  933. // ---- THE MARKS: every region stands between two comments ---------------------------
  934. // THE INVARIANT (tickets #103, #104). Every region — an `if`, a `for`, each row of a
  935. // `for`, the shell's page — stands on its host between an OPEN mark (a comment `[`)
  936. // and a CLOSE mark (`]`), and ITS NODES ARE EXACTLY THE NODES BETWEEN THEM. A region
  937. // inside another one on the same host stands between the outer one's marks. So:
  938. // - a region's nodes are never remembered, they are read off the DOM between its
  939. // marks when they are needed — whatever a region inside it did since, they are
  940. // right (a list taken when the branch was built went stale, #103);
  941. // - a region is replaced by removing what stands between its marks and putting the
  942. // new nodes before its close mark; a row moves and leaves WITH its marks, so a row
  943. // is always whole and in its list;
  944. // - nothing outside a region is touched by it, and nothing inside it outlives it: the
  945. // sites registered in the nodes it drops are dropped with them, and a region whose
  946. // marks have left its host has nothing to paint.
  947. // The server writes the same marks (compile.hl `markOpen`), the claim walk takes them in
  948. // document order and the build walk makes them, so a region looks the same however it
  949. // came to stand — and text beside a region never runs into the region's own text.
  950. mark(open) { return document.createComment(open ? '[' : ']') }
  951. isMark(n) { return n != null && n.nodeType == 8 && (n.data == '[' || n.data == ']') }
  952. // the nodes strictly between a region's marks
  953. between(start) {
  954. let out = []
  955. let end = start.__hlEnd
  956. let n = start.nextSibling
  957. while (n != null && n != end) { out.push(n) n = n.nextSibling }
  958. return out
  959. }
  960. // …and with the marks themselves: a row as it moves or leaves
  961. spanOf(start) {
  962. let out = [start]
  963. for (n of between(start)) { out.push(n) }
  964. out.push(start.__hlEnd)
  965. return out
  966. }
  967. // a region stands while both its marks are on its host
  968. standing(host, start) {
  969. return start != null && start.parentNode == host && start.__hlEnd != null && start.__hlEnd.parentNode == host
  970. }
  971. // A REGION'S MARK, made (building) or taken (claiming). The claim takes the next mark
  972. // after the host's cursor: the walk meets the regions in document order, so it is this
  973. // region's. A page whose marks do not match its tree is said so, and gets a mark where
  974. // the walk stands.
  975. markAt(host, cr, open) {
  976. if (cr) {
  977. let made = mark(open)
  978. host.appendChild(made)
  979. return made
  980. }
  981. let n = host.__hlCur == null ? host.firstChild : host.__hlCur.nextSibling
  982. while (n != null && !isMark(n)) { n = n.nextSibling }
  983. let mk = null
  984. if (n != null && n.data == (open ? '[' : ']')) { mk = n }
  985. else {
  986. console.error('claim: no region mark under', host.tagName)
  987. mk = mark(open)
  988. host.insertBefore(mk, host.__hlCur == null ? host.firstChild : host.__hlCur.nextSibling)
  989. }
  990. host.__hlCur = mk
  991. return mk
  992. }
  993. // the close mark that pairs with an open one, past the regions inside it
  994. closeOf(start) {
  995. let depth = 0
  996. let n = start.nextSibling
  997. while (n != null) {
  998. if (isMark(n)) {
  999. if (n.data == '[') { depth = depth + 1 }
  1000. else if (depth == 0) { return n }
  1001. else { depth = depth - 1 }
  1002. }
  1003. n = n.nextSibling
  1004. }
  1005. let made = mark(false)
  1006. start.parentNode.appendChild(made)
  1007. return made
  1008. }
  1009. // THE SITES IN NODES THAT LEAVE go with them: an element's, a text leaf's, and a
  1010. // region's whose open mark is among them. Marked and walked up once per site.
  1011. //
  1012. // NO CALL BETWEEN READING THE LIST AND WRITING IT BACK. Every call in this language is
  1013. // awaited, and an await lets another repaint run — one that registers sites of its
  1014. // own, which a list read before it and written after it would lose. The loops that
  1015. // read `m.sites` and write it back hold property reads, index writes and `[] =` only.
  1016. dropSitesIn(&m, host, nodes) {
  1017. if (nodes.length == 0) { return null }
  1018. for (n of nodes) { n.__hlGone = true }
  1019. let kept = []
  1020. let dropped = 0
  1021. for (site of m.sites) {
  1022. let n = (site.region && site.el == host && site.start != null) ? site.start : site.el
  1023. let gone = false
  1024. while (n != null && n != host && !gone) {
  1025. if (n.__hlGone == true) { gone = true }
  1026. n = n.parentNode
  1027. }
  1028. if (gone) { dropped = dropped + 1 } else { kept[] = site }
  1029. }
  1030. if (dropped > 0) {
  1031. m.sites = kept
  1032. dropGen = dropGen + 1
  1033. }
  1034. for (n of nodes) { n.__hlGone = null }
  1035. return null
  1036. }
  1037. // THE SITES A WALK REGISTERED ON THE FRAGMENT IT BUILT INTO belong to the host it lands
  1038. // in — this mount's, and those of the children composed straight onto the fragment,
  1039. // whose own regions (a child's View may begin with an `if`) stand on the same host.
  1040. // Those children are named by address on the fragment as the walk composes them
  1041. // (kidAt, `__hlOnFrag`), so only they are re-entered.
  1042. rehome(&m, src, dst, before) {
  1043. sitesOnto(&m, src, dst, before)
  1044. let due = src.__hlOnFrag == null ? [] : src.__hlOnFrag
  1045. src.__hlOnFrag = null
  1046. let base = m.chain == null ? 0 : m.chain.length
  1047. for (chain of due) {
  1048. if (chain.length > base) { rehomeAt(&m, chain, base, src, dst) }
  1049. }
  1050. return null
  1051. }
  1052. // (`after`: the sites numbered after it are the walk's — an index would move when
  1053. // another repaint drops sites in the meantime)
  1054. sitesOnto(&m, src, dst, after) {
  1055. // sids only grow along m.sites, so the walk's sites are its tail: read it from the end
  1056. // and stop at the first older one (a scan of every site per row built was O(rows x sites))
  1057. let k = m.sites.length - 1
  1058. while (k >= 0 && m.sites[k].sid > after) {
  1059. if (m.sites[k].el == src) { m.sites[k].el = dst }
  1060. k = k - 1
  1061. }
  1062. return null
  1063. }
  1064. rehomeAt(&m, chain, depth, src, dst) {
  1065. if (depth >= chain.length) { sitesOnto(&m, src, dst, 0) return null }
  1066. if (m.kids[chain[depth]] == null) { return null }
  1067. rehomeAt(&m.kids[chain[depth]], chain, depth + 1, src, dst)
  1068. return null
  1069. }
  1070. // A RANGE LEAVES THE PAGE — a branch, a row: every site registered in it goes (at any
  1071. // depth: an element's, a text's, a region's whose marks are in it), and so does every
  1072. // child composed in it (its nodes carry its address), with its own sites, its children
  1073. // and its handlers.
  1074. // A range can hold a FILL — a host's fragment placed at this component's `slot` — whose
  1075. // sites and children are the host's: `fill` names that host (and the one it was filled
  1076. // by in turn), and those go in settle(), where a host is re-entered by its address after
  1077. // every mount on this walk has been written back.
  1078. dropRange(&m, host, nodes, fill) {
  1079. dropSitesIn(&m, host, nodes)
  1080. let others = disposeIn(&m, nodes)
  1081. let f = fill
  1082. while (f != null) {
  1083. pendingDrops.push({ chain = f.chain; rootKind = f.rootKind; host = host; nodes = nodes; kids = others; })
  1084. others = []
  1085. f = f.outer
  1086. }
  1087. return null
  1088. }
  1089. // THE CHILDREN COMPOSED IN NODES go: those of this mount at once, and the others (a
  1090. // host's, composed in its fill) are answered for the caller to hand on
  1091. disposeIn(&m, nodes) {
  1092. let chains = []
  1093. for (n of nodes) { kidsFiledIn(n, &chains) }
  1094. let base = m.chain == null ? [] : m.chain
  1095. let others = []
  1096. for (c of chains) {
  1097. // (a range inside a child never takes the child itself, nor a mount above it: the
  1098. // child's own nodes carry its address, a region of its own among them)
  1099. let above = c.length <= base.length && JSON.stringify(base.slice(0, c.length)) == JSON.stringify(c)
  1100. if (!above) {
  1101. if (c.length > base.length && JSON.stringify(c.slice(0, base.length)) == JSON.stringify(base)) { disposeAt(&m, c, base.length) }
  1102. else { others.push(c) }
  1103. }
  1104. }
  1105. return others
  1106. }
  1107. // the ranges a fill's host still has to let go of (see dropRange)
  1108. dropPending() {
  1109. if (pendingDrops.length == 0) { return null }
  1110. let due = pendingDrops.slice(0)
  1111. pendingDrops = []
  1112. for (d of due) {
  1113. if (d.rootKind == 'shell') {
  1114. if (shell != null) { dropAt(&shell, d, 0) }
  1115. } else if (page != null) { dropAt(&page, d, 0) }
  1116. }
  1117. return null
  1118. }
  1119. dropAt(&m, d, depth) {
  1120. if (depth >= d.chain.length) {
  1121. dropSitesIn(&m, d.host, d.nodes)
  1122. for (c of d.kids) { if (c.length > depth) { disposeAt(&m, c, depth) } }
  1123. return null
  1124. }
  1125. if (m.kids[d.chain[depth]] == null) { return null }
  1126. dropAt(&m.kids[d.chain[depth]], d, depth + 1)
  1127. return null
  1128. }
  1129. // a node in a range whose host's sites are still to go (dropPending)
  1130. dropDue(n) {
  1131. let top = n.getRootNode()
  1132. for (d of pendingDrops) { if (d.nodes.includes(top)) { return true } }
  1133. return false
  1134. }
  1135. // the children whose nodes are this node or stand under it (kidAt files them there)
  1136. kidsFiledIn(n, &out) {
  1137. if (n.__hlKidChains != null) { for (c of n.__hlKidChains) { out.push(c) } }
  1138. if (n.nodeType != 1) { return null }
  1139. let w = document.createTreeWalker(n, 4294967295)
  1140. let c = w.nextNode()
  1141. while (c != null) {
  1142. if (c.__hlKidChains != null) { for (k of c.__hlKidChains) { out.push(k) } }
  1143. c = w.nextNode()
  1144. }
  1145. return null
  1146. }
  1147. disposeAt(&m, chain, depth) {
  1148. if (depth == chain.length - 1) {
  1149. if (m.kids[chain[depth]] != null) {
  1150. purgeMounts(chain, m.rootKind)
  1151. delete m.kids[chain[depth]]
  1152. }
  1153. return null
  1154. }
  1155. if (m.kids[chain[depth]] == null) { return null }
  1156. disposeAt(&m.kids[chain[depth]], chain, depth + 1)
  1157. return null
  1158. }
  1159. // a node no longer on the page: not in the document, not in a fragment a walk is
  1160. // still building, not in the slot a shell's rebuild holds aside
  1161. placedOff(n) {
  1162. if (n.isConnected) { return false }
  1163. if (n.getRootNode().nodeType == 11) { return false }
  1164. if (detachedSlot != null && detachedSlot.contains(n)) { return false }
  1165. return true
  1166. }
  1167. nextSid() {
  1168. sidNext = sidNext + 1
  1169. return sidNext
  1170. }
  1171. isLive(&m, s) {
  1172. for (e of m.sites) { if (e.sid == s.sid) { return true } }
  1173. return false
  1174. }
  1175. liveSids(&m) {
  1176. let out = {}
  1177. for (e of m.sites) { out[e.sid] = true }
  1178. return out
  1179. }
  1180. // A SITE WHOSE NODES LEFT THE PAGE, found by a repaint. Every range that leaves takes
  1181. // its sites with it (dropRange), so this is a bug: the gates' client raises it as an
  1182. // uncaught exception (the page's error event, what every gate fails on), a page in the
  1183. // field says so on the console; neither paints it.
  1184. staleSite(s) {
  1185. let msg = 'hl:web: a repaint found a site whose nodes have left the page (site ' + s.site + ')'
  1186. if (strict) { window.reportError(window.Reflect.construct(window.Error, [msg])) }
  1187. else { console.error(msg) }
  1188. return null
  1189. }
  1190. // A RUN OF TEXT LEAVES BESIDE ELEMENTS OR REGIONS (compile.hl `mixed`): one text node,
  1191. // kept on the element under the run's index so a paint rewrites it and nothing else.
  1192. // `k` null is a text the walk only has to step over (literal, or painted as a site of
  1193. // its own by `textAt`). Built, it is appended. Claimed, it is the text node right
  1194. // after the host's cursor — the last node the walk took there — which the server may
  1195. // have run together with the readable form's line break after it, so it is split off
  1196. // by the value it has now. A run that rendered empty has no node and gets one.
  1197. leafAt(host, cr, i, k, text) {
  1198. let t = leafNode(host, cr, text)
  1199. if (k != null) {
  1200. if (host.__hlLeaves == null) { host.__hlLeaves = {} }
  1201. host.__hlLeaves[k] = t
  1202. }
  1203. return null
  1204. }
  1205. leafNode(host, cr, text) {
  1206. let t = null
  1207. if (cr) {
  1208. t = document.createTextNode(text)
  1209. host.appendChild(t)
  1210. return t
  1211. }
  1212. let at = host.__hlCur == null ? host.firstChild : host.__hlCur.nextSibling
  1213. if (at != null && at.nodeType == 3 && text != '') {
  1214. if (at.data == text) { t = at }
  1215. else if (at.data.startsWith(text)) { at.splitText(text.length) t = at }
  1216. else {
  1217. // a line break of the readable form in front of it (a filled slot's line)
  1218. let lead = at.data.length - at.data.trimStart().length
  1219. if (lead > 0 && at.data.startsWith(View.newline) && at.data.slice(lead).startsWith(text)) {
  1220. t = at.splitText(lead)
  1221. if (t.data != text) { t.splitText(text.length) }
  1222. }
  1223. }
  1224. }
  1225. if (t == null) {
  1226. t = document.createTextNode(text)
  1227. host.insertBefore(t, at)
  1228. }
  1229. host.__hlCur = t
  1230. return t
  1231. }
  1232. // A TEXT LEAF STANDING IN A LIST (an `if` branch, a `for` body): its own text node, and
  1233. // a site of the member it reads — or of the row it stands in — painted by rewriting it.
  1234. textAt(&m, host, cr, readFn, names, rows, rk) {
  1235. let inst = m.instance
  1236. let t = leafNode(host, cr, readFn(&inst, &rows))
  1237. if (names.length > 0 || (rk != null && rk != '')) {
  1238. let sid = nextSid()
  1239. m.sites.push({ sid = sid; el = t; site = null; names = names; region = false; text = true; rows = rows; rowKey = rk; read = readFn; })
  1240. if (t.__hlSid == null) { t.__hlSid = sid } else { t.__hlSidMore = (t.__hlSidMore == null ? '' : t.__hlSidMore) + sid + ',' }
  1241. }
  1242. return null
  1243. }
  1244. paintText(&m, &s) {
  1245. let f = s.read
  1246. let v = f(&m.instance, &s.rows)
  1247. let t = s.el
  1248. if (t.data != v) { t.data = v }
  1249. return null
  1250. }
  1251. // ---- a `for`: the region, its rows, each between its marks -------------------------
  1252. forAt(&m, &o, host, i, site, rowName, listFn, rows, rk, fill, cr, names) {
  1253. let start = markAt(host, cr, true)
  1254. regionAt(&m, host, site, rows, rk, fill, true, rowName, listFn, names, start, null)
  1255. let box = { keys = []; rows = {}; }
  1256. let entries = listFn(&m.instance, &rows)
  1257. let body = walkOf(m.key, 'b', site)
  1258. if (entries != null) {
  1259. let keys = listKeys(&entries)
  1260. let ri = 0
  1261. for (entry of entries) {
  1262. let inner = rows + {}
  1263. inner[rowName] = shareOf(entry)
  1264. let rs = markAt(host, cr, true)
  1265. let out = run(body, &m, &o, host, cr ? 0 : i, inner, rk + '#' + keys[ri], fill, cr, null)
  1266. if (!cr) { i = out }
  1267. rs.__hlEnd = markAt(host, cr, false)
  1268. box.keys.push(keys[ri])
  1269. box.rows[keys[ri]] = { start = rs; entry = entry; }
  1270. ri = ri + 1
  1271. }
  1272. }
  1273. start.__hlEnd = markAt(host, cr, false)
  1274. start.__hlBox = box
  1275. return i
  1276. }
  1277. // an `if`: the branch that stands, between its marks
  1278. ifAt(&m, &o, host, i, site, condFn, rows, rk, fill, cr, names, fnames) {
  1279. let start = markAt(host, cr, true)
  1280. regionAt(&m, host, site, rows, rk, fill, false, null, condFn, names, start, fnames)
  1281. let side = condFn(&m.instance, &rows) ? 't' : 'e'
  1282. start.__hlSide = side
  1283. let out = run(walkOf(m.key, side, site), &m, &o, host, i, rows, rk, fill, cr, null)
  1284. start.__hlEnd = markAt(host, cr, false)
  1285. return out
  1286. }
  1287. // A REGION SITE carries what the patch needs and nothing else: the names its list or
  1288. // its condition reads (the compiler's answer, by this site), the read itself as the
  1289. // generated closure, and its open mark — the region itself, one per row it stands in.
  1290. regionAt(&m, host, site, rows, rowKey, fill, isFor, rowName, readFn, names, start, fnames) {
  1291. // a row-scoped read is its row's, and the language resolved it as one, so the
  1292. // compile step names none here: an empty set is a region no member can move. INSIDE
  1293. // A ROW it is still registered, with no names: the row's new record moves it
  1294. // (reseatRows, ticket #86). Outside one nothing ever can.
  1295. if (names == null) { names = [] }
  1296. if (names.length == 0 && (rowKey == null || rowKey == '')) { return null }
  1297. let held = null
  1298. if (fill != null) { held = fill + {} held.detached = true }
  1299. let sid = nextSid()
  1300. m.sites.push({ sid = sid; el = host; site = site; names = names; region = true; rows = rows; rowKey = rowKey; fill = held; isFor = isFor; row = rowName; read = readFn; start = start; fills = fnames; })
  1301. if (start != null) { if (start.__hlSid == null) { start.__hlSid = sid } else { start.__hlSidMore = (start.__hlSidMore == null ? '' : start.__hlSidMore) + sid + ',' } }
  1302. return null
  1303. }
  1304. // ---- a composed child --------------------------------------------------------------
  1305. // The reference's own description is written at its use site by the compile step: what
  1306. // it binds, what it overwrites, the classes it puts on the child's roots.
  1307. kidAt(&m, host, i, spec, rows, rk, fill, cr, opt) {
  1308. let kk = spec.kid + rk
  1309. if (m.kids[kk] == null) {
  1310. let minted = mintFrom(&m, spec, rows, childChain(m.chain, kk))
  1311. if (minted == null) { return i }
  1312. m.kids[kk] = minted
  1313. }
  1314. bindFrom(&m.kids[kk], &m, spec, rows)
  1315. if (cr) { m.kids[kk].sites = [] }
  1316. m.kids[kk].chain = childChain(m.chain, kk)
  1317. m.kids[kk].rootKind = m.rootKind
  1318. let firstAt = cr ? host.children.length : i
  1319. // WHAT A REFERENCE OVERWRITES REACHES THE FIRST ELEMENT, however deep. A child
  1320. // whose View is a bare reference to another component renders no element of its
  1321. // own, so the events the host overwrote travel on through it — the old hl:web kept that in
  1322. // a field that the first element consumes; here it rides on the option the walk is
  1323. // handed, and a reference with ons of its own replaces it.
  1324. let over = []
  1325. if (spec.ons != null && spec.ons.length > 0) { for (o of spec.ons) { over.push(o.event) } }
  1326. else if (opt != null && opt.over != null) { over = opt.over }
  1327. let kidOpt = { over = over; classes = spec.classes; }
  1328. let f = walkOf(m.kids[kk].key, 'v', null)
  1329. // a child composed straight onto a fragment has its regions on the host the fragment
  1330. // lands in: the fragment names it, for the re-homing (rehome)
  1331. if (cr && host.nodeType == 11) {
  1332. let onFrag = host.__hlOnFrag == null ? [] : host.__hlOnFrag
  1333. onFrag.push(m.kids[kk].chain)
  1334. host.__hlOnFrag = onFrag
  1335. }
  1336. let nodesBefore = host.childNodes.length
  1337. let curBefore = host.__hlCur
  1338. let out = run(f, &m.kids[kk], &m, host, cr ? 0 : i, {}, '', fillFor(&m, spec, rows, rk, fill), cr, kidOpt)
  1339. if (!cr) { i = out }
  1340. // THE NODES THE CHILD PUT ON ITS HOST CARRY ITS ADDRESS: when a range holding them
  1341. // leaves the page, the child goes with it (dropRange)
  1342. let made = []
  1343. if (cr) {
  1344. let k = nodesBefore
  1345. while (k < host.childNodes.length) { made.push(host.childNodes[k]) k = k + 1 }
  1346. } else {
  1347. let n = curBefore == null ? host.firstChild : curBefore.nextSibling
  1348. let stop = host.__hlCur == null ? null : host.__hlCur.nextSibling
  1349. while (n != null && n != stop) { made.push(n) n = n.nextSibling }
  1350. }
  1351. for (n of made) {
  1352. let filed = n.__hlKidChains == null ? [] : n.__hlKidChains
  1353. filed.push(m.kids[kk].chain)
  1354. n.__hlKidChains = filed
  1355. }
  1356. if (cr || m.kids[kk].bindings.length == 0) { m.kids[kk].bindings = [] bindKids(&m.kids[kk], m.kids[kk].view) }
  1357. let roots = rootsBetween(host, firstAt, cr ? host.children.length : i)
  1358. refOnsFrom(&m, spec, roots, rows)
  1359. for (el of roots) { el.__hlKid = true }
  1360. return i
  1361. }
  1362. // the value a binding of this reference carries, off the host instance
  1363. specValue(b, &m, rows) {
  1364. if (b.text != null) { return b.value }
  1365. if (b.member != null) { return view.value({ k = 'member'; name = b.member; }, m.instance, rows) }
  1366. if (b.ref != null) {
  1367. let inst = m.instance
  1368. let f = b.ref
  1369. return f(&inst, &rows)
  1370. }
  1371. return null
  1372. }
  1373. // A CHILD A REGION BUILDS is born with its address: the host's chain and its own key.
  1374. // Every patch, and every late patch a function or a waiting handler of it asks for, is
  1375. // filed and cancelled by that address (tickets #98, #99).
  1376. mintFrom(&m, spec, rows, chain) {
  1377. let bp = blueprintOf(spec.key)
  1378. if (bp == null) { return null }
  1379. let args = {}
  1380. for (b of spec.bindings) { args[b.name] = specValue(b, &m, rows) }
  1381. // A CHILD MINTED LATE takes the current route like one built at boot (#197), by the
  1382. // same `{ path, params, pattern }` the server hands construction
  1383. if (bp.route == true) {
  1384. let cur = currentRoute()
  1385. if (cur != null) { args.route = cur }
  1386. }
  1387. let inst = hlLoad(bp.module, args)
  1388. registerMount(inst, spec.key, chain, m.rootKind)
  1389. return { key = spec.key; instance = inst; view = bp.view; sites = []; isShell = false; kids = {}; bindings = []; chain = chain; rootKind = m.rootKind; }
  1390. }
  1391. // the route the address bar names, as the server hands it to construction: `{ path, params, pattern }`
  1392. currentRoute() {
  1393. let rm = router.match(location.pathname)
  1394. if (rm == null) { return null }
  1395. return { path = rm.path; params = rm.params; pattern = rm.route.pattern; }
  1396. }
  1397. // every mount a navigation did not rebuild that declares `route` (a child minted late, #197)
  1398. // takes the new route, by the sync the levels above took theirs through
  1399. routeSync(&m, &sync, cur) {
  1400. let bp = blueprintOf(m.key)
  1401. if (bp != null && bp.route == true) {
  1402. if (sync[m.key] == null) { sync[m.key] = {} }
  1403. sync[m.key].route = cur
  1404. }
  1405. for (k of m.kids.keys()) { routeSync(&m.kids[k], &sync, cur) }
  1406. return null
  1407. }
  1408. bindFrom(&kid, &m, spec, rows) {
  1409. for (b of spec.bindings) { kid.instance[b.name] = specValue(b, &m, rows) }
  1410. return null
  1411. }
  1412. // A HANDLER WRITTEN ON A REFERENCE is the HOST's: its literal is minted here with the
  1413. // reference's bindings as its own entries, and its write set is filed under the
  1414. // reference's own `on` site in the host's table.
  1415. refOnsFrom(&m, spec, els, rows) {
  1416. if (spec.ons == null || spec.ons.length == 0) { return null }
  1417. let named = {}
  1418. for (b of spec.bindings) { named[b.name] = specValue(b, &m, rows) }
  1419. for (k of rows.keys()) { named[k] = rows[k] }
  1420. let inst = m.instance
  1421. let lit = hlLiteralNew(spec.site, named, &inst)
  1422. for (el of els) {
  1423. for (o of spec.ons) { listenRef(&m, &lit, el, o) }
  1424. }
  1425. return null
  1426. }
  1427. // ---- claim: walk the tree in lockstep with the DOM the same tree produced --------
  1428. // Round one granularity: an element whose children include a member is a SITE for
  1429. // each of those members; a repaint rewrites that element's children from the tree.
  1430. // `i` is the running index into `host.children`; a `for` consumes one run of
  1431. // elements per entry, an `if` the run of the branch that stands.
  1432. claim(&m, host, Number at = 0) {
  1433. let f = walkOf(m.key, 'v', null)
  1434. return run(f, &m, &m, host, at, {}, '', null, false, null)
  1435. }
  1436. // a kid's address: its host's, plus the key the host holds it under
  1437. childChain(chain, kk) {
  1438. let out = chain == null ? [] : chain.slice(0)
  1439. out.push(kk)
  1440. return out
  1441. }
  1442. // the keys a list has, in order: the record's id (view.rowKey), an index where the row
  1443. // is no record, and a suffix where one id stands twice. THE SERVER DOES NOT DEDUPE —
  1444. // its `mountKids` gives two rows of one id one mount key; a list with repeated ids is
  1445. // written down here rather than answered twice.
  1446. listKeys(&entries) {
  1447. let out = []
  1448. let seen = {}
  1449. for (let ri = 0; ri < entries.length; ri = ri + 1) {
  1450. let k = view.rowKey(&entries[ri], ri)
  1451. if (seen[k] != null) { k = k + ':' + ri }
  1452. seen[k] = true
  1453. out.push(k)
  1454. }
  1455. return out
  1456. }
  1457. // ---- the patch: old keys against new ---------------------------------------------
  1458. // A write to the list is answered by a DIFF and nothing else: the rows that are gone
  1459. // are removed, the rows that arrived are built, the rows that stayed are MOVED where
  1460. // the order changed and are left alone where it did not — and each of them is handed
  1461. // the record it now stands for, which rewrites only the attributes and the text the
  1462. // DOM does not already hold. The region's element is never emptied. Each row is the
  1463. // span between its own marks, and every row stands between the list's marks.
  1464. paintFor(&m, &s, lean) {
  1465. let host = s.el
  1466. let start = s.start
  1467. if (!standing(host, start)) {
  1468. // dropped while this paint was on its way (a paint that was running when the
  1469. // range holding it left, and comes back for another round): it is gone, and
  1470. // nothing is painted — a site still REGISTERED here is the bug
  1471. if (!isLive(&m, s)) { return null }
  1472. if (start != null && dropDue(start)) { return null }
  1473. return staleSite(s)
  1474. }
  1475. if (start.__hlBox == null) { return null }
  1476. // ONE PAINT OF A LIST AT A TIME. Building a row can wait (a composed child's module
  1477. // loads on first use), and a second write to the list in that time — a push right
  1478. // behind the handler's own write — read the start.__hlBox before the first paint had filed
  1479. // its row, and built the row again. A paint that arrives while one runs is
  1480. // remembered, and the running one paints again from the list as it then stands.
  1481. if (start.__hlBusy != null) { start.__hlBusy = 'again' return null }
  1482. start.__hlBusy = 'busy'
  1483. // A ROW WITH A COMPOSED CHILD IS STEPPED OVER only when the write moved no member the child is
  1484. // bound to (#181): its bound values (`lit = b3`) are host members, not the record, and a write
  1485. // that moved `L` (derived) may have moved them too — only rebindRow hands them down again
  1486. if (lean == true && rowHasKid(m.key, s.site)) {
  1487. if (paintSet == null) { lean = false }
  1488. else {
  1489. let bound = rowKidReads(m.key, s.site)
  1490. for (n of paintSet) { if (bound.includes(n)) { lean = false } }
  1491. }
  1492. }
  1493. let read = s.read
  1494. let entries = read(&m.instance, &s.rows)
  1495. if (entries == null) { entries = [] }
  1496. // the keys this list has now, in order
  1497. let keys = listKeys(&entries)
  1498. let next = []
  1499. let ri = 0
  1500. while (ri < entries.length) {
  1501. next.push({ key = keys[ri]; i = ri; })
  1502. ri = ri + 1
  1503. }
  1504. // 1. THE ROWS THAT ARE GONE: their nodes, their sites and their children's
  1505. // mounts leave together.
  1506. let keep = {}
  1507. for (r of next) { keep[r.key] = true }
  1508. for (k of start.__hlBox.keys) {
  1509. if (keep[k] == null) { dropRow(&m, &s, &start.__hlBox, k) }
  1510. }
  1511. // 2. THE ROWS IN ORDER. `at` is the node standing where the next row belongs: a
  1512. // row already there advances it, a row that is not is moved or built before it.
  1513. // A DELTA COSTS WHAT CHANGED (#181): each row carries the JSON of the record it was
  1514. // painted from (`sig`), and a row that stands in place with the same record, on a paint
  1515. // the list's own member started (`lean`), is stepped over — no copy of its record, no
  1516. // refresh, no rebind. A row is read by address (`entries[r.i]`), never put in a local.
  1517. let at = start.__hlEnd
  1518. let found = false
  1519. for (k of start.__hlBox.keys) {
  1520. if (!found && keep[k] != null && start.__hlBox.rows[k] != null) { at = start.__hlBox.rows[k].start found = true }
  1521. }
  1522. let placed = []
  1523. let changed = []
  1524. kidMoved = false
  1525. for (r of next) {
  1526. let hstart = start.__hlBox.rows[r.key] == null ? null : start.__hlBox.rows[r.key].start
  1527. let sig = JSON.stringify(entries[r.i])
  1528. if (start.__hlBox.rows[r.key] == null) {
  1529. buildRow(&m, &s, &start.__hlBox, r.key, &entries[r.i], at)
  1530. if (start.__hlBox.rows[r.key] != null) { start.__hlBox.rows[r.key].sig = sig }
  1531. } else if (hstart == at) {
  1532. // already in place: step over it
  1533. at = hstart.__hlEnd.nextSibling
  1534. if (!(lean == true && start.__hlBox.rows[r.key].sig == sig)) {
  1535. if (updateRow(&m, &s, &start.__hlBox, r.key, &entries[r.i], sig)) { changed.push(start.__hlBox.rows[r.key]) }
  1536. }
  1537. } else {
  1538. moveRow(host, &start.__hlBox.rows[r.key], at)
  1539. if (updateRow(&m, &s, &start.__hlBox, r.key, &entries[r.i], sig)) { changed.push(start.__hlBox.rows[r.key]) }
  1540. }
  1541. placed.push(r.key)
  1542. }
  1543. start.__hlBox.keys = placed
  1544. if (changed.length > 0) { reseatRows(&m, &s, changed) }
  1545. let again = start.__hlBusy == 'again'
  1546. start.__hlBusy = null
  1547. if (again) { paintFor(&m, &s, false) }
  1548. return null
  1549. }
  1550. // a row's own elements: the ones between its marks
  1551. rowEls(&held) {
  1552. let out = []
  1553. for (n of between(held.start)) { if (n.nodeType == 1) { out.push(n) } }
  1554. return out
  1555. }
  1556. // A ROW LEAVES: its span goes off the document, the sites that were registered inside
  1557. // it are dropped (they point at nodes nobody can see), and so do the mounts of the
  1558. // children it held — a child of a row is keyed by that row (view.rowKey).
  1559. dropRow(&m, &s, &box, key) {
  1560. let held = box.rows[key]
  1561. if (held == null) { return null }
  1562. let host = s.el
  1563. let nodes = spanOf(held.start)
  1564. dropRange(&m, host, nodes, s.fill)
  1565. for (n of nodes) { n.remove() }
  1566. delete box.rows[key]
  1567. return null
  1568. }
  1569. // A ROW MOVES, marks and all: `moveBefore` where the browser has it, because it keeps
  1570. // an element's state (focus, a playing video, an open dialog) across the move;
  1571. // `insertBefore` else.
  1572. moveRow(host, held, at) {
  1573. for (n of spanOf(held.start)) {
  1574. if (host.moveBefore != null) { host.moveBefore(n, at) }
  1575. else { host.insertBefore(n, at) }
  1576. }
  1577. return null
  1578. }
  1579. // A ROW ARRIVES: built into a fragment between its marks and put in with one insertion,
  1580. // because the walk appends and the row's place is where `at` stands — which may be in
  1581. // the middle of the region. What the walk registered on the fragment belongs to the host.
  1582. buildRow(&m, &s, &box, key, &entry, at) {
  1583. let host = s.el
  1584. let inner = s.rows + {}
  1585. inner[s.row] = shareOf(entry)
  1586. let bin = document.createDocumentFragment()
  1587. let body = walkOf(m.key, 'b', s.site)
  1588. let before = sidNext
  1589. let rs = markAt(bin, true, true)
  1590. run(body, &m, &m, bin, 0, inner, (s.rowKey == null ? '' : s.rowKey) + '#' + key, s.fill, true, null)
  1591. rs.__hlEnd = markAt(bin, true, false)
  1592. rehome(&m, bin, host, before)
  1593. // THE LIST LEFT WHILE THE ROW WAS BEING BUILT (a module to load is a wait, and a
  1594. // branch around the list can be replaced in it): the row never stands, and what
  1595. // its walk registered goes with it
  1596. if (!standing(host, s.start)) {
  1597. dropRange(&m, host, Array.from(bin.childNodes), s.fill)
  1598. return null
  1599. }
  1600. host.insertBefore(bin, at)
  1601. box.rows[key] = { start = rs; entry = entry; }
  1602. return null
  1603. }
  1604. // A ROW STAYS, AND THE RECORD IT SHOWS MAY BE ANOTHER ONE — `upsert` hands the row a
  1605. // new record under the same id, and `items[0].title = 'edited'` writes into the one it
  1606. // already has. Both are answered here: the row's elements are handed the record (their
  1607. // `__hlRows`, which is also what a handler on the row reads), and every attribute and
  1608. // text leaf is rewritten ONLY where the DOM does not already hold the value. That is
  1609. // why a push into 2000 rows costs no mutation on the 2000 that did not change.
  1610. updateRow(&m, &s, &box, key, &entry, sig) {
  1611. if (box.rows[key] == null) { return false }
  1612. // the row holds its own copy of the record, so the two are compared by what they say
  1613. // (read and written in place: a row held in a local, and put back, is a copy of its record twice per delta — #181)
  1614. let changed = box.rows[key].sig != sig
  1615. box.rows[key].entry = entry
  1616. box.rows[key].sig = sig
  1617. // ONE shared handle to the record for every element and site of the row (see shareOf): an element or site that took
  1618. // its own copy would be a copy per element, N^2 for a long list inside the row (#198)
  1619. let share = shareOf(entry)
  1620. box.rows[key].share = share
  1621. for (el of rowEls(&box.rows[key])) { refreshRow(&m, el, s.row, &share) }
  1622. rebindRow(&m, &s, key, &entry)
  1623. return changed
  1624. }
  1625. // A ROW THAT NOW SHOWS ANOTHER RECORD HANDS IT TO THE SITES INSIDE IT (ticket #86). A
  1626. // row without an id is keyed by its place, so a new list of the same shape keeps every
  1627. // row — and refreshRow repaints only the row's own elements. The sites registered in
  1628. // the row still hold the rows of the moment they were built: a nested `for` read the
  1629. // old record's list, and a `td` inside it the old inner record. Each site whose element
  1630. // stands in a changed row takes the new record, and the regions among them are patched
  1631. // again, outermost first — a nested row that changed in turn does the same for its own.
  1632. // One pass over the sites, marked rows and an ancestor walk, not a scan per row.
  1633. reseatRows(&m, &s, changed) {
  1634. let host = s.el
  1635. // (#200) a child of these rows took a new bound value: the row's `if`s with a child in their standing branch rebuild
  1636. let kidsMoved = kidMoved
  1637. // every node of a changed row carries its record for the walk up — its text and
  1638. // its regions' marks as well as its elements, so a text leaf or an `if` standing
  1639. // in the row directly on the host takes the record too
  1640. // (the record rides as the row's ONE shared handle, made by updateRow: a site that took a copy of it would be a
  1641. // copy per site, sites x record size — N^2 for a long list inside the row: #198)
  1642. for (held of changed) {
  1643. for (n of between(held.start)) { n.__hlReseat = held.share }
  1644. }
  1645. // (#71) THE SITES OF THE CHANGED ROWS ARE FOUND FROM THEIR NODES, not by walking every site of the mount up to the
  1646. // host: an append of one row to a chat of 150 rows scanned and ancestor-walked every site of every row. The nodes of a
  1647. // changed row carry their sites' numbers (`__hlSids`); the numbers grow along m.sites, so each is found by bisection.
  1648. // (sites are read and written in place, by index: a site held in a local and put back is a copy of its
  1649. // rows — every record of every enclosing row — twice per site, and a row with a long list inside has a site per item: #198)
  1650. // (the record is handed to the site while the rows are still marked; the regions are repainted in the document order the
  1651. // walk met them in: an outer region's mark comes before the ones inside it)
  1652. reseatGen = reseatGen + 1
  1653. let gen = reseatGen
  1654. let regs = []
  1655. let txts = []
  1656. let last = -1
  1657. for (held of changed) {
  1658. for (top of between(held.start)) {
  1659. // every node under the row's top node, by hand: a call per node is what a reseat of a long row cost (#198)
  1660. let c = top
  1661. while (c != null) {
  1662. // the record this node stands under: its own mark, else its parent's (the walk reaches a parent first)
  1663. c.__hlHit = c.__hlReseat != null ? c : c.parentNode.__hlHit
  1664. if (c.__hlSid != null && c.__hlSeen != gen) {
  1665. c.__hlSeen = gen
  1666. let hit = c.__hlHit
  1667. if (hit != null) {
  1668. let wants = [c.__hlSid]
  1669. if (c.__hlSidMore != null) { for (id of c.__hlSidMore.split(',')) { if (id != '') { wants.push(JSON.parse(id)) } } }
  1670. for (want of wants) {
  1671. // the sites of one row were registered one after the other: the one after the last found is tried first, else bisected
  1672. let k = -1
  1673. if (last >= -1 && last + 1 < m.sites.length && m.sites[last + 1].sid == want) { k = last + 1 }
  1674. else {
  1675. let lo = 0
  1676. let hi = m.sites.length - 1
  1677. while (lo <= hi) {
  1678. let sum = lo + hi
  1679. let mid = (sum - (sum % 2)) / 2
  1680. let sid = m.sites[mid].sid
  1681. if (sid == want) { k = mid lo = hi + 1 } else if (sid < want) { lo = mid + 1 } else { hi = mid - 1 }
  1682. }
  1683. }
  1684. if (k >= 0) { last = k }
  1685. if (k >= 0 && m.sites[k].el != null && m.sites[k].rows != null && !(m.sites[k].region && m.sites[k].start == s.start)) {
  1686. m.sites[k].rows[s.row] = hit.__hlReseat
  1687. if (m.sites[k].region) { regs.push(want) } else if (m.sites[k].text == true) { txts.push(want) }
  1688. }
  1689. }
  1690. }
  1691. }
  1692. if (c.firstChild != null) { c = c.firstChild }
  1693. else {
  1694. while (c != top && c.nextSibling == null) { c = c.parentNode }
  1695. c = c == top ? null : c.nextSibling
  1696. }
  1697. }
  1698. }
  1699. }
  1700. for (held of changed) { for (n of between(held.start)) { n.__hlReseat = null } }
  1701. for (t of txts) {
  1702. let at = sitePos(&m, t, -1)
  1703. if (at >= 0) { paintText(&m, &m.sites[at]) }
  1704. }
  1705. for (r of regs) {
  1706. // read again from the mount: a region an outer one already patched away is gone
  1707. let at = sitePos(&m, r, -1)
  1708. if (at >= 0) {
  1709. if (m.sites[at].isFor) { paintFor(&m, &m.sites[at], false) } else { rebuildIf(&m, &m.sites[at], kidsMoved && ifBranchHasKid(m.key, m.sites[at].site, m.sites[at].start.__hlSide)) }
  1710. }
  1711. }
  1712. return null
  1713. }
  1714. // the index of the site numbered `want` in m.sites, or -1: sids only grow along m.sites, so it is bisected (#71)
  1715. sitePos(&m, want, hint) {
  1716. // the sites of one row were registered one after the other: the one after the last found is tried first
  1717. if (hint >= 0 && hint < m.sites.length && m.sites[hint].sid == want) { return hint }
  1718. let lo = 0
  1719. let hi = m.sites.length - 1
  1720. while (lo <= hi) {
  1721. let sum = lo + hi
  1722. let mid = (sum - (sum % 2)) / 2
  1723. let sid = m.sites[mid].sid
  1724. if (sid == want) { return mid }
  1725. if (sid < want) { lo = mid + 1 } else { hi = mid - 1 }
  1726. }
  1727. return -1
  1728. }
  1729. // ONE HANDLE TO A RECORD: hlShared (core, #198/mission 383) gives a read-only shared handle the language's copy rules
  1730. // pass as is, so storing it in the `rows` of a hundred sites copies nothing; the sites read it in place, a write through
  1731. // it is a located 'read-only shared value' error. Scalars come back as themselves; on native it is the identity.
  1732. shareOf(entry) {
  1733. return hlShared(entry)
  1734. }
  1735. // A ROW'S CHILDREN TAKE THEIR BINDINGS AGAIN. A reference inside a `for` body is not
  1736. // in `m.bindings` — that list is built from the View's own walk, which does not enter a
  1737. // `for` (the row is the scope, and there is one child per row) — so a host member that
  1738. // feeds a row's child used to reach it only because the region REBUILT and the child
  1739. // was minted again with the value of the moment (the framework gate's own case:
  1740. // home.hl writes `noteOpen`, every row's RowNote takes it and hands it to RowMark, and
  1741. // the grandchild's region appears). Nothing rebuilds any more, so the edge is walked
  1742. // here — and only what MOVED is written and repainted, or every list write would
  1743. // repaint every row's child.
  1744. rebindRow(&m, &s, key, &entry) {
  1745. // a row body with no composed child has nothing to take again: decided once per body
  1746. let bk = m.key + '|' + s.site
  1747. if (rowKidless[bk] == true) { return null }
  1748. if (rowKidless[bk] == null) { rowKidless[bk] = !rowHasKid(m.key, s.site) }
  1749. if (rowKidless[bk] == true) { return null }
  1750. let rk = (s.rowKey == null ? '' : s.rowKey) + '#' + key
  1751. let inner = s.rows + {}
  1752. inner[s.row] = entry
  1753. // (the seed's tree is read in place: a copy of it, or of the instance, per row per delta
  1754. // is what froze a streaming chat — #181)
  1755. if (blueprints[m.key] != null) { rebindIn(&m, &blueprints[m.key].view, s.site, &inner, rk) }
  1756. return null
  1757. }
  1758. // the host members the composed children of the `for` at `site` are bound to (read in place, once per body)
  1759. rowKidReads(key, site) {
  1760. let bk = key + '|' + site
  1761. if (rowKidBound[bk] != null) { return rowKidBound[bk] }
  1762. let out = []
  1763. if (blueprints[key] != null) { kidReadsIn(&blueprints[key].view, site, out) }
  1764. rowKidBound[bk] = out
  1765. return out
  1766. }
  1767. kidReadsIn(&nodes, site, &out) {
  1768. for (let i = 0; i < nodes.length; i = i + 1) {
  1769. if (nodes[i].k == 'for' && nodes[i].site == site) { bodyReads(&nodes[i].body, out) }
  1770. else if (nodes[i].k == 'el') { kidReadsIn(&nodes[i].children, site, out) }
  1771. else if (nodes[i].k == 'if') { kidReadsIn(&nodes[i].then, site, out) kidReadsIn(&nodes[i].other, site, out) }
  1772. else if (nodes[i].k == 'for') { kidReadsIn(&nodes[i].body, site, out) }
  1773. else if (nodes[i].k == 'component' && nodes[i].fill != null) { kidReadsIn(&nodes[i].fill, site, out) }
  1774. }
  1775. return null
  1776. }
  1777. // every name a composed child's binding reads, anywhere in these nodes
  1778. bodyReads(&nodes, &out) {
  1779. for (let i = 0; i < nodes.length; i = i + 1) {
  1780. if (nodes[i].k == 'component') {
  1781. for (let j = 0; j < nodes[i].bindings.length; j = j + 1) {
  1782. if (nodes[i].bindings[j].member != null) { out.push(nodes[i].bindings[j].member) }
  1783. else if (nodes[i].bindings[j].ref != null) { exprReads(&nodes[i].bindings[j].ref, out) }
  1784. }
  1785. if (nodes[i].fill != null) { bodyReads(&nodes[i].fill, out) }
  1786. } else if (nodes[i].k == 'el') { bodyReads(&nodes[i].children, out) }
  1787. else if (nodes[i].k == 'if') { bodyReads(&nodes[i].then, out) bodyReads(&nodes[i].other, out) }
  1788. else if (nodes[i].k == 'for') { bodyReads(&nodes[i].body, out) }
  1789. }
  1790. return null
  1791. }
  1792. exprReads(&x, &out) {
  1793. if (x == null) { return null }
  1794. if (x.k == 'member' || x.k == 'field') { out.push(x.name) return null }
  1795. if (x.k == 'expr') { exprReads(&x.x, out) return null }
  1796. if (x.k == 'bin') { exprReads(&x.l, out) exprReads(&x.r, out) return null }
  1797. if (x.k == 'un') { exprReads(&x.x, out) return null }
  1798. if (x.k == 'cond') { exprReads(&x.c, out) exprReads(&x.a, out) exprReads(&x.b, out) return null }
  1799. return null
  1800. }
  1801. // whether the body of the `for` at `site` holds a composed child — read in place, nothing copied
  1802. rowHasKid(key, site) {
  1803. if (blueprints[key] == null) { return false }
  1804. return kidIn(&blueprints[key].view, site, false)
  1805. }
  1806. // walks the tree by address to the `for` at `site`; asks (find false) or re-binds (find true) its body
  1807. kidIn(&nodes, site, inside) {
  1808. for (let i = 0; i < nodes.length; i = i + 1) {
  1809. if (nodes[i].k == 'for' && nodes[i].site == site) { if (bodyHasKid(&nodes[i].body)) { return true } }
  1810. else if (nodes[i].k == 'el') { if (kidIn(&nodes[i].children, site, inside)) { return true } }
  1811. else if (nodes[i].k == 'if') { if (kidIn(&nodes[i].then, site, inside) || kidIn(&nodes[i].other, site, inside)) { return true } }
  1812. else if (nodes[i].k == 'for') { if (kidIn(&nodes[i].body, site, inside)) { return true } }
  1813. else if (nodes[i].k == 'component' && nodes[i].fill != null) { if (kidIn(&nodes[i].fill, site, inside)) { return true } }
  1814. }
  1815. return false
  1816. }
  1817. // the row body of the `for` at `site`, bound again — found and walked by address (rebindNodes)
  1818. rebindIn(&m, &nodes, site, &rows, rk) {
  1819. for (let i = 0; i < nodes.length; i = i + 1) {
  1820. if (nodes[i].k == 'for' && nodes[i].site == site) { rebindNodes(&m, &nodes[i].body, &rows, rk) return true }
  1821. let got = false
  1822. if (nodes[i].k == 'el') { got = rebindIn(&m, &nodes[i].children, site, &rows, rk) }
  1823. else if (nodes[i].k == 'if') { got = rebindIn(&m, &nodes[i].then, site, &rows, rk) || rebindIn(&m, &nodes[i].other, site, &rows, rk) }
  1824. else if (nodes[i].k == 'for') { got = rebindIn(&m, &nodes[i].body, site, &rows, rk) }
  1825. else if (nodes[i].k == 'component' && nodes[i].fill != null) { got = rebindIn(&m, &nodes[i].fill, site, &rows, rk) }
  1826. if (got) { return true }
  1827. }
  1828. return false
  1829. }
  1830. bodyHasKid(&nodes) {
  1831. for (let i = 0; i < nodes.length; i = i + 1) {
  1832. if (nodes[i].k == 'component') { return true }
  1833. if (nodes[i].k == 'el' && bodyHasKid(&nodes[i].children)) { return true }
  1834. if (nodes[i].k == 'if' && (bodyHasKid(&nodes[i].then) || bodyHasKid(&nodes[i].other))) { return true }
  1835. if (nodes[i].k == 'for' && bodyHasKid(&nodes[i].body)) { return true }
  1836. }
  1837. return false
  1838. }
  1839. // whether the branch of the `if` at `site` that stands (side 't' / 'e') holds a composed child — by address, nothing copied
  1840. ifBranchHasKid(key, site, side) {
  1841. if (blueprints[key] == null) { return false }
  1842. return ifKidIn(&blueprints[key].view, site, side)
  1843. }
  1844. ifKidIn(&nodes, site, side) {
  1845. for (let i = 0; i < nodes.length; i = i + 1) {
  1846. if (nodes[i].k == 'if' && nodes[i].site == site) { return side == 't' ? bodyHasKid(&nodes[i].then) : bodyHasKid(&nodes[i].other) }
  1847. let got = false
  1848. if (nodes[i].k == 'el') { got = ifKidIn(&nodes[i].children, site, side) }
  1849. else if (nodes[i].k == 'if') { got = ifKidIn(&nodes[i].then, site, side) || ifKidIn(&nodes[i].other, site, side) }
  1850. else if (nodes[i].k == 'for') { got = ifKidIn(&nodes[i].body, site, side) }
  1851. else if (nodes[i].k == 'component' && nodes[i].fill != null) { got = ifKidIn(&nodes[i].fill, site, side) }
  1852. if (got) { return true }
  1853. }
  1854. return false
  1855. }
  1856. // (#200) whether every member that moved holds a plain value (text, number, flag): a child handed one may draw it once
  1857. // (MdEditor's mount hook), so its branch is composed anew; a list or record is drawn by the child's own `for`s and sites,
  1858. // which repaintAll patches — rebuilding them is the cost #198 removed

Only the first lines are shown.

Branches

Latest commits

  • 043624b2theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 05d89fd0deploy: security gate (tests/security.mjs)mre
  • 2ae8a47bMerge branch 'sk-sec'mre
  • 8fee5d52sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • 944ad51eSK: report reports/SK-sec.md, LOGsk
  • 415487c1SK: import budget per user + id type check, gate checkssk
  • 7b8a4f74security audit SK: nosniff + image sandbox headers, face id checks, adult follow, login budget + new session at login, guest-link budget, tests/security.mjssk
  • f0f26c5areports: T18 section (gates, boot-gate, 3x3 table, search + short-id compare)mre
  • 2a8682a1deploy.sh GATES: tests/warmstart.mjsmre
  • 3761338breports/T17-boot.md: handoff (phase table, fix, numbers so far, open proof)mre
  • 52e09eb2boot: sync window 1000 ms (the gates ask for a complete start), README boot-time sectionmre
  • c87149e8tests/warmstart.mjs: the progressive start proven over HTTP (note in the window, 503 short id, after: everything); bootBeginmre
  • 593a6350boot: resumable start-up scan, the server listens first (T17)mre
  • be4890a0reports: H2 hydration audit (no test change needed)mre
  • 1f9c8bf4re-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • c1df88eddocker-compose: HL_GC_TABLE_LOAD=62 (tracker mission 048, T16 real-data numbers)mre
  • d0a82867mission 048: report, LOG (HL_GC_TABLE_LOAD=62 measured)mre
  • 2d4be66fre-vendor Hybriel 5df4eb7e (GC: HL_GC_TABLE_LOAD knob, mission 406)mre
  • 99e7c8b9tests: realdata-m035-radii, repro-t38, realdata-060-backfill ports from env (REAL_PORT, REAL_CHROME, REPRO_CHROME), defaults unchangedmre
  • 4625aa22re-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre