gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit33ad4bd533ad4bd5Merge commit 'f57f64f'mre33ad4bd5/plugins/web/client.hl

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

Only the first lines are shown.

Branches

Latest commits

  • 33ad4bd5Merge commit 'f57f64f'mre
  • 7d38cec4docker-compose: nofile 65536 (default soft 1024 refused boot: errno 24 in fs.listDir, Hybriel 528bc89f)mre
  • f57f64f0mission 046: report, LOGmre
  • 5965963adeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
  • efe1b485mission 046: re-vendor Hybriel master 60b2817b (integration 8, #203): plugins mpackdb + web (bin/ git-ignored: hybriel, libhlrt.a, hlc.o, hlrt/ from native/zig-out)mre
  • 53099ffbmission 046: person page reads a slim per-title credit row cache (no whole-record fetch per credit); gate tests/person-lookup.mjsmre
  • 9b3828aemission 046a: report, LOGmre
  • 4b37356emission 046: franchise seed supersedeCollections reads the curated timelines once (10 s one-tick block at the end of boot)mre
  • ac6268e3mission 046a: re-vendor Hybriel 528bc89f (integration 6): plugins mpackdb/time/web (bin/ git-ignored)mre
  • 322b3293mission 046: photoQueue guard for non-row fetch results (damaged persons index)mre
  • e5b99aecmission 046: photoQueue in slices (people.hl photoQueueStart/Step, jobs.hl photoTick), tests/photoqueue.hlmre
  • 31dcf2admission 045: report, LOGmre
  • 9c3971d0tests/persons-skip-count.hlmre
  • 4d72e365mission 045mre
  • b9cae4c9slices: skip and count non-row values from range(); merge persons pass falls back to iterate; guard testmre
  • 3db7dc31merge w44-jobs-slices into w43-trkmemmre
  • 593dab6fmission 043: report, LOGmre
  • 1a5a4818boot scans back to iterate (slices measured slower: titles 114 s, episodes 141 s, persons failed); lambda per rowmre
  • 03d29893boot scans read primary-key slices (lib/slices.hl from mission 044): iterate is eager too; people view log line back; allShows/allEpisodes stay for toolsmre
  • 23375c58mission 044: report, LOG, STATUSmre