gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit69141f6569141f65|||PR pulls page: cache the git part per repo by branch tipsmre69141f65/plugins/web/client.hl

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

Only the first lines are shown.

Branches

Latest commits

  • 69141f65|||PR pulls page: cache the git part per repo by branch tipsmre
  • 155861f6Merge branch 'w52-person-approval' into mainGitoria
  • b5435de8|||PR person approvals: second Kunibert round (#52)mre
  • 3a8b2fb8|||PR person approvals: Kunibert review fixes (#52)mre
  • 2a89d8d0|||PR person approvals: Approve button for people; merge needs the required checks or a listed person's approval (#52)mre
  • 83f0c953Report person approval verificationmre
  • 3ae94b86Record browser failures and verify owned tab cleanupmre
  • 54eec687Gate person approvals and merge alternativesmre
  • 12b08829Add person approvals for pull request headsmre
  • 55c84779Merge branch 'gp-gate-ports' into mainGitoria
  • 57af42bbREADME: point to deploy.sh for the port listmre
  • d63375d2README: gate commands with explicit portsmre
  • feeaa701|||PR gates: explicit ports only, deploy.sh passes themmre
  • 6cc1f943Merge branch 'records-1112' into mainGitoria
  • 9448c438|||PR records: LOG, missions and reports since 11-09mre
  • 6dc08a8eMerge branch 'p51-public-pr-reads' into mainGitoria
  • 7b88cfe0Type publicReaderOfRequest parameters (L10)Gitoria
  • 2a4b3dc1|||PR Anonymous reads of public repos' pulls and statusesmre
  • 21b2c141Merge branch 's25-no-session-on-login-error' into mainGitoria
  • 698458e2|||PR No session for cookieless login errors: READMEmre