The best way to control Claude Code, Codex and more from any computer or phone

main
astro 1,528 lines 43.3 KB
Raw
1 ---
2 /**
3 * `<StubWalkthrough />` — how distilled's lazy service stubs load an
4 * operation. Left: what is in memory, starting from the stub. Right: the
5 * full module on disk, scrolled to whatever the loader reads. Byte counts
6 * come from a traced run of fly-io's `machines` service on Node 24
7 * (distilled 6ff867b87). The code is simplified.
8 *
9 * One instance per page: element ids are prefixed `lz-`.
10 */
11 ---
12
13 <figure class="lz not-content" id="lz" tabindex="0" aria-label="Step-through of a lazy service stub loading fly-io's machines service">
14 <div class="lz-narr" id="lz-narr" aria-live="polite"></div>
15 <div class="lz-grid" id="lz-grid">
16 <section class="lz-pane">
17 <div class="lz-hd"><b>Your program</b></div>
18 <div class="lz-view lz-progview"><div class="lz-code" id="lz-prog"></div></div>
19 <div class="lz-status" id="lz-status"></div>
20 <div class="lz-hd"><b>In memory</b> · what it has loaded</div>
21 <div class="lz-view lz-memview"><div class="lz-code" id="lz-left"></div></div>
22 </section>
23 <div class="lz-meters" aria-hidden="true">
24 <div class="lz-meter">
25 <div class="lz-track" id="lz-memtrack"><div class="lz-fill lz-mem" id="lz-memfill"></div></div>
26 <span class="lz-mlabel">in memory</span>
27 <span class="lz-mval" id="lz-memval">0 B</span>
28 </div>
29 <div class="lz-meter">
30 <div class="lz-track" id="lz-disktrack"><div class="lz-fill lz-disk"></div></div>
31 <span class="lz-mlabel">on disk</span>
32 <span class="lz-mval">63.2<br />KB</span>
33 </div>
34 </div>
35 <section class="lz-pane">
36 <div class="lz-hd"><b>On disk</b> · the full module, by byte offset</div>
37 <div class="lz-view lz-file" id="lz-fileview"><div class="lz-code" id="lz-right"></div></div>
38 </section>
39 </div>
40 <div class="lz-bar">
41 <button type="button" id="lz-prev">◀ Back</button>
42 <div class="lz-dots" id="lz-dots"></div>
43 <label class="lz-truth" title="Show the stub, the slices and the index exactly as they ship, minified">
44 <input type="checkbox" id="lz-real" />
45 <span><b>I can handle the truth</b></span>
46 </label>
47 <button type="button" class="lz-primary" id="lz-next">Next ▶</button>
48 </div>
49 </figure>
50
51 <style is:global>
52 .lz {
53 --lz-op: var(--alc-info);
54 --lz-sh: var(--alc-terracotta);
55 --lz-ix: var(--alc-warn);
56 --lz-st: var(--alc-accent);
57 --lz-fg: var(--alc-fg-1);
58 --lz-mut: var(--alc-fg-4);
59 --lz-bg: var(--alc-bg-elev-1);
60 --lz-panel: var(--alc-bg-elev-2);
61 --lz-bd: var(--alc-hairline-2);
62 --lz-bd2: var(--alc-hairline-3);
63 --lz-lh: 17px;
64 position: relative;
65 margin: 2rem 0;
66 overflow: hidden;
67 font: 14px/1.5 var(--alc-font-sans);
68 color: var(--lz-fg);
69 background: var(--lz-bg);
70 border: 1px solid var(--lz-bd);
71 border-radius: 12px;
72 }
73 .lz:focus-visible {
74 outline: 2px solid var(--alc-accent);
75 outline-offset: 2px;
76 }
77 .lz * {
78 box-sizing: border-box;
79 }
80 .lz code {
81 font-family: var(--alc-font-mono);
82 font-size: 0.9em;
83 }
84 .lz-hd {
85 margin-bottom: 5px;
86 font-size: 11.5px;
87 color: var(--lz-mut);
88 white-space: nowrap;
89 overflow: hidden;
90 text-overflow: ellipsis;
91 }
92 .lz-hd b {
93 color: var(--lz-fg);
94 }
95
96 .lz-narr {
97 min-height: 5.4rem;
98 padding: 13px 16px 10px;
99 background: var(--lz-panel);
100 border-bottom: 1px solid var(--lz-bd);
101 }
102 .lz-step {
103 font: 600 11px var(--alc-font-mono);
104 letter-spacing: 0.06em;
105 text-transform: uppercase;
106 color: var(--lz-mut);
107 }
108 .lz-ttl {
109 display: block;
110 margin: 1px 0 3px;
111 font-size: 16px;
112 font-weight: 700;
113 }
114 .lz-narr p {
115 margin: 0;
116 color: var(--alc-fg-2);
117 }
118 .lz-in {
119 animation: lzin 0.35s ease both;
120 }
121 @keyframes lzin {
122 from {
123 opacity: 0;
124 transform: translateY(3px);
125 }
126 }
127
128
129 /* code lines */
130 .lz-code {
131 font: 11px/var(--lz-lh) var(--alc-font-mono);
132 font-variant-ligatures: none;
133 }
134 .lz-ln {
135 display: flex;
136 height: var(--lz-lh);
137 padding-right: 6px;
138 white-space: pre;
139 overflow: hidden;
140 color: var(--lz-mut);
141 border-left: 3px solid transparent;
142 transition:
143 background 0.4s,
144 color 0.4s,
145 border-color 0.4s,
146 opacity 0.4s;
147 }
148 .lz-bytes .lz-ln .no {
149 width: 3.6em;
150 font-weight: 600;
151 opacity: 0.85;
152 }
153 .lz-ln .no.hit,
154 .lz mark.lz-off {
155 padding: 0 2px;
156 color: var(--lz-bg);
157 background: var(--lz-ix);
158 border-radius: 2px;
159 opacity: 1;
160 }
161 .lz-ln .no {
162 flex: none;
163 width: 2.1em;
164 padding-right: 0.5em;
165 text-align: right;
166 opacity: 0.5;
167 }
168 .lz-ln .tx {
169 padding-left: 5px;
170 }
171 .lz-ln.c {
172 font-style: italic;
173 }
174 .lz-ln .tag {
175 margin-left: auto;
176 padding: 0 8px;
177 font-weight: 600;
178 font-style: normal;
179 color: var(--alc-accent-text);
180 }
181 .lz-ln .tag.wait {
182 color: var(--lz-ix);
183 }
184 .lz-ln.phase {
185 color: var(--lz-fg);
186 font-weight: 600;
187 }
188 .lz-ln.now {
189 color: var(--lz-fg);
190 background: color-mix(in srgb, var(--alc-accent) 18%, transparent);
191 border-left-color: var(--alc-accent);
192 }
193 .lz-ln.read {
194 color: var(--lz-fg);
195 background: color-mix(in srgb, var(--c) 28%, transparent);
196 border-left-color: var(--c);
197 }
198 .lz-ln.mem {
199 color: var(--lz-fg);
200 background: color-mix(in srgb, var(--c) 9%, transparent);
201 border-left-color: color-mix(in srgb, var(--c) 55%, transparent);
202 }
203 .lz-ln.never {
204 opacity: 0.4;
205 }
206 .lz-ln.pending {
207 opacity: 0;
208 transition: none;
209 }
210 .lz-slice {
211 position: absolute;
212 z-index: 1;
213 left: -2px;
214 right: -2px;
215 min-height: 3px;
216 background: var(--c);
217 border-radius: 1px;
218 opacity: 0.55;
219 transition: opacity 0.4s;
220 }
221 .lz-slice.hot {
222 opacity: 1;
223 box-shadow: 0 0 0 2px color-mix(in srgb, var(--c) 45%, transparent);
224 }
225 .lz-segfly {
226 position: absolute;
227 z-index: 4;
228 top: 0;
229 left: 0;
230 background: var(--c);
231 border-radius: 2px;
232 box-shadow: 0 0 0 2px color-mix(in srgb, var(--c) 40%, transparent);
233 pointer-events: none;
234 }
235 .lz-ln.fresh {
236 animation: lzfresh 1.2s ease both;
237 }
238 @keyframes lzfresh {
239 from {
240 background: color-mix(in srgb, var(--c) 45%, transparent);
241 }
242 }
243 .lz-ln.look {
244 color: var(--lz-fg);
245 background: color-mix(in srgb, var(--lz-ix) 18%, transparent);
246 border-left-color: var(--lz-ix);
247 }
248 .lz mark.lz-tok {
249 padding: 0 1px;
250 font-weight: 700;
251 color: var(--lz-bg);
252 background: var(--lz-ix);
253 border-radius: 2px;
254 animation: lzin 0.3s ease both;
255 }
256 .lz-ln.off {
257 opacity: 0.45;
258 }
259
260 .lz-grid {
261 display: grid;
262 grid-template-columns: minmax(0, 1fr) 60px minmax(0, 1fr);
263 gap: 8px;
264 padding: 12px 12px 12px 10px;
265 }
266 .lz-pane {
267 display: flex;
268 flex-direction: column;
269 min-width: 0;
270 }
271 .lz-status {
272 height: 22px;
273 margin: 3px 0 6px;
274 padding-left: 4px;
275 font: 600 11px/22px var(--alc-font-mono);
276 white-space: nowrap;
277 overflow: hidden;
278 text-overflow: ellipsis;
279 color: var(--alc-accent-text);
280 }
281 .lz-status.wait {
282 color: var(--lz-ix);
283 }
284 .lz-progview {
285 height: auto !important;
286 }
287 .lz-view {
288 position: relative;
289 height: calc(15 * var(--lz-lh) + 8px);
290 padding: 4px 0;
291 overflow: hidden;
292 background: var(--lz-panel);
293 border: 1px solid var(--lz-bd);
294 border-radius: 8px;
295 }
296 .lz-file .lz-code {
297 transition: transform 0.7s cubic-bezier(0.4, 0, 0.2, 1);
298 }
299 .lz-file {
300 flex: 1;
301 height: auto;
302 min-height: 200px;
303 }
304 /* out of flow, so the right pane matches the left column's height */
305 .lz-file .lz-code {
306 position: absolute;
307 top: 4px;
308 right: 0;
309 left: 0;
310 }
311 .lz-memview {
312 height: calc(13 * var(--lz-lh) + 8px);
313 }
314 .lz-file::after {
315 position: absolute;
316 right: 0;
317 bottom: 0;
318 left: 0;
319 height: 18px;
320 content: "";
321 background: linear-gradient(transparent, var(--lz-panel));
322 }
323
324 /* meters */
325 .lz-meters {
326 display: flex;
327 justify-content: center;
328 gap: 6px;
329 padding-top: 22px;
330 }
331 .lz-meter .lz-track {
332 flex: 1;
333 }
334 .lz-meter {
335 display: flex;
336 flex-direction: column;
337 align-items: center;
338 gap: 4px;
339 width: 28px;
340 }
341 .lz-track {
342 position: relative;
343 width: 14px;
344 background: color-mix(in srgb, var(--lz-mut) 14%, transparent);
345 border-radius: 3px;
346 }
347 .lz-fill {
348 position: absolute;
349 right: 0;
350 bottom: 0;
351 left: 0;
352 height: var(--pct, 0%);
353 border-radius: 3px;
354 transition: height 0.7s cubic-bezier(0.4, 0, 0.2, 1);
355 }
356 .lz-disk {
357 --pct: 100%;
358 background: var(--lz-bd2);
359 }
360 /* No growth animation: the slice that flies over from the disk bar fills it. */
361 .lz-mem {
362 transition: none !important;
363 display: flex;
364 flex-direction: column-reverse;
365 overflow: hidden;
366 }
367 .lz-mem i {
368 flex: var(--b) 0 0;
369 background: var(--c);
370 border-top: 1px solid var(--lz-bg);
371 }
372 .lz-mem i.new {
373 animation: lzseg 1s ease;
374 }
375 @keyframes lzseg {
376 from {
377 filter: brightness(1.6);
378 }
379 }
380 .lz-mlabel {
381 font-size: 10px;
382 line-height: 1.15;
383 text-align: center;
384 color: var(--lz-mut);
385 }
386 .lz-mval {
387 font: 600 10px var(--alc-font-mono);
388 white-space: nowrap;
389 }
390 .lz-plus {
391 position: absolute;
392 left: 10px;
393 z-index: 4;
394 font: 700 11px var(--alc-font-mono);
395 white-space: nowrap;
396 color: var(--lz-st);
397 pointer-events: none;
398 animation: lzplus 1.4s ease forwards;
399 }
400 @keyframes lzplus {
401 from {
402 opacity: 1;
403 transform: translateY(0);
404 }
405 to {
406 opacity: 0;
407 transform: translateY(-16px);
408 }
409 }
410
411 /* the bytes travelling from disk to memory */
412 .lz-chip {
413 position: absolute;
414 top: 0;
415 left: 0;
416 z-index: 5;
417 padding: 1px 8px;
418 font: 700 11px var(--alc-font-mono);
419 white-space: nowrap;
420 color: var(--lz-bg);
421 background: var(--c);
422 border-radius: 999px;
423 box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
424 pointer-events: none;
425 }
426
427 .lz-truth {
428 display: flex;
429 align-items: center;
430 gap: 6px;
431 margin: 0;
432 font-size: 12.5px;
433 white-space: nowrap;
434 color: var(--lz-mut);
435 cursor: pointer;
436 }
437 .lz-truth input {
438 accent-color: var(--alc-accent);
439 transform: translateY(1px);
440 }
441 .lz-truth b {
442 color: var(--lz-fg);
443 }
444 .lz-bar {
445 display: flex;
446 align-items: center;
447 gap: 8px;
448 padding: 9px 14px;
449 border-top: 1px solid var(--lz-bd);
450 }
451 .lz button {
452 all: unset;
453 cursor: pointer;
454 padding: 4px 13px;
455 font-size: 13px;
456 font-weight: 600;
457 color: var(--lz-fg);
458 background: var(--lz-bg);
459 border: 1px solid var(--lz-bd2);
460 border-radius: 7px;
461 }
462 .lz button:focus-visible {
463 outline: 2px solid var(--alc-accent);
464 }
465 .lz button.lz-primary {
466 color: var(--lz-bg);
467 background: var(--lz-fg);
468 border-color: var(--lz-fg);
469 }
470 .lz button:disabled {
471 cursor: default;
472 opacity: 0.4;
473 }
474 .lz-bar {
475 flex-wrap: wrap;
476 }
477 @media (max-width: 620px) {
478 .lz-truth {
479 order: 5;
480 flex-basis: 100%;
481 justify-content: center;
482 }
483 }
484 .lz-dots {
485 display: flex;
486 flex: 1;
487 justify-content: center;
488 gap: 5px;
489 }
490 .lz-dot {
491 width: 18px;
492 height: 5px;
493 cursor: pointer;
494 background: var(--lz-bd);
495 border-radius: 3px;
496 }
497 .lz-dot.past {
498 background: var(--lz-bd2);
499 }
500 .lz-dot.now {
501 background: var(--lz-fg);
502 }
503
504 /* Narrow screens: stack memory, meters, disk. */
505 @media (max-width: 620px) {
506 .lz-grid {
507 grid-template-columns: minmax(0, 1fr);
508 }
509 .lz-meters {
510 flex-direction: column;
511 gap: 4px;
512 padding-top: 0;
513 }
514 .lz-meter {
515 flex-direction: row;
516 width: auto;
517 gap: 8px;
518 }
519 .lz-track {
520 order: 2;
521 flex: 1;
522 width: auto;
523 height: 12px;
524 }
525 .lz-mem {
526 flex-direction: row;
527 }
528 .lz-mem i {
529 border-top: 0;
530 border-right: 1px solid var(--lz-bg);
531 }
532 .lz-slice {
533 top: -2px !important;
534 bottom: -2px;
535 left: auto;
536 right: auto;
537 min-width: 3px;
538 }
539 .lz-fill {
540 top: 0;
541 right: auto;
542 width: var(--pct, 0%);
543 height: auto;
544 transition-property: width;
545 }
546 .lz-mlabel {
547 order: 1;
548 width: 5em;
549 text-align: left;
550 }
551 .lz-mval {
552 order: 3;
553 width: 4.5em;
554 text-align: right;
555 }
556 .lz-mval br {
557 display: none;
558 }
559 .lz-plus {
560 top: -14px !important;
561 left: auto !important;
562 right: 0;
563 }
564 }
565 @media (prefers-reduced-motion: reduce) {
566 .lz * {
567 animation: none !important;
568 transition: none !important;
569 }
570 }
571 </style>
572
573 <script>
574 const FILE = 63178;
575 const STUB = 2564;
576 const kb = (n: number) => (n < 1000 ? `${n} B` : `${(n / 1000).toFixed(1)} KB`);
577 const esc = (s: string) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;");
578
579 const OP = "var(--lz-op)";
580 const SH = "var(--lz-sh)";
581 const IX = "var(--lz-ix)";
582 const ST = "var(--lz-st)";
583
584 /** What the reader's program does. */
585 const PROG = [
586 "// at startup",
587 "import * as fly from",
588 ' "@distilled.cloud/fly-io/machines"',
589 "",
590 "// at runtime",
591 "Effect.gen(function* () {",
592 " yield* fly.getMachine({ id })",
593 " yield* fly.listMachines({ app })",
594 " yield* fly.getMachine({ id })",
595 "})",
596 ]
597
598 // Chunks the loader can read. Sizes are the real byte counts from the trace.
599 const CHUNK: Record<string, { c: string; at: number; bytes: number; label: string }> = {
600 index: { c: IX, at: 55605, bytes: 7533, label: "index" },
601 getMachine: { c: OP, at: 19123, bytes: 284, label: "getMachine" },
602 listMachines: { c: OP, at: 25842, bytes: 483, label: "listMachines" },
603 Machine: { c: SH, at: 48375, bytes: 607, label: "Machine" },
604 MachineConfig: { c: SH, at: 43941, bytes: 4591, label: "MachineConfig" },
605 MachineEvent: { c: SH, at: 43780, bytes: 131, label: "MachineEvent" },
606 };
607
608 /** The full module on disk, simplified. `[id]` tags the lines of a chunk; `~` marks never-read filler. */
609 const FULL: [string, string][] = [
610 ["", "// services.full/machines.js"],
611 ["", 'import { op, struct } from "./sdk"'],
612 ["", ""],
613 ["~", "export const createApp = op({"],
614 ["~", ' method: "POST",'],
615 ["~", ' path: "/apps",'],
616 ["~", " output: App,"],
617 ["~", "})"],
618 ["~", "// …43 more operations"],
619 ["getMachine", "export const getMachine = op({"],
620 ["getMachine", ' method: "GET",'],
621 ["getMachine", ' path: "/machines/{id}",'],
622 ["getMachine", " input: GetMachineInput,"],
623 ["getMachine", " output: Machine,"],
624 ["getMachine", " errors: [NotFound],"],
625 ["getMachine", "})"],
626 ["~", "// …15 more operations"],
627 ["listMachines", "export const listMachines = op({"],
628 ["listMachines", ' method: "GET",'],
629 ["listMachines", ' path: "/machines",'],
630 ["listMachines", " input: ListMachinesInput,"],
631 ["listMachines", " output: array(Machine),"],
632 ["listMachines", " errors: [NotFound],"],
633 ["listMachines", "})"],
634 ["~", "// …38 more operations"],
635 ["", ""],
636 ["MachineEvent", "const MachineEvent = struct({"],
637 ["MachineEvent", " type: string,"],
638 ["MachineEvent", " status: string,"],
639 ["MachineEvent", "})"],
640 ["MachineConfig", "const MachineGuest = struct({"],
641 ["MachineConfig", " cpus: number,"],
642 ["MachineConfig", " memory_mb: number,"],
643 ["MachineConfig", "})"],
644 ["MachineConfig", "const MachineConfig = struct({"],
645 ["MachineConfig", " image: string,"],
646 ["MachineConfig", " env: record(string),"],
647 ["MachineConfig", " guest: MachineGuest,"],
648 ["MachineConfig", " // …30 more fields"],
649 ["MachineConfig", "})"],
650 ["Machine", "const Machine = struct({"],
651 ["Machine", " id: string,"],
652 ["Machine", " state: string,"],
653 ["Machine", " config: MachineConfig,"],
654 ["Machine", " events: array(MachineEvent),"],
655 ["Machine", "})"],
656 ["~", "// …21 more schemas"],
657 ["", ""],
658 ["index", "/* index: where, needs"],
659 ["index", " getMachine: 19123, Machine"],
660 ["index", " listMachines: 25842, Machine"],
661 ["index", " Machine: 48375, MachineConfig,"],
662 ["index", " MachineEvent"],
663 ["index", " MachineConfig: 43941"],
664 ["index", " MachineEvent: 43780"],
665 ["index", " …126 more rows */"],
666 ];
667
668 // ---- "I can handle the truth" mode: real excerpts from the traced build ----
669 /** The real full module, hard-wrapped. `~` lines stand in for bytes not shown. */
670 const REAL_FULL: [string, string][] = [
671 [
672 "",
673 "import{_ as e,a as t,c as n,d as r"
674 ],
675 [
676 "",
677 ",errors$BadRequest as i,errors$Con"
678 ],
679 [
680 "",
681 "…"
682 ],
683 [
684 "~",
685 "…18.9 KB of other operations…"
686 ],
687 [
688 "getMachine",
689 "{input:#1,output:#9:4,errors:[$10,"
690 ],
691 [
692 "getMachine",
693 "$11],protocol:$12,retry:$13};$8(>2"
694 ],
695 [
696 "getMachine",
697 ")@\"GetMachineRequest\";$7({app_name"
698 ],
699 [
700 "getMachine",
701 ":$3.pipe($2()),machine_id:$3.pipe("
702 ],
703 [
704 "getMachine",
705 "$2()),version:$5($3.pipe($4())),in"
706 ],
707 [
708 "getMachine",
709 "clude_leases:$5($6.pipe($4()))}).p"
710 ],
711 [
712 "getMachine",
713 "ipe($1({method:\"GET\",uri:\"/v1/apps"
714 ],
715 [
716 "getMachine",
717 "/{app_name}/machines/{machine_id}\""
718 ],
719 [
720 "getMachine",
721 ",code:200}))"
722 ],
723 [
724 "~",
725 "…6.4 KB of other operations…"
726 ],
727 [
728 "listMachines",
729 "{input:#1,output:#3,errors:[$13,$1"
730 ],
731 [
732 "listMachines",
733 "4],protocol:$15,retry:$16};$9(>4)@"
734 ],
735 [
736 "listMachines",
737 "\"ListMachinesRequest\";$11(#10:4);$"
738 ],
739 [
740 "listMachines",
741 "9(>5)@\"ListMachinesResponse\";$8({a"
742 ],
743 [
744 "listMachines",
745 "pp_name:$3.pipe($2()),include_dele"
746 ],
747 [
748 "listMachines",
749 "ted:$6($5.pipe($4())),include_leas"
750 ],
751 [
752 "listMachines",
753 "es:$6($5.pipe($4())),region:$6($3."
754 ],
755 [
756 "listMachines",
757 "pipe($4())),state:$6($3.pipe($4())"
758 ],
759 [
760 "listMachines",
761 "…"
762 ],
763 [
764 "~",
765 "…17 KB of other operations…"
766 ],
767 [
768 "MachineConfig",
769 "$6(>1)@\"FlyMachineGuest\";$5({cpu_k"
770 ],
771 [
772 "MachineConfig",
773 "ind:$2($1),cpus:$2($3),gpu_kind:$2"
774 ],
775 [
776 "MachineConfig",
777 "($1),gpus:$2($3),host_dedication_i"
778 ],
779 [
780 "MachineConfig",
781 "d:$2($1),kernel_args:$2(#4:0),max_"
782 ],
783 [
784 "MachineConfig",
785 "…"
786 ],
787 [
788 "MachineEvent",
789 "$2($1,$1)"
790 ],
791 [
792 "MachineEvent",
793 "$6(>2)@\"MachineEvent\";$7(#0);$5({i"
794 ],
795 [
796 "MachineEvent",
797 "d:$2($1),request:$2($3),source:$2("
798 ],
799 [
800 "MachineEvent",
801 "$1),status:$2($1),timestamp:$2($4)"
802 ],
803 [
804 "MachineEvent",
805 ",type:$2($1)})"
806 ],
807 [
808 "MachineConfig",
809 "$4(>53)@\"FlyMachineCacheDrive\";$4("
810 ],
811 [
812 "MachineConfig",
813 ">54)@\"FlyMachineHTTPHeader\";$7(#1)"
814 ],
815 [
816 "MachineConfig",
817 ";$4(>55)@\"FlyMachineCheck\";$9($5,#"
818 ],
819 [
820 "MachineConfig",
821 "3);$4(>56)@\"FlyContainerDependency"
822 ],
823 [
824 "MachineConfig",
825 "\";$7(#5);$4(>57)@\"FlyEnvFrom\";$7(#"
826 ],
827 [
828 "MachineConfig",
829 "…4.1 KB more of __d5…"
830 ],
831 [
832 "Machine",
833 "$4(>5)@\"CheckStatus\";$5(#0);$4(>6)"
834 ],
835 [
836 "Machine",
837 "@\"ImageRef\";$4(>7)@\"StrippedLease\""
838 ],
839 [
840 "Machine",
841 ";$4(>8)@\"Machine\";$3({name:$2($1),"
842 ],
843 [
844 "Machine",
845 "output:$2($1),status:$2($1),update"
846 ],
847 [
848 "Machine",
849 "d_at:$2($1)});$3({digest:$2($1),la"
850 ],
851 [
852 "Machine",
853 "bels:$2(#6:0),registry:$2($1),repo"
854 ],
855 [
856 "Machine",
857 "sitory:$2($1),tag:$2($1)});$3({des"
858 ],
859 [
860 "Machine",
861 "…"
862 ],
863 [
864 "~",
865 "…3 KB of other schemas…"
866 ],
867 [
868 "MachineEvent",
869 "$2($1)"
870 ],
871 [
872 "~",
873 "…3.5 KB of other schemas…"
874 ],
875 [
876 "index",
877 "/*34 131"
878 ],
879 [
880 "index",
881 "e:0:_ t:0:a n:0:c r:0:d i:0:errors"
882 ],
883 [
884 "index",
885 "$BadRequest a:0:errors$Con…"
886 ],
887 [
888 "index",
889 "…"
890 ],
891 [
892 "index",
893 "2 eqw f0a Ae|=;er7 ez3;3,p,q,h,r,a"
894 ],
895 [
896 "index",
897 ",g,m,i,40,6,8,j,l"
898 ],
899 [
900 "index",
901 "…126 more rows */"
902 ]
903 ];
904 /** Real index rows (row numbers added), tagged with the chunk they point at. */
905 const REAL_IX: [string, string][] = [
906 [
907 "getMachine",
908 "#44 2 eqw f0a Ae|=;er7 ez3;3,p,q,h"
909 ],
910 [
911 "getMachine",
912 " ,r,a,g,m,i,40,6,8,j,l"
913 ],
914 [
915 "listMachines",
916 "#60 2 jxj kcm Ke|=;jxu kb9;3,p,q,h"
917 ],
918 [
919 "listMachines",
920 " ,r,g,a,d,m,i,40,n,s,6,8,j,l"
921 ],
922 [
923 "MachineConfig",
924 "#106 1 xio xq8 __d2|=;xjc xpl;3,h,"
925 ],
926 [
927 "MachineConfig",
928 " a,d,4g,m,i"
929 ],
930 [
931 "MachineEvent",
932 "#107 1 xq8 xrg __d3|=;xqw xr5;3,h,"
933 ],
934 [
935 "MachineEvent",
936 " 1"
937 ],
938 [
939 "MachineEvent",
940 "#108 1 xrg xvx __d4|=;xs4 xvc;3,h,"
941 ],
942 [
943 "MachineEvent",
944 " a,v,d,m,i,n"
945 ],
946 [
947 "MachineConfig",
948 "#109 1 xvx 11b3 __d5|=;xwl 119v;3,"
949 ],
950 [
951 "MachineConfig",
952 " d,a,m,i,h,4g,n,g,1,3x,e,v,3w"
953 ],
954 [
955 "Machine",
956 "#110 1 11b3 11ts __d6|=;11br 11sm;"
957 ],
958 [
959 "Machine",
960 " 3,h,a,m,i,n,3x,d,3z,g,3y,4g"
961 ],
962 [
963 "MachineEvent",
964 "#126 1 145n 146t __d22|=;146c 146i"
965 ],
966 [
967 "MachineEvent",
968 " ;3,h,n"
969 ]
970 ];
971 const REAL_OP: Record<string, string[]> = {
972 "getMachine": [
973 "{input:#1,output:#9:4,errors:[$10,",
974 "$11],protocol:$12,retry:$13}"
975 ],
976 "listMachines": [
977 "{input:#1,output:#3,errors:[$13,$1",
978 "4],protocol:$15,retry:$16}"
979 ]
980 };
981 const REAL_LABEL: Record<string, string> = {
982 index: "index",
983 getMachine: "getMachine",
984 listMachines: "listMachines",
985 Machine: "__d6",
986 MachineConfig: "__d5 + __d2",
987 MachineEvent: "__d4 + __d3 + __d22",
988 };
989 /** Index row each chunk is found by, in the real index. */
990 const REAL_ROW: Record<string, string> = {
991 getMachine: "#44",
992 listMachines: "#60",
993 Machine: "#110",
994 MachineConfig: "#109",
995 MachineEvent: "#108",
996 };
997 let real = false;
998 const label = (id: string) => (real ? REAL_LABEL[id]! : CHUNK[id]!.label);
999
1000 function realLeftLines(loaded: Set<string>, stub: boolean): [string, string][] {
1001 if (!stub) return [["c", "// nothing loaded yet"]];
1002 const L: [string, string][] = [
1003 ["", 'import{$L}from"../_bundle/lazy.js";'],
1004 ["", "const{o:$o,e:$e,E:$E}=$L("],
1005 ["", ' "../services.full/machines.js",'],
1006 ["", ' "ooooo…99 o\'s…ooooo",55605,7533);'],
1007 ["", "export const …,"],
1008 ];
1009 for (const op of ["getMachine", "listMachines"]) {
1010 if (loaded.has(op)) {
1011 L.push([op, ` ${op}= // slot filled:`]);
1012 for (const t of REAL_OP[op]!) L.push([op, ` ${t}`]);
1013 } else L.push(["", ` ${op}=$o(),`]);
1014 }
1015 L.push(["c", " …97 more =$o();"]);
1016 if (loaded.has("index")) {
1017 L.push(["", ""], ["index", "/*34 131 …34 imports…"]);
1018 for (const [id, t] of REAL_IX) if (!loaded.has(id)) L.push([`ix:${id}`, t]);
1019 L.push(["ixc", " …123 more rows */"]);
1020 }
1021 if (loaded.has("Machine")) L.push(["Machine", "__d6: $4(>5)@\"CheckStatus\";…"]);
1022 if (loaded.has("MachineConfig")) L.push(["MachineConfig", "__d5: $4(>53)@\"FlyMachineCa…"]);
1023 if (loaded.has("MachineEvent")) L.push(["MachineEvent", '__d4: $6(>2)@"MachineEvent";…']);
1024 return L;
1025 }
1026
1027 /** Index rows shown in memory: [entry, line text]. Continuation lines repeat the entry. */
1028 const IXROWS: [string, string][] = [
1029 ["getMachine", " getMachine: 19123, Machine"],
1030 ["listMachines", " listMachines: 25842, Machine"],
1031 ["Machine", " Machine: 48375, MachineConfig,"],
1032 ["Machine", " MachineEvent"],
1033 ["MachineConfig", " MachineConfig: 43941"],
1034 ["MachineEvent", " MachineEvent: 43780"],
1035 ];
1036
1037 /** In-memory view: the stub, plus whatever has loaded. */
1038 function leftLines(loaded: Set<string>, stub: boolean): [string, string][] {
1039 if (real) return realLeftLines(loaded, stub);
1040 if (!stub) return [["c", "// nothing loaded yet"]];
1041 const L: [string, string][] = [
1042 ["", 'import { stub } from "./lazy.js"'],
1043 ["", ""],
1044 ];
1045 const op = (name: string, path: string, out: string) =>
1046 loaded.has(name)
1047 ? ([
1048 [name, `export const ${name} = op({`],
1049 [name, ' method: "GET",'],
1050 [name, ` path: "${path}",`],
1051 [name, ` output: ${out}, … })`],
1052 ] as [string, string][])
1053 : ([
1054 ["", `export const ${name} =`],
1055 ["", ` stub("${name}")`],
1056 ] as [string, string][]);
1057 L.push(...op("getMachine", "/machines/{id}", "Machine"));
1058 L.push(...op("listMachines", "/machines", "array(Machine)"));
1059 L.push(["c", "// …97 more stubs"]);
1060 if (loaded.has("index")) {
1061 L.push(["", ""], ["index", "const index = { // where, needs"]);
1062 for (const [name, text] of IXROWS) if (!loaded.has(name)) L.push([`ix:${name}`, text]);
1063 L.push(["ixc", " // …126 more rows"], ["index", "}"]);
1064 }
1065 if (loaded.has("Machine"))
1066 L.push(
1067 ["Machine", "const Machine = struct({ …,"],
1068 ["Machine", " config: MachineConfig,"],
1069 ["Machine", " events: MachineEvent[] })"],
1070 );
1071 for (const n of ["MachineConfig", "MachineEvent"]) if (loaded.has(n)) L.push([n, `const ${n} = struct(…)`]);
1072 return L;
1073 }
1074
1075 interface Step {
1076 t: string;
1077 p: string;
1078 /** Line of PROG running in this step; -1 = before the program starts. */
1079 at: number;
1080 /** Note on the running line once the step is done. */
1081 done?: string;
1082 stub?: boolean;
1083 reads?: string[];
1084 reuse?: string;
1085 end?: boolean;
1086 }
1087
1088 const STEPS: Step[] = [
1089 {
1090 t: "Before your program runs",
1091 p: "Two files ship in the package. The stub has an empty placeholder for each operation. The full module has the real code and, at the end, an index. Nothing is in memory.",
1092 at: -1,
1093 },
1094 {
1095 t: "import loads only the stub",
1096 p: "At startup, your <code>import</code> loads the 2.5 KB stub. Each export is a <code>stub(\"name\")</code> placeholder with no code behind it. The full module stays on disk.",
1097 at: 1,
1098 stub: true,
1099 },
1100 {
1101 t: "Right after import: read the index",
1102 p: "Still at startup, the loader reads one part of the full module: the index at the end. Each row says where an entry sits in the file and what else it needs. A row disappears once its entry is in memory.",
1103 at: 1,
1104 done: "index read",
1105 reads: ["index"],
1106 },
1107 {
1108 t: "First call to getMachine()",
1109 p: "Your program reaches its first <code>yield*</code>, and only now does loading start. The placeholder looks up getMachine's row in the index: byte 19123. The loader reads only those 284 bytes, and the real operation replaces the placeholder.",
1110 at: 6,
1111 done: "paused: needs Machine",
1112 reads: ["getMachine"],
1113 },
1114 {
1115 t: "Still in that call: load what it needs",
1116 p: "getMachine's code names <code>Machine</code>, and Machine's code names MachineConfig and MachineEvent. The loader finds each name's row in the index and reads its bytes the same way. Then your program resumes and the request goes out.",
1117 at: 6,
1118 done: "request sent",
1119 reads: ["Machine", "MachineConfig", "MachineEvent"],
1120 },
1121 {
1122 t: "First call to listMachines()",
1123 p: "Same lookup for listMachines: 483 bytes. Its row says it needs <code>Machine</code>, which is already in memory, so nothing more is read.",
1124 at: 7,
1125 done: "request sent",
1126 reads: ["listMachines"],
1127 reuse: "Machine",
1128 },
1129 {
1130 t: "getMachine() again",
1131 p: "getMachine is already loaded, so this call reads nothing. Of the 63 KB file, the faded parts were never read: 96 operations and 21 schemas.",
1132 at: 8,
1133 done: "request sent, nothing loaded",
1134 end: true,
1135 },
1136 ];
1137
1138 const root = document.getElementById("lz");
1139 if (root) {
1140 const $ = (id: string) => document.getElementById(`lz-${id}`)!;
1141 const reduced = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
1142 const wait = (ms: number) => new Promise((r) => setTimeout(r, reduced() ? 0 : ms));
1143
1144 const line = (cls: string, text: string, no?: number, c?: string) =>
1145 `<div class="lz-ln ${cls}"${c ? ` style="--c:${c}"` : ""}>${no !== undefined ? `<span class="no">${no}</span>` : ""}<span class="tx">${esc(text) || " "}</span></div>`;
1146
1147 // Right: the full file.
1148 let fileLines: HTMLElement[] = [];
1149 /** The offset each chunk's index row points at (index: where it starts). */
1150 const OFFSET: Record<string, number> = {
1151 getMachine: 19123,
1152 listMachines: 25842,
1153 MachineEvent: 43780,
1154 MachineConfig: 43941,
1155 Machine: 48375,
1156 index: 55605,
1157 };
1158 function renderRight() {
1159 let prev = "";
1160 $("right").classList.toggle("lz-bytes", !real);
1161 $("right").innerHTML = (real ? REAL_FULL : FULL)
1162 .map(([id, t], i) => {
1163 const cls = [id === "~" ? "filler" : "", t.trim().startsWith("//") || (real && id === "~") ? "c" : ""].join(" ");
1164 let html = line(cls, t, i + 1, CHUNK[id]?.c).replace('class="lz-ln', `data-id="${id}" class="lz-ln`);
1165 if (!real) {
1166 const start = OFFSET[id] !== undefined && id !== prev;
1167 html = html.replace(/<span class="no">\d+<\/span>/, `<span class="no">${start ? OFFSET[id] : ""}</span>`);
1168 }
1169 prev = id;
1170 return html;
1171 })
1172 .join("");
1173 fileLines = [...$("right").children] as HTMLElement[];
1174 }
1175 renderRight();
1176
1177 function scrollTo(id: string | undefined) {
1178 const view = $("fileview");
1179 const code = $("right");
1180 let y = 0;
1181 if (id) {
1182 const ls = fileLines.filter((l) => l.dataset.id === id);
1183 const top = ls[0]!.offsetTop;
1184 const last = ls[ls.length - 1]!;
1185 const h = Math.min(last.offsetTop + last.offsetHeight - top, view.clientHeight - 20);
1186 y = top - (view.clientHeight - h) / 2;
1187 }
1188 code.style.transform = `translateY(${-y}px)`;
1189 }
1190
1191 function paintRight(loaded: Set<string>, reading: string | undefined, end: boolean) {
1192 for (const l of fileLines) {
1193 const id = l.dataset.id!;
1194 l.classList.toggle("read", id === reading);
1195 l.classList.toggle("mem", id !== reading && loaded.has(id));
1196 l.classList.toggle("never", end && id === "~");
1197 }
1198 }
1199
1200 /** Where the loader finds each name it needs: the in-memory line that mentions it. */
1201 const FOUND_IN: Record<string, string> = {
1202 getMachine: "stub",
1203 listMachines: "stub",
1204 Machine: "getMachine",
1205 MachineConfig: "Machine",
1206 MachineEvent: "Machine",
1207 };
1208 const word = (name: string) => new RegExp(`(?<![\\w#])${name.replace(/[$]/g, "\\$&")}(?![\\w])`);
1209 const mark = (html: string, name: string) =>
1210 html.replace(/(<span class="tx">)(.*?)(<\/span>)/, (_m, a, b, c) => a + b.replace(word(name), '<mark class="lz-tok">$&</mark>') + c);
1211
1212 function paintLeft(loaded: Set<string>, stub: boolean, fresh?: string, look?: string, from = look && FOUND_IN[look]) {
1213 let markedFrom = false;
1214 $("left").innerHTML = leftLines(loaded, stub)
1215 .map(([id, t]) => {
1216 let cls = "";
1217 let c: string | undefined;
1218 if (id === "c") cls = "c";
1219 else if (id === "ixc") ((cls = "mem c"), (c = IX));
1220 else if (id.startsWith("ix:")) ((cls = id.slice(3) === look ? "look" : "mem"), (c = IX));
1221 else if (CHUNK[id]) ((cls = `mem${id === fresh ? " fresh" : ""}`), (c = CHUNK[id]!.c));
1222 let html = line(cls, t, undefined, c).replace('class="lz-ln', `data-id="${id}" class="lz-ln`);
1223 if (look) {
1224 if (!real && id === `ix:${look}` && t.trimStart().startsWith(`${look}:`)) {
1225 const off = t.match(/: (\d+)/)?.[1];
1226 if (off) html = html.replace(off, `<mark class="lz-off">${off}</mark>`);
1227 }
1228 if (real) {
1229 // Real code has no names to follow: mark the placeholder, or the index row it is found by.
1230 const tok = from === "stub" ? look : REAL_ROW[look];
1231 const hit = !markedFrom && !!tok && (from === "stub" ? t.includes(`${look}=$o()`) : id === `ix:${look}` && t.startsWith(tok));
1232 if (hit) ((markedFrom = true), (html = mark(html, tok!)));
1233 } else {
1234 const isFrom = !markedFrom && (from === "stub" ? t.includes(`stub("${look}")`) : id === from && word(look).test(t));
1235 if (isFrom) markedFrom = true;
1236 if (isFrom) html = mark(html, look);
1237 }
1238 }
1239 return html;
1240 })
1241 .join("");
1242 }
1243
1244 function paintProg(at: number, tag = "", waiting = false, markName?: string) {
1245 // the import is two lines; the active phase comment lights up too
1246 const now = at === 1 ? [1, 2] : at >= 0 ? [at] : [];
1247 const phase = at < 0 ? -1 : at <= 2 ? 0 : 4;
1248 $("prog").innerHTML = PROG.map((t, i) => {
1249 let html = line(now.includes(i) ? "now" : i === phase ? "c phase" : t.startsWith("//") ? "c" : "", t);
1250 if (markName && i === at) html = mark(html, markName);
1251 return html;
1252 }).join("");
1253 const st = $("status");
1254 const wait = waiting || tag.startsWith("paused");
1255 st.textContent = tag ? `${wait ? "⏸" : "✓"} ${tag}` : "";
1256 st.classList.toggle("wait", wait);
1257 }
1258
1259 /** Stack one colored segment per thing in memory, in load order. */
1260 function setMem(stub: boolean, loaded: Set<string>, fresh?: string) {
1261 const parts: [string, string, number][] = [];
1262 if (stub) parts.push(["stub", ST, STUB]);
1263 for (const id of loaded) parts.push([id, CHUNK[id]!.c, CHUNK[id]!.bytes]);
1264 const bytes = parts.reduce((a, p) => a + p[2], 0);
1265 const fill = $("memfill");
1266 fill.innerHTML = parts
1267 .map(([id, c, b]) => `<i class="${id === fresh ? "new" : ""}" style="--c:${c};--b:${b}" title="${id} · ${kb(b)}"></i>`)
1268 .join("");
1269 fill.style.setProperty("--pct", `${(100 * bytes) / FILE}%`);
1270 $("memval").innerHTML = kb(bytes).replace(" ", "<br />");
1271 }
1272
1273 function plus(bytes: number) {
1274 const track = $("memtrack");
1275 const tag = document.createElement("span");
1276 tag.className = "lz-plus";
1277 tag.textContent = `+${kb(bytes)}`;
1278 const fill = $("memfill");
1279 tag.style.top = `${track.clientHeight - fill.offsetHeight - 16}px`;
1280 track.appendChild(tag);
1281 setTimeout(() => tag.remove(), 1500);
1282 }
1283
1284 /** Scroll the in-memory pane so `el` sits in its middle; resolves once it's there. */
1285 async function showInMemory(el: Element | null | undefined) {
1286 const view = root!.querySelector<HTMLElement>(".lz-memview")!;
1287 if (!el) return;
1288 const e = el as HTMLElement;
1289 const top = Math.max(0, e.offsetTop - view.clientHeight / 2 + e.offsetHeight / 2);
1290 if (Math.abs(view.scrollTop - top) < 4) return;
1291 view.scrollTo({ top, behavior: reduced() ? "auto" : "smooth" });
1292 await wait(380);
1293 }
1294
1295 /** Mark the parts of the file that have been read on the disk bar. */
1296 function slice(id: string, hot = false) {
1297 const track = $("disktrack");
1298 const ch = CHUNK[id]!;
1299 const horiz = track.clientWidth > track.clientHeight;
1300 const el = document.createElement("i");
1301 el.className = `lz-slice${hot ? " hot" : ""}`;
1302 el.dataset.id = id;
1303 el.style.setProperty("--c", ch.c);
1304 if (horiz) ((el.style.left = `${(100 * ch.at) / FILE}%`), (el.style.width = `${(100 * ch.bytes) / FILE}%`));
1305 else ((el.style.top = `${(100 * ch.at) / FILE}%`), (el.style.height = `${(100 * ch.bytes) / FILE}%`));
1306 track.appendChild(el);
1307 return el;
1308 }
1309 function paintDisk(loaded: Set<string>) {
1310 $("disktrack").querySelectorAll(".lz-slice").forEach((e) => e.remove());
1311 for (const id of loaded) slice(id);
1312 }
1313
1314 /**
1315 * One read. Two things move together: the pill goes straight from the
1316 * chunk on disk to the line it becomes in memory, and the matching slice
1317 * of the disk bar slides across into the top of the memory bar.
1318 */
1319 async function fly(id: string, loaded: Set<string>, me: number) {
1320 const chunk = CHUNK[id]!;
1321 const box = root!.getBoundingClientRect();
1322 const src = fileLines.find((l) => l.dataset.id === id)!.getBoundingClientRect();
1323 // Measure where the new entry will land by laying out the next state
1324 // and putting the old one back in the same tick, so it never paints.
1325 const next = new Set(loaded).add(id);
1326 const view = root!.querySelector<HTMLElement>(".lz-memview")!;
1327 const prevScroll = view.scrollTop;
1328 paintLeft(next, true, id);
1329 const landing = $("left").querySelector<HTMLElement>(`[data-id="${id}"]`) ?? ($("left") as HTMLElement);
1330 const land = { top: landing.offsetTop, left: landing.offsetLeft, w: landing.offsetWidth, h: landing.offsetHeight };
1331 const keep = Math.max(0, land.top - view.clientHeight / 2 + land.h / 2);
1332 paintLeft(loaded, true);
1333 view.scrollTop = prevScroll;
1334 // Bring the landing spot into view (old content still showing), then aim at it.
1335 if (Math.abs(view.scrollTop - keep) >= 4) {
1336 view.scrollTo({ top: keep, behavior: reduced() ? "auto" : "smooth" });
1337 await wait(380);
1338 if (me !== run) return;
1339 }
1340 const vr = view.getBoundingClientRect();
1341 const dst = {
1342 left: vr.left + view.clientLeft + land.left,
1343 top: vr.top + view.clientTop + land.top - view.scrollTop,
1344 width: land.w,
1345 height: land.h,
1346 };
1347
1348 // Where the slice starts (on the disk bar) and ends (top of the memory bar).
1349 const hot = slice(id, true);
1350 const sl = hot.getBoundingClientRect();
1351 const mt = $("memtrack").getBoundingClientRect();
1352 const before = STUB + [...loaded].reduce((a, x) => a + CHUNK[x]!.bytes, 0);
1353 const horiz = mt.width > mt.height;
1354 const to = horiz
1355 ? { x: mt.left + (mt.width * before) / FILE, y: mt.top, w: (mt.width * chunk.bytes) / FILE, h: mt.height }
1356 : { x: mt.left, y: mt.bottom - (mt.height * (before + chunk.bytes)) / FILE, w: mt.width, h: (mt.height * chunk.bytes) / FILE };
1357
1358 const done = () => {
1359 chip.remove();
1360 seg.remove();
1361 hot.classList.remove("hot");
1362 paintLeft(next, true, id);
1363 view.scrollTop = keep;
1364 setMem(true, next, id);
1365 plus(chunk.bytes);
1366 };
1367
1368 const chip = document.createElement("span");
1369 chip.className = "lz-chip";
1370 chip.style.setProperty("--c", chunk.c);
1371 chip.textContent = `${label(id)} · ${kb(chunk.bytes)}`;
1372 root!.appendChild(chip);
1373 const seg = document.createElement("i");
1374 seg.className = "lz-segfly";
1375 seg.style.setProperty("--c", chunk.c);
1376 root!.appendChild(seg);
1377 if (reduced()) return done();
1378
1379 const w = chip.offsetWidth;
1380 const h = chip.offsetHeight;
1381 const at = (x: number, y: number) => `translate(${x - box.left - w / 2}px, ${y - box.top - h / 2}px)`;
1382 const p0 = at(src.left + 60, src.top + src.height / 2);
1383 const p1 = at(dst.left + Math.min(dst.width / 2, 110), dst.top + dst.height / 2);
1384 const rect = (x: number, y: number, ww: number, hh: number) => ({
1385 transform: `translate(${x - box.left}px, ${y - box.top}px)`,
1386 width: `${Math.max(3, ww)}px`,
1387 height: `${Math.max(3, hh)}px`,
1388 });
1389 const opts = { duration: 1300, easing: "cubic-bezier(.45,0,.25,1)", fill: "forwards" as const };
1390 await Promise.all([
1391 chip.animate(
1392 [
1393 { transform: p0, opacity: 0 },
1394 { transform: p0, opacity: 1, offset: 0.1 },
1395 { transform: p1, opacity: 1 },
1396 ],
1397 opts,
1398 ).finished,
1399 seg.animate(
1400 [
1401 { ...rect(sl.left, sl.top, sl.width, sl.height), offset: 0 },
1402 { ...rect(sl.left, sl.top, sl.width, sl.height), offset: 0.1 },
1403 rect(to.x, to.y, to.w, to.h),
1404 ],
1405 opts,
1406 ).finished,
1407 ]);
1408 done();
1409 }
1410
1411 let cur = 0;
1412 let run = 0;
1413
1414 function stateBefore(n: number) {
1415 const loaded = new Set<string>();
1416 let stub = false;
1417 let mem = 0;
1418 for (let i = 0; i < n; i++) {
1419 const st = STEPS[i]!;
1420 if (st.stub && !stub) ((stub = true), (mem += STUB));
1421 for (const r of st.reads ?? []) if (!loaded.has(r)) (loaded.add(r), (mem += CHUNK[r]!.bytes));
1422 }
1423 return { loaded, stub, mem };
1424 }
1425
1426 async function go(n: number) {
1427 const me = ++run;
1428 const forward = n === cur + 1;
1429 cur = Math.max(0, Math.min(STEPS.length - 1, n));
1430 const st = STEPS[cur]!;
1431 root!.querySelectorAll(".lz-chip").forEach((c) => c.remove());
1432
1433 $("narr").innerHTML =
1434 `<div class="lz-in">` +
1435 `<strong class="lz-ttl">${st.t}</strong><p>${st.p}</p></div>`;
1436 $("dots")
1437 .querySelectorAll(".lz-dot")
1438 .forEach((d, i) => {
1439 d.classList.toggle("now", i === cur);
1440 d.classList.toggle("past", i < cur);
1441 });
1442 ($("prev") as HTMLButtonElement).disabled = cur === 0;
1443 ($("next") as HTMLButtonElement).disabled = cur === STEPS.length - 1;
1444 paintProg(st.at, forward ? "" : (st.done ?? ""));
1445
1446 // Jumping (back, or by dot): show the finished state of this step.
1447 const s = forward ? stateBefore(cur) : stateBefore(cur + 1);
1448 const stub = forward ? s.stub || !!st.stub : s.stub;
1449 paintLeft(s.loaded, forward ? s.stub : stub);
1450 paintRight(s.loaded, undefined, !!st.end);
1451 setMem(forward ? s.stub : stub, s.loaded);
1452 paintDisk(s.loaded);
1453 const lastRead = [...(st.reads ?? [])].pop();
1454 scrollTo(lastRead);
1455 if (!forward) {
1456 return;
1457 }
1458
1459 if (st.stub) {
1460 await wait(400);
1461 if (me !== run) return;
1462 paintLeft(s.loaded, true);
1463 setMem(true, s.loaded, "stub");
1464 plus(STUB);
1465 s.mem += STUB;
1466 }
1467 for (const id of st.reads ?? []) {
1468 paintProg(st.at, `paused: loading ${label(id)}`, true);
1469 if (id !== "index") {
1470 // The loader finds the entry's row in the index first…
1471 paintLeft(s.loaded, true, undefined, id);
1472 void showInMemory($("left").querySelector(".lz-ln.look") ?? $("left").querySelector("mark"));
1473 await wait(900);
1474 if (me !== run) return;
1475 }
1476 // …which says where its bytes are in the file on disk.
1477 scrollTo(id);
1478 paintRight(s.loaded, id, false);
1479 const gutter = real ? undefined : fileLines.find((l) => l.dataset.id === id)?.querySelector(".no");
1480 gutter?.classList.add("hit");
1481 await wait(1000);
1482 gutter?.classList.remove("hit");
1483 if (me !== run) return;
1484 await fly(id, s.loaded, me);
1485 if (me !== run) return;
1486 s.loaded.add(id);
1487 s.mem += CHUNK[id]!.bytes;
1488 paintRight(s.loaded, undefined, false);
1489 await wait(500);
1490 if (me !== run) return;
1491 }
1492 if (st.reuse) {
1493 // Already in memory: point at the copy we have, and leave the disk alone.
1494 await wait(600);
1495 if (me !== run) return;
1496 paintLeft(s.loaded, true, undefined, st.reuse, "listMachines");
1497 const el = $("left").querySelector<HTMLElement>(`[data-id="${st.reuse}"]`);
1498 el?.classList.add("fresh");
1499 void showInMemory(el);
1500 paintProg(st.at, `${label(st.reuse)}: already in memory, 0 B read`, false);
1501 await wait(1600);
1502 if (me !== run) return;
1503 }
1504 if (st.done || (st.reads && !st.done)) paintProg(st.at, st.done ?? "", false);
1505 if (st.end) scrollTo(undefined);
1506 }
1507
1508 $("dots").innerHTML = STEPS.map((s, i) => `<i class="lz-dot" data-i="${i}" title="${s.t}"></i>`).join("");
1509 $("dots")
1510 .querySelectorAll<HTMLElement>(".lz-dot")
1511 .forEach((d) => (d.onclick = () => void go(Number(d.dataset.i))));
1512 ($("real") as HTMLInputElement).onchange = (e) => {
1513 real = (e.target as HTMLInputElement).checked;
1514 renderRight();
1515 // Replay the current step in the new view.
1516 const at = cur;
1517 cur = at - 1;
1518 void go(at);
1519 };
1520 $("next").onclick = () => void go(cur + 1);
1521 $("prev").onclick = () => void go(cur - 1);
1522 root.addEventListener("keydown", (e) => {
1523 if (e.key === "ArrowRight") void go(cur + 1);
1524 if (e.key === "ArrowLeft") void go(cur - 1);
1525 });
1526 void go(0);
1527 }
1528 </script>