gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit5b0b4b9e5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre5b0b4b9e/plugins/web/client.hl

210.7 KB

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

Only the first lines are shown.

Branches

Latest commits

  • 5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre
  • bc8f20d9gitoria#25: re-vendor Hybriel c09538bb with the W141 render cache, cache the homepage per commit, add the readme-cache gatemre
  • 9feebbd3Re-vendor Hybriel to ded47c13 (W145: Firefox start script fix)mre
  • 28ed41e5gitoria#24: one sign-in comes from hl:web batch 20, the /session/share workaround is gonemre
  • 3b2669e4gs: drop the unbuildable legacy check, fix pwa fetchmre
  • 2a2fdd77gs: C2 check before cookie experimentsmre
  • 76e0587aWIP gs 2mre
  • a80da7bdWIP gs: security gate for lazy sessions, g24 browser checksmre
  • 23fb05d8re-vendor Hybriel aaa39e69 (hl:web batch 20: login emit and Sessions.mint, tab follows a login, signed-in session cookie wins among duplicates and is re-issued with the Domain, W85 faster render, lazy sessions)mre
  • a0727ca3ident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
  • a06975f9gitoria: a gap between your name and Log out in the phone header (they touched); gate checks itmre
  • 32ba9d59gitoria: leave the host-only cookie experiment out of the browser gate (gitoria#24 is a separate mission)mre
  • 43ad2bcfgitoria header: the selector alone plus your name and Log out as before the theme missions; tests follow (WIP: host-only cookie experiment fails)mre
  • ac2c4e3fident selector back in the header (creator 2026-10-24): selector top right signed in and out on phone and desktop, no second Log in button, new gate checks (WIP: gates not re-run after the last edits)mre
  • b279f536theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 2b01515ctests/ticketskit: a failed start kills the tickets server (own process group); stop() kills the groupmre
  • 109cfc5ctests/ticketskit: keep the rotated session cookie of the tickets login (tickets ST2 session rotation)mre
  • 7eb6be27sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • b3911918reports/MG-mobile.md: 404 fixed, rebased gatesMG
  • 9da65b42tests/browser.mjs: 404 checks (host, no hydration wait)MG