samagotchi 0.188.0 → 0.189.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 (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +42 -1
  3. data/docs/architecture.md +5 -3
  4. data/docs/bundle-authoring.md +1 -1
  5. data/docs/bundles.md +15 -0
  6. data/docs/cli.md +23 -5
  7. data/docs/guardrails.md +3 -2
  8. data/docs/internals/bundles.md +3 -3
  9. data/docs/internals/pages.md +65 -18
  10. data/docs/internals/session-lifecycle.md +19 -6
  11. data/docs/internals/web-frontend.md +3 -3
  12. data/docs/internals/web-push.md +11 -5
  13. data/docs/pages.md +68 -15
  14. data/docs/peers.md +3 -0
  15. data/docs/plugins.md +25 -1
  16. data/docs/releasing.md +1 -1
  17. data/docs/scheduling.md +71 -8
  18. data/docs/sessions.md +4 -4
  19. data/docs/testing.md +1 -1
  20. data/lib/samagotchi/bundle_manifest.rb +34 -5
  21. data/lib/samagotchi/bundles/schedules/manifest.yml +11 -0
  22. data/lib/samagotchi/bundles/schedules/scripts/schedules.rb +159 -0
  23. data/lib/samagotchi/bundles/system/manifest.yml +3 -3
  24. data/lib/samagotchi/bundles/system/self_map.md +2 -2
  25. data/lib/samagotchi/bundles/system/skill_pages.md +9 -4
  26. data/lib/samagotchi/installed_bundles/manifest.rb +8 -1
  27. data/lib/samagotchi/memory_bundle/asset_installer.rb +3 -2
  28. data/lib/samagotchi/memory_bundle/builder.rb +1 -0
  29. data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
  30. data/lib/samagotchi/page_command.rb +9 -6
  31. data/lib/samagotchi/pages/bundled.rb +83 -0
  32. data/lib/samagotchi/pages/launch.rb +14 -2
  33. data/lib/samagotchi/pages/runner.rb +7 -2
  34. data/lib/samagotchi/pages/runtime.rb +83 -0
  35. data/lib/samagotchi/pages/store.rb +18 -5
  36. data/lib/samagotchi/pages.rb +47 -3
  37. data/lib/samagotchi/peer_continue.rb +14 -8
  38. data/lib/samagotchi/run_options.rb +1 -1
  39. data/lib/samagotchi/schedule_command.rb +79 -37
  40. data/lib/samagotchi/schedule_display.rb +69 -0
  41. data/lib/samagotchi/schedules/busy_part.rb +146 -0
  42. data/lib/samagotchi/schedules/edit_part.rb +120 -0
  43. data/lib/samagotchi/schedules/history_part.rb +51 -0
  44. data/lib/samagotchi/schedules/listing_part.rb +52 -0
  45. data/lib/samagotchi/schedules/notices_part.rb +116 -0
  46. data/lib/samagotchi/schedules/protection_part.rb +103 -0
  47. data/lib/samagotchi/schedules/recurrence.rb +103 -0
  48. data/lib/samagotchi/schedules.rb +134 -330
  49. data/lib/samagotchi/session_access/local.rb +9 -1
  50. data/lib/samagotchi/session_access/remote.rb +2 -1
  51. data/lib/samagotchi/session_chain/delete.rb +7 -5
  52. data/lib/samagotchi/session_lifecycle/allowed.rb +2 -1
  53. data/lib/samagotchi/session_lifecycle.rb +3 -2
  54. data/lib/samagotchi/session_manager/removal.rb +16 -9
  55. data/lib/samagotchi/session_protection.rb +99 -0
  56. data/lib/samagotchi/session_retention.rb +21 -6
  57. data/lib/samagotchi/sessions_command.rb +4 -0
  58. data/lib/samagotchi/testing/boundary.rb +13 -0
  59. data/lib/samagotchi/tools/cancel_schedule.rb +5 -2
  60. data/lib/samagotchi/turn_outcome.rb +41 -0
  61. data/lib/samagotchi/version.rb +1 -1
  62. data/lib/samagotchi/web/page_routes.rb +13 -8
  63. data/lib/samagotchi/web/public/app.js +12 -18
  64. data/lib/samagotchi/web/public/index.html +11 -2
  65. data/lib/samagotchi/web/public/owner_claim.js +7 -5
  66. data/lib/samagotchi/web/public/page_links.js +1 -1
  67. data/lib/samagotchi/web/public/page_view.js +8 -5
  68. data/lib/samagotchi/web/public/pages.css +9 -2
  69. data/lib/samagotchi/web/public/pages_group.js +21 -19
  70. data/lib/samagotchi/web/public/pages_list.js +73 -0
  71. data/lib/samagotchi/web/public/pages_model.js +54 -0
  72. data/lib/samagotchi/web/public/pages_wiring.js +66 -0
  73. data/lib/samagotchi/web/public/views.js +77 -23
  74. data/lib/samagotchi/web/push/notifier.rb +4 -3
  75. data/lib/samagotchi/web/server.rb +3 -1
  76. data/lib/samagotchi/web/session_hub.rb +64 -19
  77. metadata +17 -1
@@ -14,10 +14,11 @@ export const CLAIM_ROOTS = ".bubble.output, .bubble.user .user-message, .gen-tex
14
14
  // current.dir, sessions; read only). +claimTarget()+: participants_panel.js's
15
15
  // (the people a link would open for, or null). +toast(text, action,
16
16
  // options)+, +copyText(text)+ (copy.js), +allViewOpen()+: the page's.
17
- // +home+: the home folder the page shows as ~ (body data-home). +win+: the
18
- // window (location, history).
17
+ // +pagesAreaOpen()+: the page's pages check (views.js's: a page at #/p/NAME
18
+ // or the Pages list at #/pages open). +home+: the home folder the page shows
19
+ // as ~ (body data-home). +win+: the window (location, history).
19
20
  export function createOwnerClaim({
20
- historyEl, store, claimTarget, toast, copyText, allViewOpen, home = "", win = globalThis,
21
+ historyEl, store, claimTarget, toast, copyText, allViewOpen, pagesAreaOpen = () => false, home = "", win = globalThis,
21
22
  }) {
22
23
  // A claim link waiting for its session to be drawn (selectFromHash sets it).
23
24
  let pendingClaim = null;
@@ -35,13 +36,14 @@ export function createOwnerClaim({
35
36
  // participant page shows it (the session's folder relative, home ~); a
36
37
  // quote not found marks its turn; neither found: the top, and a toast.
37
38
  // The hash is the session's again (select() already set it when it
38
- // switched), so the same link opened again is a hashchange.
39
+ // switched), so the same link opened again is a hashchange. Not while a
40
+ // pages view is open either: #/pages or #/p/NAME stays, as All sessions does.
39
41
  function showPending() {
40
42
  const claim = pendingClaim;
41
43
  if (!claim || claim.sessionId !== store.selected) return;
42
44
  pendingClaim = null;
43
45
  const { location, history } = win;
44
- if (!allViewOpen() && location.hash !== sessionHash(store.selected)) {
46
+ if (!allViewOpen() && !pagesAreaOpen() && location.hash !== sessionHash(store.selected)) {
45
47
  history.replaceState(null, "", location.pathname + location.search + sessionHash(store.selected));
46
48
  }
47
49
  if (showClaim(historyEl, claim, { roots: CLAIM_ROOTS, opts: opts() })) return;
@@ -5,7 +5,7 @@
5
5
  // whose href starts with #/p/: a plain left click (button 0, no modifier
6
6
  // key, not a ref link) opens the view and stops the new tab; a modified or
7
7
  // middle click keeps the browser's own way. Other in-page links are left as
8
- // they are. Installed only with the pages capability (app.js).
8
+ // they are. Installed only with the pages capability (pages_wiring.js).
9
9
  import { pageNameFromHash } from "./page_route.js";
10
10
 
11
11
  // The page a click on +event+ asks for, or null when it isn't one this
@@ -1,17 +1,20 @@
1
1
  // The page view, #/p/NAME (docs/pages.md, docs/internals/pages.md): in
2
2
  // place of the session area, a page's header (Back, the title, when it ran
3
- // and how long it took, Refresh, ⋯ with Delete) and its blocks. Opening
3
+ // and how long it took, Refresh, ⋯ with Delete) and its blocks. A page a
4
+ // bundle ships (+bundle+ in its JSON) has no ⋯: it can't be deleted, only
5
+ // its bundle uninstalled (Pages::FromBundle). Opening
4
6
  // reads the stored page (GET /api/pages/NAME) and runs nothing; Refresh
5
7
  // runs the script (POST …/run, answered when the run ended) and redraws
6
8
  // from its answer. A failed run keeps the last result, dimmed, under the
7
9
  // failure line; a script changed since the result says so. Nothing is
8
10
  // live: a run from a terminal shows after the next open.
9
11
  //
10
- // Made only with the pages capability (app.js; the null-control rule).
12
+ // Made only with the pages capability (pages_wiring.js; the null-control rule).
11
13
  // +root+: #pageView. +api+: data.js api. +toast(text)+, +copyText(text)+:
12
14
  // the page's. +back()+: Back (views.js closePage). +gone()+: the page was
13
15
  // removed (a Delete, or a Refresh answered "no such page"): views.js leaves
14
- // for All sessions. +backLabel()+: what Back says ("Sessions" or "Back").
16
+ // for the Pages list. +backLabel()+: what Back says ("Pages", "Back", or
17
+ // "Sessions" from All sessions).
15
18
  // +phone()+: the page's phone width now (the table's phone form). The view
16
19
  // keeps each table's sort and opened rows (page_table.js) until another
17
20
  // page opens.
@@ -158,12 +161,12 @@ export function createPageView({
158
161
  ${meta ? html`<span class="pg-meta">${meta}</span>` : nothing}
159
162
  ${ready ? html`<span class="pg-actions">
160
163
  <button type="button" class="pg-refresh primary" ?disabled=${v.running} @click=${refresh}>${v.running ? "Running…" : "Refresh"}</button>
161
- <span class="pg-more">
164
+ ${page.bundle ? nothing : html`<span class="pg-more">
162
165
  <button type="button" class="pg-menu-btn ghost" aria-label="More" title="More" aria-haspopup="menu"
163
166
  aria-expanded=${v.menu === "closed" ? "false" : "true"}
164
167
  @click=${() => set({ menu: v.menu === "closed" ? "open" : "closed" })}>⋯</button>
165
168
  ${menuTemplate(v)}
166
- </span>
169
+ </span>`}
167
170
  </span>` : nothing}
168
171
  </div>`;
169
172
  }
@@ -4,6 +4,12 @@
4
4
  #pageView{display:none;flex:1;min-height:0;min-width:0;flex-direction:column;gap:12px;overflow-y:auto;overflow-x:hidden;padding:2px 2px 24px}
5
5
  body.page-view #pageView{display:flex}
6
6
  body.page-view :is(#center,#topStrip,#stripGrip,#allView,#sessionTitleRow){display:none}
7
+ /* The Pages list, #/pages (pages_list.js): the same place, while body.pages-list; its cards in All sessions' grid. */
8
+ #pagesList{display:none;flex:1;min-height:0;min-width:0;flex-direction:column;gap:12px;overflow-y:auto;overflow-x:hidden;padding:2px 2px 24px}
9
+ body.pages-list #pagesList{display:flex}
10
+ body.pages-list :is(#center,#topStrip,#stripGrip,#allView,#sessionTitleRow){display:none}
11
+ .pl-cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:12px;align-content:start}
12
+ .pl-cards .pg-card{padding:12px 14px;font-size:12.5px}
7
13
  .pg-head{display:flex;flex-wrap:wrap;align-items:center;gap:8px 12px}
8
14
  .pg-back{flex:none;padding:5px 10px;font-size:13px;color:var(--accent);background:transparent;border:1px solid transparent}
9
15
  @media(hover:hover){.pg-back:hover{filter:none;border-color:var(--border-strong)}}
@@ -64,8 +70,9 @@ button.pg-row-main:focus-visible{outline:2px solid var(--accent);outline-offset:
64
70
  /* The meta wraps under the title, on a line of its own. */
65
71
  .pg-meta{order:5;flex-basis:100%;white-space:normal}
66
72
  }
67
- /* All sessions' Pages group (pages_group.js): one row of cards that scrolls sideways, over a "Sessions" heading. Its own
68
- card rules: the session cards' are scoped to #allList .card. */
73
+ /* A page card (the Pages list's), and All sessions' former Pages group (pages_group.js, kept for an older cached app.js:
74
+ remove the group's rules after 0.189.0): one row of cards that scrolls sideways. Their own card rules: the session
75
+ cards' are scoped to #allList .card. */
69
76
  #pagesGroup{display:flex;flex-direction:column;gap:8px;flex:none;min-width:0}
70
77
  #pagesGroup[hidden]{display:none}
71
78
  .pg-group-label{font-size:11px;font-weight:600;line-height:1;letter-spacing:.06em;text-transform:uppercase;color:var(--text-faint)}
@@ -1,25 +1,21 @@
1
- // All sessions' Pages group (decision D1 A, docs/pages.md): above the
2
- // session cards, one row of page cards that scrolls sideways (it never takes
3
- // more than a row of a phone's first screen), each with its title and "ran 2
4
- // min ago", "failed 1 h ago" or "never ran"; a tap opens the page. A
5
- // "Sessions" heading sits over the session cards while the group shows. It
6
- // is read when All sessions opens (GET /api/pages) and filtered by the
7
- // search field (#filter) by name and title; with no page, or none the search
8
- // matches, the group and its heading are not there. Not live: a page made
9
- // meanwhile shows the next time All sessions opens.
1
+ // All sessions' former Pages group: KEPT for an older cached app.js until
2
+ // the next release (the mixed-cache rule, docs/internals/web-frontend.md):
3
+ // remove after 0.189.0, with index.html's inert #pagesGroup and pages.css's
4
+ // group rules. Pages have their own way in now: the top bar's pages pill
5
+ // and the Pages list (pages_list.js, pages_wiring.js); nothing new imports
6
+ // this. What it did: above the session cards, one row of page cards, read
7
+ // when All sessions opened (GET /api/pages) and filtered by the search
8
+ // field (#filter), under a "Pages" heading and over a "Sessions" one.
10
9
  //
11
- // Made only with the pages capability (app.js). +root+: #pagesGroup.
12
- // +filterEl+: #filter. +api+: data.js api. +open(name)+: views.js openPage.
10
+ // +root+: #pagesGroup. +filterEl+: #filter. +api+: data.js api.
11
+ // +open(name)+: views.js openPage. +model+: pages_model.js's list, read
12
+ // through instead of its own GET when given.
13
13
  import { html, nothing, render } from "./vendor/lit-html.js";
14
14
  import { createLatest } from "./latest.js";
15
- import { relativeTime } from "./format.js";
15
+ import { pageCardMeta } from "./pages_list.js";
16
16
 
17
- // A card's second line.
18
- export function pageCardMeta(page, now = Date.now()) {
19
- if (page.state === "failed" && page.failed_at) return `failed ${relativeTime(page.failed_at, now)}`;
20
- if (page.ran_at) return `ran ${relativeTime(page.ran_at, now)}`;
21
- return "never ran";
22
- }
17
+ // A card's second line (the list's).
18
+ export { pageCardMeta };
23
19
 
24
20
  // The pages the search shows: +query+ in the name or the title, any case.
25
21
  export function filterPages(pages, query = "") {
@@ -36,7 +32,7 @@ export function pagesGroupTemplate(pages, { open, now = Date.now() }) {
36
32
  <div class="pg-group-label">Sessions</div>`;
37
33
  }
38
34
 
39
- export function createPagesGroup({ root, filterEl, api, open, now = () => Date.now() }) {
35
+ export function createPagesGroup({ root, filterEl, api, open, model = null, now = () => Date.now() }) {
40
36
  let pages = [];
41
37
  const latest = createLatest();
42
38
 
@@ -47,6 +43,7 @@ export function createPagesGroup({ root, filterEl, api, open, now = () => Date.n
47
43
  }
48
44
 
49
45
  async function load() {
46
+ if (model) return model.load();
50
47
  const fresh = latest.begin();
51
48
  try {
52
49
  const answer = await api("/api/pages");
@@ -59,6 +56,11 @@ export function createPagesGroup({ root, filterEl, api, open, now = () => Date.n
59
56
  draw();
60
57
  }
61
58
 
59
+ // A failed read empties the group, as its own read does.
60
+ model?.subscribe((state) => {
61
+ pages = state.failed ? [] : state.pages;
62
+ draw();
63
+ });
62
64
  filterEl?.addEventListener("input", draw);
63
65
  draw();
64
66
  return { load, redraw: draw };
@@ -0,0 +1,73 @@
1
+ // The Pages list, #/pages (docs/pages.md): the one home of pages, opened by
2
+ // the top bar's pages pill (pages_wiring.js). In place of the session area,
3
+ // as a page is: Back, "Pages", and a card per page (its title, "ran 2 min
4
+ // ago", "failed 1 h ago" or "never ran") in /api/pages's order; a tap
5
+ // opens #/p/NAME. "Loading…" until the first answer ("Couldn't read the
6
+ // pages." when it failed), "No pages." when there is none. It draws the page's list (pages_model.js) and reads it again
7
+ // each time it opens. Also the list's hash, so views.js reads it where it
8
+ // reads a page's (a new module: page_route.js takes no new export).
9
+ //
10
+ // Made only with the pages capability (pages_wiring.js). +root+: #pagesList
11
+ // (null on an older cached index.html: nothing drawn). +model+:
12
+ // pages_model.js's. +open(name)+: views.js openPage. +back()+: views.js
13
+ // closePagesList.
14
+ import { html, nothing, render } from "./vendor/lit-html.js";
15
+ import { relativeTime } from "./format.js";
16
+
17
+ export const PAGES_HASH = "#/pages";
18
+
19
+ // Whether +hash+ is the Pages list for this viewer: never on a
20
+ // participant's page and never without the pages capability.
21
+ export function pagesListOpen(hash, { participant = null, canPages = false } = {}) {
22
+ return !participant && !!canPages && hash === PAGES_HASH;
23
+ }
24
+
25
+ // A card's second line.
26
+ export function pageCardMeta(page, now = Date.now()) {
27
+ if (page.state === "failed" && page.failed_at) return `failed ${relativeTime(page.failed_at, now)}`;
28
+ if (page.ran_at) return `ran ${relativeTime(page.ran_at, now)}`;
29
+ return "never ran";
30
+ }
31
+
32
+ // +state+: the model's ({pages, loaded}).
33
+ export function pagesListTemplate(state, { open, back, now = Date.now() }) {
34
+ let body;
35
+ if (!state.loaded && state.failed) body = html`<div class="pg-note">Couldn't read the pages.</div>`;
36
+ else if (!state.loaded) body = html`<div class="pg-note">Loading…</div>`;
37
+ else if (!state.pages.length) body = html`<div class="pg-note">No pages.</div>`;
38
+ else {
39
+ body = html`<div class="pl-cards" role="list">${state.pages.map((page) => html`<button type="button" class="pg-card"
40
+ role="listitem" data-page=${page.name} @click=${() => open(page.name)}><b>${page.title || page.name}</b><span
41
+ class="pg-card-meta">${pageCardMeta(page, now)}</span></button>`)}</div>`;
42
+ }
43
+ return html`<div class="pg-head">
44
+ <button type="button" class="pg-back ghost" @click=${back}>← Back</button>
45
+ <h1 class="pg-title">Pages</h1>
46
+ </div>${body}`;
47
+ }
48
+
49
+ export function createPagesList({ root, model, open, back, now = () => Date.now() }) {
50
+ let shown = false;
51
+
52
+ function draw() {
53
+ if (!root) return;
54
+ render(shown ? pagesListTemplate(model.state(), { open, back, now: now() }) : nothing, root);
55
+ }
56
+
57
+ model.subscribe(() => shown && draw());
58
+
59
+ return {
60
+ open() {
61
+ if (!shown) {
62
+ shown = true;
63
+ draw();
64
+ }
65
+ model.load();
66
+ },
67
+ close() {
68
+ if (!shown) return;
69
+ shown = false;
70
+ draw();
71
+ },
72
+ };
73
+ }
@@ -0,0 +1,54 @@
1
+ // The page's one list of pages (GET /api/pages, docs/pages.md): what the
2
+ // pages' ways in read (the top bar's pages pill and the Pages list,
3
+ // pages_wiring.js). DOM-free. +load()+ reads the list, or returns the read
4
+ // already out (one request at boot however many views ask); +reload()+
5
+ // always asks again (something changed: the hub's `pages` event, a
6
+ // reconnect, a Delete), and an answer older than the newest ask is dropped
7
+ // (createLatest). A failed read keeps the last list and +loaded+ as they
8
+ // were (chi web restarting doesn't hide the pill); +failed+ says the last
9
+ // read failed. Subscribers are called with the state after every answer.
10
+ //
11
+ // +api+: data.js api.
12
+ import { createLatest } from "./latest.js";
13
+
14
+ export function createPagesModel({ api }) {
15
+ let state = { pages: [], loaded: false, failed: false };
16
+ let inFlight = null;
17
+ const subscribers = new Set();
18
+ const latest = createLatest();
19
+
20
+ function publish(fields) {
21
+ state = { ...state, ...fields };
22
+ for (const fn of [...subscribers]) fn(state);
23
+ }
24
+
25
+ function reload() {
26
+ const fresh = latest.begin();
27
+ const ask = (async () => {
28
+ await null; // inFlight is set before the read starts, even when api throws at once
29
+ try {
30
+ const answer = await api("/api/pages");
31
+ if (!fresh()) return;
32
+ publish({ pages: Array.isArray(answer?.pages) ? answer.pages : [], loaded: true, failed: false });
33
+ } catch (_) {
34
+ if (!fresh()) return;
35
+ publish({ failed: true });
36
+ } finally {
37
+ if (fresh()) inFlight = null;
38
+ }
39
+ })();
40
+ inFlight = ask;
41
+ return ask;
42
+ }
43
+
44
+ function load() {
45
+ return inFlight || reload();
46
+ }
47
+
48
+ function subscribe(fn) {
49
+ subscribers.add(fn);
50
+ return () => subscribers.delete(fn);
51
+ }
52
+
53
+ return { load, reload, subscribe, state: () => state };
54
+ }
@@ -0,0 +1,66 @@
1
+ // The pages capability's parts on the main page, wired in one place so
2
+ // app.js keeps one call (docs/pages.md, docs/internals/pages.md): the page's
3
+ // list of pages (pages_model.js), the top bar's pages pill (#pagesBtn), the
4
+ // Pages list at #/pages (pages_list.js), the page view at #/p/NAME
5
+ // (page_view.js) and the #/p/ links opening a page in place (page_links.js).
6
+ // Null without the capability (the null-control rule): the pill stays
7
+ // withheld, nothing of pages is made and nothing is read.
8
+ //
9
+ // The list is read once at boot (one request: a view opened at boot shares
10
+ // it), again when All sessions, the Pages list or a page opens, after a
11
+ // Delete, and live: on the hub's `pages` event (a script made or removed)
12
+ // and on every snapshot (the hub has no replay): the first one loads (it
13
+ // shares the boot read while that is out, and covers a page made between the
14
+ // boot read and the stream), every later one, a reconnect, reads again. The
15
+ // pill shows while the list has a page, in every view; a failed read keeps it
16
+ // as it was. With an older cached views.js (no openPagesList) it stays
17
+ // hidden: it would open nothing.
18
+ //
19
+ // +viewer+: viewer.js. +$+: the page's querySelector. +api+: data.js api.
20
+ // +toast(text)+, +copyText(text)+: the page's. +turnRoots()+: the roots an
21
+ // answer's links are in. +views()+: views.js's (made after this, so a
22
+ // getter): openPage, closePage, leavePage, pageBackLabel, openPagesList,
23
+ // closePagesList.
24
+ import { createPagesModel } from "./pages_model.js";
25
+ import { createPageView } from "./page_view.js";
26
+ import { createPagesList } from "./pages_list.js";
27
+ import { installPageLinks } from "./page_links.js";
28
+
29
+ export function wirePages({ viewer, $, api, toast, copyText, turnRoots, views, win = window }) {
30
+ const pill = viewer.control($("#pagesBtn"), "pages");
31
+ if (!viewer.can("pages")) return null;
32
+ const model = createPagesModel({ api });
33
+ const open = (name) => views().openPage(name);
34
+ const view = createPageView({
35
+ root: $("#pageView"), api, toast, copyText, back: () => views().closePage(),
36
+ // Gone (a Delete, or no such page on Refresh): the list is read again, so the pill hides with the last page.
37
+ gone: () => { model.reload(); views().leavePage(); },
38
+ backLabel: () => views().pageBackLabel(), phone: () => !!win.matchMedia?.("(max-width: 600px)").matches,
39
+ });
40
+ // A page opened reads the list too (views.js calls open again while it is open: once per page).
41
+ const pageView = { ...view, open: (name) => { if (view.name() !== name) model.load(); return view.open(name); } };
42
+ const pagesList = createPagesList({ root: $("#pagesList"), model, open, back: () => views().closePagesList?.() });
43
+ installPageLinks([...turnRoots(), $("#pageView")], { open });
44
+
45
+ model.subscribe((state) => {
46
+ if (pill) pill.hidden = !state.pages.length || typeof views()?.openPagesList !== "function";
47
+ });
48
+ pill?.addEventListener("click", () => views().openPagesList?.());
49
+ model.load();
50
+
51
+ let snapshots = 0;
52
+ return {
53
+ pageView,
54
+ pagesList,
55
+ // All sessions just opened.
56
+ allViewOpened: () => model.load(),
57
+ // The hub's `pages` event.
58
+ changed: () => model.reload(),
59
+ // A hub snapshot: the first loads (sharing a read that is out), every later one is a reconnect.
60
+ snapshot: () => {
61
+ snapshots += 1;
62
+ if (snapshots > 1) model.reload();
63
+ else model.load();
64
+ },
65
+ };
66
+ }
@@ -2,11 +2,12 @@
2
2
  // browser's back button leaves it, and the open session's #/s/<id>, so a
3
3
  // reload or a copied link opens it again. A hash typed, pasted or reached by
4
4
  // back selects what it names (hashIntent). Opened from the page, All sessions
5
- // closes with history.back(). A page, #/p/NAME (page_view.js), is read here
6
- // before the session intent (hashIntent calls it "keep"): it shows in place
7
- // of the session area, and the session's hash isn't written while it is
8
- // open. app.js makes this once its pieces exist and hands the functions on
9
- // (lazily to anything made before it).
5
+ // closes with history.back(). A page, #/p/NAME (page_view.js), and the Pages
6
+ // list, #/pages (pages_list.js), are read here before the session intent
7
+ // (hashIntent calls them "keep"): they show in place of the session area,
8
+ // and the session's hash isn't written while one is open. app.js makes this
9
+ // once its pieces exist and hands the functions on (lazily to anything made
10
+ // before it).
10
11
  //
11
12
  // +win+ / +doc+: the window (location, history, the hashchange listener) and
12
13
  // the document. +participant+: the participant's page (createParticipantMain),
@@ -16,24 +17,38 @@
16
17
  // +eventsLive()+: the hub's stream is open. +refresh()+, +updateSessionTitle()+,
17
18
  // +select(id)+, +clearSelection()+: the page's. +ownerClaim()+: owner_claim.js's
18
19
  // (made after this, so a getter). +pages+: page_view.js's view, or null
19
- // without the pages capability (then no hash is a page). +allViewOpened()+:
20
- // All sessions just opened (the Pages group reads its list then).
20
+ // without the pages capability (then no hash is a page). +pagesList+:
21
+ // pages_list.js's view, or null (without the capability, or an older cached
22
+ // app.js: then #/pages is no view). +allViewOpened()+: All sessions just
23
+ // opened (the page's list of pages is read again then).
21
24
  import { ALL_SESSIONS_HASH, sessionHash } from "./route.js";
22
25
  import { hashIntent } from "./hash_intent.js";
23
26
  import { claimFromHash } from "./claim_link.js";
24
27
  import { sortedByUpdated } from "./sessions_list.js";
25
28
  import { openPageName, pageHash } from "./page_route.js";
29
+ import { PAGES_HASH, pagesListOpen } from "./pages_list.js";
26
30
 
27
31
  export function createViews({
28
32
  win = globalThis, doc = globalThis.document, participant, store, lists, topStripEl, pictures, eventsLive,
29
- refresh, updateSessionTitle, select, clearSelection, ownerClaim, pages = null, allViewOpened = () => {},
33
+ refresh, updateSessionTitle, select, clearSelection, ownerClaim, pages = null, pagesList = null,
34
+ allViewOpened = () => {},
30
35
  }) {
31
36
  const { location, history } = win;
32
37
  const $ = (sel) => doc.querySelector(sel);
33
38
  let allViewPushed = false;
34
- // A page this page opened itself (a Pages card, an answer's link): Back is
35
- // history.back(), to where it was opened from ("sessions" or "back").
36
- let pagePushed = null;
39
+ // A page or the Pages list this page opened itself is pushed with where it
40
+ // was opened from in its history entry (history.state.from: "pages",
41
+ // "sessions" or "back" for a page, "pill" for the list), so Back and its
42
+ // label stay right when Back or Forward comes to that entry again. An
43
+ // entry with no state (typed, reloaded, replaced) wasn't pushed by us.
44
+ const openedFrom = () => history.state?.from ?? null;
45
+ // Where a page goes when it has nowhere to go back to, or went.
46
+ const pagesHome = () => (pagesList ? PAGES_HASH : ALL_SESSIONS_HASH);
47
+ // Push +hash+ with +from+, and show it (pushState fires no hashchange).
48
+ function push(hash, from) {
49
+ history.pushState({ from }, "", location.pathname + location.search + hash);
50
+ applyView();
51
+ }
37
52
 
38
53
  // The page the hash opens, or null.
39
54
  function pageOpen() {
@@ -41,22 +56,51 @@ export function createViews({
41
56
  }
42
57
  function openPage(name) {
43
58
  if (pageOpen() === name) return;
44
- pagePushed = allViewOpen() ? "sessions" : "back";
45
- location.hash = pageHash(name);
59
+ push(pageHash(name), listOpen() ? "pages" : allViewOpen() ? "sessions" : "back");
46
60
  }
47
- // Back: where the page was opened from, else All sessions.
61
+ // Back: where the page was opened from, else the Pages list (in the
62
+ // page's own entry, so that list's Back leaves for the session's hash).
48
63
  function closePage() {
49
64
  if (!pageOpen()) return;
50
- if (pagePushed) history.back();
51
- else leaveAllView(ALL_SESSIONS_HASH);
65
+ if (openedFrom()) history.back();
66
+ else leaveAllView(pagesHome());
52
67
  }
53
- // The page went (deleted, or no such page on Refresh): All sessions, in
54
- // its history entry.
68
+ // The page went (deleted, or no such page on Refresh): back to the list it
69
+ // was opened from, else the Pages list in its history entry.
55
70
  function leavePage() {
56
- if (pageOpen()) leaveAllView(ALL_SESSIONS_HASH);
71
+ if (!pageOpen()) return;
72
+ if (openedFrom() === "pages") history.back();
73
+ else leaveAllView(pagesHome());
57
74
  }
58
75
  function pageBackLabel() {
59
- return pagePushed === "back" ? "Back" : "Sessions";
76
+ const from = openedFrom();
77
+ if (from === "back") return "Back";
78
+ if (from === "sessions" || !pagesList) return "Sessions";
79
+ return "Pages";
80
+ }
81
+
82
+ // The Pages list (the pill): opened from anywhere, pushed; already open,
83
+ // nothing; from a page opened from the list, back to that list's entry.
84
+ // Back: history.back() when pushed, else the session's own hash.
85
+ function listOpen() {
86
+ return pagesListOpen(location.hash, { participant, canPages: !!pagesList });
87
+ }
88
+ function openPagesList() {
89
+ if (listOpen()) return;
90
+ if (pageOpen() && openedFrom() === "pages") {
91
+ history.back();
92
+ return;
93
+ }
94
+ push(PAGES_HASH, "pill");
95
+ }
96
+ function closePagesList() {
97
+ if (!listOpen()) return;
98
+ if (openedFrom()) history.back();
99
+ else leaveAllView(sessionHash(store.selected));
100
+ }
101
+ // A page or the Pages list is open: the session's hash isn't written.
102
+ function pagesAreaOpen() {
103
+ return !!pageOpen() || listOpen();
60
104
  }
61
105
 
62
106
  function allViewOpen() {
@@ -90,7 +134,7 @@ export function createViews({
90
134
  // page for it; the selection cleared under it (a gone session) doesn't.
91
135
  function setSessionHash(id) {
92
136
  if (allViewOpen()) return;
93
- if (pageOpen()) {
137
+ if (pagesAreaOpen()) {
94
138
  if (id) leaveAllView(sessionHash(id));
95
139
  return;
96
140
  }
@@ -116,15 +160,22 @@ export function createViews({
116
160
  function applyView() {
117
161
  const open = allViewOpen();
118
162
  const page = pageOpen();
163
+ const list = listOpen();
119
164
  doc.body.classList.toggle("page-view", !!page);
165
+ doc.body.classList.toggle("pages-list", list);
166
+ if (list) {
167
+ pictures.close(); // #center is hidden there too
168
+ pagesList.open();
169
+ } else {
170
+ pagesList?.close();
171
+ }
120
172
  if (page) {
121
173
  pictures.close(); // #center, the picture viewer's parent, is hidden there
122
174
  pages.open(page);
123
175
  } else {
124
- pagePushed = null;
125
176
  pages?.close();
126
177
  }
127
- if (!open && !page) {
178
+ if (!open && !page && !list) {
128
179
  allViewPushed = false;
129
180
  }
130
181
  if (!open) {
@@ -135,6 +186,8 @@ export function createViews({
135
186
  doc.body.classList.toggle("all-view", open);
136
187
  // The sessions pill is the view's button there: pressed while it is open.
137
188
  $("#stripStatus").setAttribute("aria-pressed", String(open));
189
+ // The pages pill: pressed on the Pages list and on a page.
190
+ $("#pagesBtn")?.setAttribute("aria-pressed", String(!!page || list));
138
191
  updateSessionTitle();
139
192
  // Its chip was in the list just hidden (the strip, or all sessions).
140
193
  lists.chainPop.close();
@@ -157,5 +210,6 @@ export function createViews({
157
210
  return {
158
211
  allViewOpen, openAllView, closeAllView, leaveAllView, setSessionHash, selectFromHash, applyView,
159
212
  pageOpen, openPage, closePage, leavePage, pageBackLabel,
213
+ listOpen, openPagesList, closePagesList, pagesAreaOpen,
160
214
  };
161
215
  }
@@ -263,9 +263,10 @@ module Samagotchi
263
263
  enqueue_update("chi:#{kind}:#{version}", title, body) if kind
264
264
  end
265
265
 
266
- # A recurring schedule chi web's tick turned off (SessionHub#publish_disabled).
267
- # The key's +at+ (the tick's time) lets a later disable of the same
268
- # schedule notify again; its chi: prefix keeps it out of WAITING_TAG.
266
+ # A recurring schedule a ticker (cron, run-now or chi web's own tick)
267
+ # turned off by its budget (SessionHub#publish_disabled). The key's +at+
268
+ # (the disabling tick's time) lets a later disable of the same schedule
269
+ # notify again; its chi: prefix keeps it out of WAITING_TAG.
269
270
  def notice_schedule_disabled(event)
270
271
  data = event.data
271
272
  id = data[:id]
@@ -25,6 +25,7 @@ require_relative "auto_update"
25
25
  require_relative "token"
26
26
  require_relative "tunnel"
27
27
  require_relative "../config"
28
+ require_relative "../pages"
28
29
  require_relative "../chi_home"
29
30
  require_relative "../child_env"
30
31
  require_relative "../installed_gem"
@@ -473,7 +474,8 @@ module Samagotchi
473
474
  host = resolve_host(host)
474
475
  url ||= "http://#{url_host(host)}:#{port}/"
475
476
 
476
- hub ||= SessionHub.new(state_dir: state_dir || Session.default_state_dir, manager: manager || SessionManager)
477
+ hub ||= SessionHub.new(state_dir: state_dir || Session.default_state_dir, manager: manager || SessionManager,
478
+ pages_dir: Pages.scripts_dir)
477
479
  update_check ||= UpdateCheck.new(hub: hub)
478
480
  Token.load_or_create(token_path) if lan || public_url
479
481
  lan_option = { ip: lan.ip, token: Token::Source.new(token_path) } if lan