@moku-labs/game 0.13.2 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/README.md +2 -2
  2. package/dist/app/page.d.mts +1 -1
  3. package/dist/app/system.d.mts +3 -3
  4. package/dist/app.d.mts +2 -2
  5. package/dist/app.mjs +1 -1
  6. package/dist/assets.d.mts +1 -1
  7. package/dist/{capture-fpd83Mi5.mjs → capture-D7XvoHdH.mjs} +78 -1
  8. package/dist/{commands-BrgeMP5y.d.mts → commands-Cpn2ga7s.d.mts} +5 -5
  9. package/dist/control.d.mts +4 -4
  10. package/dist/control.mjs +1 -1
  11. package/dist/{define-ZmWbWxon.d.mts → define-Bwp8CNkW.d.mts} +2 -2
  12. package/dist/{headless-BsNdJs9W.mjs → headless-BA6ffzqD.mjs} +30 -9
  13. package/dist/{headless-Dgxvp6Ym.d.mts → headless-IfD6_JMH.d.mts} +1 -1
  14. package/dist/{index-CdbE_jGs.d.mts → index-yUO4Dx2C.d.mts} +4 -4
  15. package/dist/index.d.mts +5 -5
  16. package/dist/index.mjs +1 -1
  17. package/dist/inspect.d.mts +7 -7
  18. package/dist/jsx-dev-runtime.d.mts +1 -1
  19. package/dist/jsx-runtime.d.mts +1 -1
  20. package/dist/{memory-DJ2sPExv.d.mts → memory-D1IKoFr1.d.mts} +1 -1
  21. package/dist/{run-wYty_Lwo.mjs → run-Bf2hKyjZ.mjs} +151 -43
  22. package/dist/{runtime-6qakboTm.d.mts → runtime-CWY6co6m.d.mts} +1 -1
  23. package/dist/{src-CltFFKtc.mjs → src-Dgmdzl8R.mjs} +444 -128
  24. package/dist/testing.d.mts +4 -4
  25. package/dist/testing.mjs +2 -2
  26. package/dist/{types-DGIkfZB7.d.mts → types-B98iRBSG.d.mts} +4 -4
  27. package/dist/{types-CmuwAjkV.d.mts → types-BArxen_M.d.mts} +1 -1
  28. package/dist/{types-C6y8EfEB.d.mts → types-BWDESfx2.d.mts} +94 -25
  29. package/dist/{types-zRGJdFGX.d.mts → types-D7MGz4c-.d.mts} +2 -2
  30. package/dist/{types-D57SntUt.d.mts → types-InZGjvkI.d.mts} +2 -2
  31. package/dist/{types-B3T2X6To.d.mts → types-oQoQEj-y.d.mts} +19 -12
  32. package/dist/visual.d.mts +6 -6
  33. package/dist/visual.mjs +1 -1
  34. package/llms.txt +5 -4
  35. package/package.json +3 -3
@@ -1,6 +1,6 @@
1
- import { a as sheetAlone } from "./capture-fpd83Mi5.mjs";
1
+ import { a as sheetAlone } from "./capture-D7XvoHdH.mjs";
2
2
  import { n as isDev, r as notInstalled, t as controlRefused } from "./dev-Cc50Dk7H.mjs";
3
- import { n as reproBookmark } from "./headless-BsNdJs9W.mjs";
3
+ import { n as reproBookmark } from "./headless-BA6ffzqD.mjs";
4
4
  import { a as readTarget, i as isProjectionTarget, n as isTainted, o as inputOrEmpty, r as recordCheat, s as defineCommand } from "./session-DgukhR_7.mjs";
5
5
  //#region src/plugins/anim/control.ts
6
6
  /**
@@ -342,8 +342,61 @@ function invalidBookmark() {
342
342
  return /* @__PURE__ */ new Error("[game] The bookmark is not a flow.bookmark() value.\n Pass the JSON that game.bookmark returned.");
343
343
  }
344
344
  /**
345
+ * Tells whether a JSON value has the shape of a rest point: an object with a path and an input.
346
+ *
347
+ * @param value - The `rest` field of a bookmark.
348
+ * @returns True when `path` is a string and `input` is there.
349
+ * @example
350
+ * ```ts
351
+ * isRestPoint({ path: "home", input: null }); // true
352
+ * isRestPoint({ path: "home" }); // false: a rest point has an input, `null` when it is empty
353
+ * ```
354
+ */
355
+ function isRestPoint(value) {
356
+ return isRecord(value) && typeof value.path === "string" && value.input !== void 0;
357
+ }
358
+ /**
359
+ * Reads the rest point of a bookmark taken at a transit node.
360
+ *
361
+ * @param value - The `rest` field of a bookmark.
362
+ * @returns The rest point, or `undefined` when the value is not one.
363
+ * @example
364
+ * ```ts
365
+ * readRest({ path: "home", input: null }); // { path: "home", input: null }
366
+ * readRest("home"); // undefined
367
+ * ```
368
+ */
369
+ function readRest(value) {
370
+ if (!isRestPoint(value)) return void 0;
371
+ return {
372
+ path: value.path,
373
+ input: value.input
374
+ };
375
+ }
376
+ /**
377
+ * Reads the optional fields of a bookmark onto it: the `scene` the `game.bookmark` door added and
378
+ * the `rest` point of a bookmark taken at a transit node. An absent field leaves no key.
379
+ *
380
+ * @param value - The JSON object of the bookmark.
381
+ * @param bookmark - The bookmark with its required fields.
382
+ * @returns The bookmark with its scene and its rest point.
383
+ * @throws {Error} When the scene or the rest point has the wrong shape.
384
+ */
385
+ function readBookmarkOptions(value, bookmark) {
386
+ const { scene, rest } = value;
387
+ if (scene !== void 0 && typeof scene !== "string") throw invalidBookmark();
388
+ if (scene !== void 0) bookmark.scene = scene;
389
+ if (rest !== void 0) {
390
+ const point = readRest(rest);
391
+ if (point === void 0) throw invalidBookmark();
392
+ bookmark.rest = point;
393
+ }
394
+ return bookmark;
395
+ }
396
+ /**
345
397
  * Reads a bookmark out of JSON, the value `flow.bookmark()` made and the editor kept. The
346
- * `scene` the `game.bookmark` door added is kept when present.
398
+ * `scene` the `game.bookmark` door added and the `rest` point of a bookmark taken at a transit
399
+ * node are kept when present.
347
400
  *
348
401
  * @param value - The JSON the editor sent.
349
402
  * @returns The bookmark.
@@ -358,25 +411,14 @@ function readBookmark(value) {
358
411
  const { path, input, player, session, graph } = value;
359
412
  const rng = readRng(value.rng);
360
413
  if (!(typeof path === "string" && typeof graph === "string") || !(input !== void 0 && player !== void 0 && session !== void 0) || rng === void 0) throw invalidBookmark();
361
- const { scene } = value;
362
- if (scene === void 0) return {
414
+ return readBookmarkOptions(value, {
363
415
  path,
364
416
  input,
365
417
  player,
366
418
  session,
367
419
  rng,
368
420
  graph
369
- };
370
- if (typeof scene !== "string") throw invalidBookmark();
371
- return {
372
- path,
373
- input,
374
- player,
375
- session,
376
- rng,
377
- graph,
378
- scene
379
- };
421
+ });
380
422
  }
381
423
  /**
382
424
  * Builds the error of a value that is not a repro.
@@ -577,29 +619,66 @@ function nextFrame(app) {
577
619
  });
578
620
  }
579
621
  /**
580
- * Waits on drawn frames until the graph rests at a gate: at most 600.
622
+ * Starts the restore of a bookmark and does not wait for it. Both ways it can end are marked on
623
+ * the entry, so a restore that rejects while a frame is waited on is never left unhandled.
581
624
  *
582
625
  * @param app - The app.
583
- * @returns True once it rests, false when it still does not after 600 drawn frames.
626
+ * @param bookmark - The bookmark to enter.
627
+ * @returns The restore in flight.
584
628
  */
585
- async function waitForRest(app) {
586
- for (let frame = 0; frame < REST_FRAMES; frame += 1) {
587
- if (atRest(app.flow.state())) return true;
588
- await nextFrame(app);
589
- }
590
- return atRest(app.flow.state());
629
+ function startRestore(app, bookmark) {
630
+ const entry = {
631
+ restore: app.flow.restore(bookmark),
632
+ state: "pending"
633
+ };
634
+ entry.restore.then(() => {
635
+ entry.state = "entered";
636
+ }, () => {
637
+ entry.state = "failed";
638
+ });
639
+ return entry;
640
+ }
641
+ /**
642
+ * Tells whether a restore still has frames to wait for: it has not settled, or it resolved and
643
+ * the graph does not rest at a gate yet. A gate counts only after the restore resolved: until
644
+ * then it can be the gate of the node the restore leaves. A failed restore waits for nothing.
645
+ *
646
+ * @param app - The app.
647
+ * @param entry - The restore in flight.
648
+ * @returns True while another frame is needed.
649
+ */
650
+ function waiting(app, entry) {
651
+ if (entry.state === "pending") return true;
652
+ return entry.state === "entered" && !atRest(app.flow.state());
653
+ }
654
+ /**
655
+ * Waits on drawn frames until a restore resolved and the graph rests at a gate, or the restore
656
+ * failed: at most 600.
657
+ *
658
+ * @param app - The app.
659
+ * @param entry - The restore in flight.
660
+ * @returns False when frames are still needed after 600 drawn ones.
661
+ */
662
+ async function waitForRest(app, entry) {
663
+ for (let frame = 0; frame < REST_FRAMES && waiting(app, entry); frame += 1) await nextFrame(app);
664
+ return !waiting(app, entry);
591
665
  }
592
666
  /**
593
667
  * Restores a bookmark and waits until the graph rests at a gate, then two drawn frames more, so
594
- * the screen of the gate is built and drawn.
668
+ * the screen of the gate is built and drawn. The frames run while the restore is in flight: a
669
+ * bookmark of a transit node resolves only at its gate, and on a paused clock no frame but the
670
+ * ones stepped here brings its node there.
595
671
  *
596
672
  * @param app - The app.
597
673
  * @param bookmark - The bookmark to enter.
598
- * @throws {Error} When the graph does not rest within 600 drawn frames.
674
+ * @throws {Error} When the graph does not rest within 600 drawn frames, and whatever the restore
675
+ * rejects with.
599
676
  */
600
677
  async function restoreAtRest(app, bookmark) {
601
- await app.flow.restore(bookmark);
602
- if (!await waitForRest(app)) throw new Error("[game] game.capture diff: the bookmark did not come to rest in 600 frames.\n Capture a bookmark taken at a gate.");
678
+ const entry = startRestore(app, bookmark);
679
+ const rested = await waitForRest(app, entry);
680
+ if (entry.state !== "pending") await entry.restore;
681
+ if (!rested) throw new Error("[game] game.capture diff: the bookmark did not come to rest in 600 frames.\n Capture a bookmark taken at a gate.");
603
682
  for (let frame = 0; frame < AFTER_REST_FRAMES; frame += 1) await nextFrame(app);
604
683
  }
605
684
  /**
@@ -880,8 +959,29 @@ const fillCommand = defineCommand({
880
959
  //#endregion
881
960
  //#region src/plugins/flow/control.ts
882
961
  /**
962
+ * Restores a bookmark with its `scene` expected first, so the node the restore enters mounts it.
963
+ * A refused restore puts the record back to the mounted scene, when one is mounted: no later
964
+ * node switches to the scene of a bookmark that was never entered. With nothing mounted the
965
+ * record stays, as the scenes API has no way to clear it.
966
+ *
967
+ * @param app - The app; with `scenes`, the bookmark's scene is expected before the restore.
968
+ * @param bookmark - The bookmark to enter.
969
+ * @throws {Error} What `flow.restore` refused the bookmark with, unchanged.
970
+ */
971
+ async function restoreWithScene(app, bookmark) {
972
+ const mounted = app.scenes?.current();
973
+ if (bookmark.scene !== void 0) app.scenes?.expect(bookmark.scene);
974
+ try {
975
+ await app.flow.restore(bookmark);
976
+ } catch (error) {
977
+ if (mounted !== void 0) app.scenes?.expect(mounted);
978
+ throw error;
979
+ }
980
+ }
981
+ /**
883
982
  * Enters a bookmark, or a repro's bookmark followed by its route. A bookmark's `scene` is handed
884
- * to `scenes.expect` first, so a rest node without a scene of its own mounts it.
983
+ * to `scenes.expect` first, so a node without a scene of its own mounts it: a rest node, or the
984
+ * transit node of a bookmark taken while an effect that takes answers waited.
885
985
  *
886
986
  * @param app - The app; with `scenes`, the bookmark's scene is expected before the restore.
887
987
  * @param input - Exactly one of a bookmark and a repro, as JSON.
@@ -892,13 +992,13 @@ const fillCommand = defineCommand({
892
992
  */
893
993
  async function restoreFrom(app, input) {
894
994
  const { bookmark, repro } = input;
895
- if (bookmark !== void 0 && repro === void 0) {
896
- const read = readBookmark(bookmark);
897
- if (read.scene !== void 0) app.scenes?.expect(read.scene);
898
- await app.flow.restore(read);
995
+ const bookmarkOnly = bookmark !== void 0 && repro === void 0;
996
+ const reproOnly = repro !== void 0 && bookmark === void 0;
997
+ if (bookmarkOnly) {
998
+ await restoreWithScene(app, readBookmark(bookmark));
899
999
  return app.flow.state();
900
1000
  }
901
- if (repro !== void 0 && bookmark === void 0) {
1001
+ if (reproOnly) {
902
1002
  const parsed = readRepro(repro);
903
1003
  await app.flow.restore(reproBookmark(app, parsed));
904
1004
  return app.flow.walk(parsed.route);
@@ -959,15 +1059,19 @@ const walkCommand = defineCommand({
959
1059
  }
960
1060
  });
961
1061
  /**
962
- * Takes a bookmark of the rest point: the node plus the committed state, plain JSON. An app with
963
- * the screen plugins adds the mounted `scene`, so a restore in a fresh page builds it again.
1062
+ * Takes a bookmark of where the graph stands: the node plus the committed state, plain JSON. The
1063
+ * node is the last rest node, or, while a transit node waits for an effect that takes answers (a
1064
+ * `popup`, or a hand-written descriptor with `answers`), that node; `rest` then names the rest
1065
+ * point before it. An app with the screen plugins adds the mounted `scene`, so a restore in a
1066
+ * fresh page builds it again.
964
1067
  *
965
1068
  * @example
966
1069
  * ```ts
967
- * // The editor keeps the position while the Settings popup stands over Home.
1070
+ * // The editor keeps the position while the info popup of the mini game is open over Home.
968
1071
  * const { value } = await run(app, commands.bookmark);
969
- * value.path; // "settings/open"
1072
+ * value.path; // "info/show": the node that waits for the popup's answer
970
1073
  * value.scene; // "home"
1074
+ * value.rest; // { path: "home", input: null }
971
1075
  * ```
972
1076
  */
973
1077
  const bookmarkCommand = defineCommand({
@@ -990,6 +1094,10 @@ const bookmarkCommand = defineCommand({
990
1094
  * Restores a bookmark, or a repro: its state at its checkpoint, then its route. Replaces the
991
1095
  * state outside the graph, so the session is tainted and the command journaled. A bookmark's
992
1096
  * `scene` goes to `scenes.expect` first: a bookmark at a popup comes back with the scene under it.
1097
+ * A bookmark taken while a transit node waited for an effect that takes answers (a `popup`, or a
1098
+ * hand-written descriptor with `answers`) runs that node again, and the command answers once its
1099
+ * gate is open: the state it returns already lists the answers. The popup is drawn on the next
1100
+ * frame. A repro starts at a rest node only.
993
1101
  *
994
1102
  * @example
995
1103
  * ```ts
@@ -998,11 +1106,11 @@ const bookmarkCommand = defineCommand({
998
1106
  * const ran = await run(app, commands.restore, { repro });
999
1107
  * ran.state; // { path: "board/awaitIntent", frame: 12, tainted: true }
1000
1108
  *
1001
- * // A fresh page restores the bookmark game.bookmark took at the Settings popup over Home.
1002
- * bookmark.scene; // "home"
1003
- * await run(app, commands.restore, { bookmark });
1004
- * await app.flow.walk([]); // the scene stage runs after the restore resolved
1005
- * app.scenes.current(); // "home"
1109
+ * // A fresh page restores the bookmark game.bookmark took at the info popup of the mini game.
1110
+ * const { value } = await run(app, commands.restore, { bookmark });
1111
+ * value.path; // "info/show"
1112
+ * value.pending.gate; // ["ok", "close"]: the popup waits for its answer again
1113
+ * app.scenes.current(); // "home": the scene the bookmark named, under the popup
1006
1114
  * ```
1007
1115
  */
1008
1116
  const restoreCommand = defineCommand({
@@ -1,4 +1,4 @@
1
- import { a as JsxChild, m as UiIntrinsicElements, t as DescriptionNode } from "./types-CmuwAjkV.mjs";
1
+ import { a as JsxChild, m as UiIntrinsicElements, t as DescriptionNode } from "./types-BArxen_M.mjs";
2
2
 
3
3
  //#region src/plugins/ui/jsx/runtime.d.ts
4
4
  /** What the compiler passes as props: the children plus whatever the tag declared. */