@blueshed/joined 0.1.20 → 0.2.1

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 (84) hide show
  1. package/.claude/skills/joined/SKILL.md +1 -1
  2. package/.claude/skills/joined/reference.md +99 -18
  3. package/README.md +6 -4
  4. package/docs/design.md +31 -8
  5. package/docs/introduction.md +1 -1
  6. package/docs/learning.md +1 -1
  7. package/docs/light-and-shadow.svg +119 -0
  8. package/docs/links-and-trust.svg +93 -0
  9. package/docs/logo.svg +47 -0
  10. package/docs/stories-to-code.svg +86 -0
  11. package/docs/wire-op.svg +99 -0
  12. package/examples/cafe/kitchen.ts +2 -2
  13. package/examples/desk/card.ts +43 -0
  14. package/examples/desk/desk.css +17 -0
  15. package/examples/desk/desk.json +41 -0
  16. package/examples/desk/generated/elements.ts +69 -0
  17. package/examples/desk/generated/queries.ts +33 -0
  18. package/examples/desk/generated/rules.ts +94 -0
  19. package/examples/desk/index.html +34 -0
  20. package/examples/desk/lanes.css +126 -0
  21. package/examples/desk/lanes.ts +133 -0
  22. package/examples/desk/migrations/001-desk.ts +8 -0
  23. package/examples/desk/page.ts +29 -0
  24. package/examples/desk/pulse.ts +20 -0
  25. package/examples/desk/routes.ts +9 -0
  26. package/examples/desk/steps.ts +32 -0
  27. package/examples/desk/stories.jsonl +18 -0
  28. package/examples/desk/tally.css +62 -0
  29. package/examples/desk/tally.ts +76 -0
  30. package/examples/desk/thumbs.ts +45 -0
  31. package/examples/desk/trust.ts +24 -0
  32. package/examples/desk/words.ts +17 -0
  33. package/examples/trip/bar.ts +1 -1
  34. package/examples/trip/calendar-map.css +2 -0
  35. package/examples/trip/drags.ts +3 -3
  36. package/examples/trip/drawer.ts +5 -5
  37. package/examples/trip/event-form.ts +8 -7
  38. package/examples/trip/holding.ts +1 -1
  39. package/examples/trip/ics.ts +22 -112
  40. package/examples/trip/line-stops.ts +1 -1
  41. package/examples/trip/line-words.ts +1 -1
  42. package/examples/trip/line.ts +1 -1
  43. package/examples/trip/map.ts +1 -1
  44. package/examples/trip/month.ts +1 -1
  45. package/examples/trip/other-weeks.ts +1 -1
  46. package/examples/trip/page.ts +2 -2
  47. package/examples/trip/peek.ts +1 -1
  48. package/examples/trip/routes.ts +3 -3
  49. package/examples/trip/shell.ts +3 -3
  50. package/examples/trip/stays.ts +1 -1
  51. package/examples/trip/week.ts +1 -1
  52. package/examples/trip/writes.ts +1 -1
  53. package/examples/trips/card.ts +1 -1
  54. package/examples/trips/map.ts +1 -1
  55. package/examples/weeks/derive.ts +1 -1
  56. package/package.json +9 -2
  57. package/src/attachment.ts +18 -11
  58. package/src/client.ts +7 -0
  59. package/src/demo.css +29 -26
  60. package/src/demo.ts +2 -2
  61. package/src/draft.ts +23 -8
  62. package/src/files.ts +25 -9
  63. package/{examples/trip/mercator.ts → src/geo.ts} +13 -3
  64. package/src/icon-names.ts +4 -0
  65. package/src/ics.ts +130 -0
  66. package/src/members/group.ts +3 -1
  67. package/src/members/panel.css +208 -201
  68. package/src/members/panel.ts +17 -8
  69. package/src/ops.ts +5 -0
  70. package/src/outbox.ts +70 -0
  71. package/src/page.css +76 -70
  72. package/src/page.ts +86 -15
  73. package/src/parts.css +575 -428
  74. package/src/parts.ts +18 -5
  75. package/{examples/trip → src}/place-search.ts +5 -5
  76. package/{examples/trip → src}/places.ts +9 -7
  77. package/src/rank.ts +20 -0
  78. package/src/scrim.ts +81 -0
  79. package/src/server.ts +59 -13
  80. package/{examples/trip → src}/time.ts +37 -5
  81. package/src/together.ts +2 -6
  82. package/src/tokens.css +5 -0
  83. package/src/tokens.json +35 -0
  84. package/src/write.ts +29 -14
@@ -1,208 +1,215 @@
1
1
  /* <members-panel>'s own look, adopted into its shadow DOM: the fewer design's ink, lines and 4px
2
2
  corners. */
3
3
 
4
- :host {
5
- display: inline-flex;
6
- flex-direction: column;
7
- gap: var(--space-8);
8
- font: var(--font-row);
9
- color: var(--ink);
10
- }
11
- :host([hidden]) {
12
- display: none;
13
- }
14
- [hidden] {
15
- display: none !important;
16
- }
17
- .bar {
18
- display: flex;
19
- gap: var(--space-8);
20
- align-items: center;
21
- }
22
- /* its buttons are joined's (src/parts.css, adopted before this); the tabs and a row's menu are its own */
23
- button {
24
- font: inherit;
25
- cursor: pointer;
26
- }
27
- /* away: the button says so, and comes back as it was */
28
- .who.away {
29
- border-style: dashed;
30
- border-color: var(--muted);
31
- color: var(--muted);
32
- }
33
- .status {
34
- color: var(--danger);
35
- font: var(--font-small);
36
- }
37
- /* nothing to say (a status, a note), it takes no room: the next thing on the page's bar sits at the
38
- bar's own spacing */
39
- .bar > :empty {
40
- display: none;
41
- }
42
- .note {
43
- color: var(--muted);
44
- font: var(--font-small);
45
- }
46
- /* a notice the BDFL left you: a toast (src/toast.ts), at the foot of the page, taking no room in the bar,
47
- held until you close it */
48
- .notice {
49
- position: fixed;
50
- left: 50%;
51
- bottom: calc(var(--space-16) + env(safe-area-inset-bottom));
52
- z-index: 20;
53
- display: flex;
54
- align-items: center;
55
- gap: var(--space-8);
56
- box-sizing: border-box;
57
- /* as wide as its words, as the screen allows: fixed at the middle, it would fit only the half to its right */
58
- width: max-content;
59
- max-width: min(36rem, calc(100vw - 2rem));
60
- padding: var(--space-4) var(--space-4) var(--space-4) var(--space-16);
61
- border-radius: 4px;
62
- background: var(--ink);
63
- color: var(--card);
64
- font: var(--font-small);
65
- translate: -50% 0;
66
- }
67
- .notice p {
68
- margin: 0;
69
- flex: 1;
70
- }
71
- .notice button {
72
- color: inherit;
73
- }
74
- /* the group opens from the button that names you, under it, and flips to keep on the page */
75
- .who {
76
- anchor-name: --who;
77
- }
78
- /* the group is a sheet (src/parts.css), anchored under its button; on a phone, rising from the foot */
79
- #group {
80
- position-anchor: --who;
81
- position-area: block-end span-inline-end;
82
- position-try-fallbacks: flip-inline;
83
- inset: auto;
84
- margin: 4px 0 0;
85
- }
86
- @media (max-width: 40rem) {
4
+ @layer joined {
5
+ :host {
6
+ display: inline-flex;
7
+ flex-direction: column;
8
+ gap: var(--space-8);
9
+ font: var(--font-row);
10
+ color: var(--ink);
11
+ }
12
+ :host([hidden]) {
13
+ display: none;
14
+ }
15
+ [hidden] {
16
+ display: none !important;
17
+ }
18
+ .bar {
19
+ display: flex;
20
+ gap: var(--space-8);
21
+ align-items: center;
22
+ }
23
+ /* its buttons are joined's (src/parts.css, adopted before this); the tabs and a row's menu are its own */
24
+ button {
25
+ font: inherit;
26
+ cursor: pointer;
27
+ }
28
+ /* away: the button says so, and comes back as it was */
29
+ .who.away {
30
+ border-style: dashed;
31
+ border-color: var(--muted);
32
+ color: var(--muted);
33
+ }
34
+ .status {
35
+ color: var(--danger);
36
+ font: var(--font-small);
37
+ }
38
+ /* nothing to say (a status, a note), it takes no room: the next thing on the page's bar sits at the
39
+ bar's own spacing */
40
+ .bar > :empty {
41
+ display: none;
42
+ }
43
+ .note {
44
+ color: var(--muted);
45
+ font: var(--font-small);
46
+ }
47
+ /* what's kept for when there's signal: said in ink, not a refusal */
48
+ .waiting {
49
+ color: var(--ink);
50
+ font: var(--font-small);
51
+ }
52
+ /* a notice the BDFL left you: a toast (src/toast.ts), at the foot of the page, taking no room in the bar,
53
+ held until you close it */
54
+ .notice {
55
+ position: fixed;
56
+ left: 50%;
57
+ bottom: calc(var(--space-16) + env(safe-area-inset-bottom));
58
+ z-index: 20;
59
+ display: flex;
60
+ align-items: center;
61
+ gap: var(--space-8);
62
+ box-sizing: border-box;
63
+ /* as wide as its words, as the screen allows: fixed at the middle, it would fit only the half to its right */
64
+ width: max-content;
65
+ max-width: min(36rem, calc(100vw - 2rem));
66
+ padding: var(--space-4) var(--space-4) var(--space-4) var(--space-16);
67
+ border-radius: 4px;
68
+ background: var(--ink);
69
+ color: var(--card);
70
+ font: var(--font-small);
71
+ translate: -50% 0;
72
+ }
73
+ .notice p {
74
+ margin: 0;
75
+ flex: 1;
76
+ }
77
+ .notice button {
78
+ color: inherit;
79
+ }
80
+ /* the group opens from the button that names you, under it, and flips to keep on the page */
81
+ .who {
82
+ anchor-name: --who;
83
+ }
84
+ /* the group is a sheet (src/parts.css), anchored under its button; on a phone, rising from the foot */
87
85
  #group {
88
- position-area: none;
89
- inset: auto 0 0;
86
+ position-anchor: --who;
87
+ position-area: block-end span-inline-end;
88
+ position-try-fallbacks: flip-inline;
89
+ inset: auto;
90
+ margin: 4px 0 0;
91
+ }
92
+ @media (max-width: 40rem) {
93
+ #group {
94
+ position-area: none;
95
+ inset: auto 0 0;
96
+ margin: 0;
97
+ }
98
+ }
99
+ /* the sheet holds still: its title, the tabs, the way to invite and to sign out stay where they are, and only the
100
+ list of people (or agents) scrolls, however many there are */
101
+ #group:popover-open {
102
+ display: flex;
103
+ flex-direction: column;
104
+ max-height: min(85dvh, 36rem);
105
+ overflow: hidden;
106
+ }
107
+ #group > :not(.people, .agents) {
108
+ flex: none;
109
+ }
110
+ .people,
111
+ .agents {
112
+ display: flex;
113
+ flex-direction: column;
114
+ flex: 0 1 auto;
115
+ min-height: 0;
116
+ }
117
+ .people > ul,
118
+ .agents > ul {
119
+ min-height: 0;
120
+ overflow-y: auto;
121
+ overscroll-behavior: contain;
122
+ }
123
+ ul {
124
+ list-style: none;
90
125
  margin: 0;
126
+ padding: 0;
127
+ }
128
+ li {
129
+ position: relative;
130
+ display: flex;
131
+ align-items: center;
132
+ gap: 8px;
133
+ min-height: 36px;
134
+ }
135
+ /* a line for each step in, to who invited them */
136
+ li::before {
137
+ content: "";
138
+ position: absolute;
139
+ left: var(--space-10);
140
+ top: 0;
141
+ bottom: 0;
142
+ width: calc(var(--depth, 0) * 1.25rem);
143
+ background: linear-gradient(to right, var(--edge) 1px, transparent 1px) 0 0 / 1.25rem 100% repeat-x;
144
+ }
145
+ li .name,
146
+ li input {
147
+ flex: 1;
148
+ min-width: 0;
149
+ margin-left: 8px;
150
+ overflow: hidden;
151
+ text-overflow: ellipsis;
152
+ white-space: nowrap;
153
+ }
154
+ /* your own name, changed where it is: a field, 16px (src/parts.css), its edge shown only when touched */
155
+ li input {
156
+ padding: 2px 6px;
157
+ margin-left: 2px;
158
+ border: 1px solid transparent;
159
+ border-radius: 4px;
160
+ background: none;
161
+ color: inherit;
162
+ }
163
+ li input:hover,
164
+ li input:focus {
165
+ border-color: var(--control-edge);
166
+ }
167
+ .agents li svg.icon {
168
+ margin-left: 8px;
169
+ color: var(--muted);
170
+ }
171
+ .tag {
172
+ font: var(--font-tiny);
173
+ line-height: 16px;
174
+ color: var(--muted);
175
+ border: 1px solid var(--edge);
176
+ border-radius: 999px;
177
+ padding: 0 6px;
178
+ }
179
+ form {
180
+ display: flex;
181
+ gap: 8px;
182
+ margin: 0;
183
+ }
184
+ .people form,
185
+ .agents form {
186
+ padding-top: 10px;
187
+ margin-top: 6px;
188
+ border-top: 1px solid var(--line);
189
+ }
190
+ form input {
191
+ flex: 1;
192
+ }
193
+ /* the way out, and all you can see to take with you, side by side */
194
+ .leave {
195
+ display: flex;
196
+ flex-wrap: wrap;
197
+ justify-content: space-between;
198
+ gap: var(--space-4);
199
+ }
200
+ .leave a {
201
+ text-decoration: none;
202
+ }
203
+ /* a row's menu: what else you may do for them, a list of choices (src/parts.css), the one that takes away last */
204
+ .menu {
205
+ inset: auto;
206
+ margin: 0;
207
+ min-width: 12rem;
208
+ padding: var(--space-4);
209
+ border: 1px solid var(--edge);
210
+ border-radius: 4px;
211
+ }
212
+ svg.icon {
213
+ flex: none;
91
214
  }
92
- }
93
- /* the sheet holds still: its title, the tabs, the way to invite and to sign out stay where they are, and only the
94
- list of people (or agents) scrolls, however many there are */
95
- #group:popover-open {
96
- display: flex;
97
- flex-direction: column;
98
- max-height: min(85dvh, 36rem);
99
- overflow: hidden;
100
- }
101
- #group > :not(.people, .agents) {
102
- flex: none;
103
- }
104
- .people,
105
- .agents {
106
- display: flex;
107
- flex-direction: column;
108
- flex: 0 1 auto;
109
- min-height: 0;
110
- }
111
- .people > ul,
112
- .agents > ul {
113
- min-height: 0;
114
- overflow-y: auto;
115
- overscroll-behavior: contain;
116
- }
117
- ul {
118
- list-style: none;
119
- margin: 0;
120
- padding: 0;
121
- }
122
- li {
123
- position: relative;
124
- display: flex;
125
- align-items: center;
126
- gap: 8px;
127
- min-height: 36px;
128
- }
129
- /* a line for each step in, to who invited them */
130
- li::before {
131
- content: "";
132
- position: absolute;
133
- left: var(--space-10);
134
- top: 0;
135
- bottom: 0;
136
- width: calc(var(--depth, 0) * 1.25rem);
137
- background: linear-gradient(to right, var(--edge) 1px, transparent 1px) 0 0 / 1.25rem 100% repeat-x;
138
- }
139
- li .name,
140
- li input {
141
- flex: 1;
142
- min-width: 0;
143
- margin-left: 8px;
144
- overflow: hidden;
145
- text-overflow: ellipsis;
146
- white-space: nowrap;
147
- }
148
- /* your own name, changed where it is: a field, 16px (src/parts.css), its edge shown only when touched */
149
- li input {
150
- padding: 2px 6px;
151
- margin-left: 2px;
152
- border: 1px solid transparent;
153
- border-radius: 4px;
154
- background: none;
155
- color: inherit;
156
- }
157
- li input:hover,
158
- li input:focus {
159
- border-color: var(--control-edge);
160
- }
161
- .agents li svg.icon {
162
- margin-left: 8px;
163
- color: var(--muted);
164
- }
165
- .tag {
166
- font: var(--font-tiny);
167
- line-height: 16px;
168
- color: var(--muted);
169
- border: 1px solid var(--edge);
170
- border-radius: 999px;
171
- padding: 0 6px;
172
- }
173
- form {
174
- display: flex;
175
- gap: 8px;
176
- margin: 0;
177
- }
178
- .people form,
179
- .agents form {
180
- padding-top: 10px;
181
- margin-top: 6px;
182
- border-top: 1px solid var(--line);
183
- }
184
- form input {
185
- flex: 1;
186
- }
187
- /* the way out, and all you can see to take with you, side by side */
188
- .leave {
189
- display: flex;
190
- flex-wrap: wrap;
191
- justify-content: space-between;
192
- gap: var(--space-4);
193
- }
194
- .leave a {
195
- text-decoration: none;
196
- }
197
- /* a row's menu: what else you may do for them, a list of choices (src/parts.css), the one that takes away last */
198
- .menu {
199
- inset: auto;
200
- margin: 0;
201
- min-width: 12rem;
202
- padding: var(--space-4);
203
- border: 1px solid var(--edge);
204
- border-radius: 4px;
205
- }
206
- svg.icon {
207
- flex: none;
208
215
  }
@@ -6,7 +6,9 @@
6
6
  // removing, and the BDFL's acts, each asked in a dialog that says what follows and wants a reason.
7
7
  // Inviting by name; renaming yourself in your row. A notice the BDFL left you shows until you close it,
8
8
  // and a farewell when you're removed. Like any display, it moves only what changed: a row is made
9
- // again only when what it shows has, and the others are left as they are.
9
+ // again only when what it shows has, and the others are left as they are. It looks and moves as the page's parts do
10
+ // (adopt()), and an app gives it a touch of its own by its parts: members-panel::part(bar | who | notice | sheet |
11
+ // menu | ask | waiting).
10
12
 
11
13
  import { icon, iconButton, iconify } from "../icons.ts";
12
14
  import { keep, type Row } from "../keep.ts";
@@ -15,7 +17,7 @@ import { collections } from "./generated/rules.ts";
15
17
  import { JMember, type JMembers } from "./generated/elements.ts";
16
18
  import { consequences, type Member } from "./trust.ts";
17
19
  import STYLE from "./panel.css" with { type: "text" };
18
- import { said, showsOpen, PARTS } from "../parts.ts";
20
+ import { adopt, said, showsOpen } from "../parts.ts";
19
21
  import type { Answer } from "../page.ts";
20
22
 
21
23
  customElements.define(collections.members.tag, JMember);
@@ -76,11 +78,11 @@ class MembersPanel extends HTMLElement {
76
78
  connectedCallback() {
77
79
  if (this.#root.childElementCount) return;
78
80
  if (!this.querySelector(collections.members.place)) this.append(document.createElement(collections.members.place));
79
- this.#root.adoptedStyleSheets = [PARTS, SHEET];
81
+ adopt(this.#root, SHEET);
80
82
  this.#root.innerHTML = `
81
- <div class="bar"><button class="who act" popovertarget="group" type="button"><span>The group</span></button><span class="status" role="status"></span><span class="note"></span></div>
82
- <div class="notice" hidden><p></p><button class="icon-only" type="button" name="close" aria-label="Close" title="Close" data-icon="close"></button></div>
83
- <div id="group" class="sheet" popover>
83
+ <div class="bar" part="bar"><button class="who act" part="who" popovertarget="group" type="button"><span>The group</span></button><span class="status" role="status"></span><span class="waiting" part="waiting" role="status"></span><span class="note"></span></div>
84
+ <div class="notice" part="notice" hidden><p></p><button class="icon-only" type="button" name="close" aria-label="Close" title="Close" data-icon="close"></button></div>
85
+ <div id="group" class="sheet" part="sheet" popover>
84
86
  <header><h2>The group</h2><button type="button" class="icon-only" popovertarget="group" popovertargetaction="hide" aria-label="Close" title="Close" data-icon="close"></button></header>
85
87
  <div class="j-tabs" role="tablist" aria-label="The group"><button type="button" role="tab" name="people"></button><button type="button" role="tab" name="agents"></button></div>
86
88
  <div class="people" role="tabpanel"><ul class="tree"></ul>
@@ -88,8 +90,8 @@ class MembersPanel extends HTMLElement {
88
90
  <div class="agents" role="tabpanel"><ul></ul>
89
91
  <form class="agent"><input name="label" placeholder="A label, as claude" aria-label="An agent's label" required autocomplete="off"><button class="act" data-icon="add">Make</button></form></div>
90
92
  <div class="leave"><form class="out" method="post" action="/out"><button class="act quiet" data-icon="out">Sign out of this browser</button></form><a class="act quiet" href="/export" download="joined.json" data-icon="download">Download all you can see</a></div>
91
- <div class="menu j-choices" popover role="menu"></div>
92
- <dialog class="ask"><h2></h2><p></p><label>Why, in words they'll read<textarea></textarea></label>
93
+ <div class="menu j-choices" part="menu" popover role="menu"></div>
94
+ <dialog class="ask" part="ask"><h2></h2><p></p><label>Why, in words they'll read<textarea></textarea></label>
93
95
  <footer><button type="button" name="no" class="act">Never mind</button><button type="button" name="yes" class="act danger"></button></footer></dialog>
94
96
  </div>`;
95
97
  iconify(this.#root);
@@ -149,6 +151,13 @@ class MembersPanel extends HTMLElement {
149
151
  });
150
152
  for (const away of [true, false])
151
153
  document.addEventListener(away ? "away" : "back", () => ((this.#away = away), this.#draw()));
154
+ // what was written with no signal, kept on this device till it's back (src/outbox.ts): said in the bar
155
+ document.addEventListener("waiting", (e) => {
156
+ const kept = (e as CustomEvent<{ why?: string }[]>).detail,
157
+ line = this.#root.querySelector<HTMLElement>(".waiting")!;
158
+ line.textContent = kept.length ? `${kept.length} waiting for signal` : "";
159
+ line.title = kept.length ? "Kept on this device: sent by itself once it's back." : "";
160
+ });
152
161
  // its document refused to the <j-members> it holds: the project has no members, so there's no group to draw;
153
162
  // unless it's refused to a stranger (the page signed out first), who is shown the note to sign in
154
163
  this.addEventListener("refused", () => (this.hidden = !document.body.hasAttribute("data-signed-out")));
package/src/ops.ts CHANGED
@@ -54,6 +54,11 @@ export function docParts(doc: string) {
54
54
  return { base, name, key, params: Object.fromEntries(new URLSearchParams(query ?? "")) as Record<string, string> };
55
55
  }
56
56
 
57
+ /** "$n": a write of several's nth add (src/together.ts). */
58
+ export const NTH = /^\$([1-9]\d*)$/;
59
+ /** Whether a part's document is named by an add before it in its write (`board:$1`): no page holds it yet. */
60
+ export const madeBy = (doc: string) => NTH.test(docParts(doc).key ?? "");
61
+
57
62
  /** An op's path, in its parts; `link` is an id in a list of ids, and `more` anything past it, which no op may have. */
58
63
  export function parts(path: string) {
59
64
  const [, coll = "", key = "", field, link, ...more] = path.split("/");
package/src/outbox.ts ADDED
@@ -0,0 +1,70 @@
1
+ // What's written with no signal (on a train, in a temple), kept on this device: not lost, and not said to be saved.
2
+ // Only a write that adds and does nothing else (a cost, a person, a post: every op an add at a collection's end) is
3
+ // kept, as nothing changed meanwhile can make an add stale; a change to what's there (a replace, a remove, a move)
4
+ // made away is refused at once, as ever, and a drawer keeps it to save again. Kept in the browser's storage under who
5
+ // it's from (as the page is told who it is), in this page's memory until then or where storage refuses, with when it
6
+ // was made. The page is told what's waiting: <body data-waiting="n"> and a "waiting" event, which the group's bar
7
+ // says. Sending them is the page's (src/page.ts), once it's back and holds their documents again: oldest first, each
8
+ // let go once it's answered, one refused for a reason that isn't the signal said and let go, never tried for ever.
9
+
10
+ import type { Op } from "./ops.ts";
11
+
12
+ /** A write kept for when there's signal: one document's ops, or a write of several documents' (writes). */
13
+ export type Kept = {
14
+ id: string;
15
+ at: number;
16
+ doc?: string;
17
+ ops?: Op[];
18
+ why?: string;
19
+ writes?: { doc: string; ops: Op[]; why?: string }[];
20
+ };
21
+
22
+ /** Who this page is, as it's told: the writes kept are theirs. */
23
+ let me: number | undefined;
24
+ /** What's waiting, as this page last knew it: where storage refuses (or before the page knows who it is), all of it. */
25
+ let memory: Kept[] = [];
26
+ const key = () => `joined:waiting:${me}`;
27
+
28
+ /** Whether a write only adds: each op an add at the end of a collection (or a list). */
29
+ export const adds = (ops: Op[]) => ops.length > 0 && ops.every((o) => o.op === "add" && o.path.endsWith("/-"));
30
+
31
+ /** What's waiting, oldest first. */
32
+ export function waiting(): Kept[] {
33
+ if (me === undefined) return memory;
34
+ try {
35
+ return JSON.parse(localStorage.getItem(key()) ?? "null") ?? memory;
36
+ } catch {
37
+ return memory;
38
+ }
39
+ }
40
+
41
+ function save(list: Kept[]) {
42
+ memory = list;
43
+ if (me !== undefined)
44
+ try {
45
+ if (list.length) localStorage.setItem(key(), JSON.stringify(list));
46
+ else localStorage.removeItem(key());
47
+ } catch {
48
+ // a browser that won't keep it (private, or full): it's kept for this page's life, and said all the same
49
+ }
50
+ if (list.length) document.body.dataset.waiting = String(list.length);
51
+ else delete document.body.dataset.waiting;
52
+ document.dispatchEvent(new CustomEvent("waiting", { detail: list }));
53
+ }
54
+
55
+ /** A write kept for when there's signal. */
56
+ export const keep = (write: Omit<Kept, "id" | "at">) =>
57
+ save([...waiting(), { ...write, id: crypto.randomUUID(), at: Date.now() }]);
58
+
59
+ /** A kept write let go: sent, or refused for good. */
60
+ export const sent = (id: string) => save(waiting().filter((k) => k.id !== id));
61
+
62
+ // told who this is: what was kept before it knew (this page's own, away from the start) joins theirs
63
+ document.addEventListener("told", (e) => {
64
+ const told = (e as CustomEvent<{ me?: number }>).detail.me;
65
+ if (told === undefined || told === me) return;
66
+ const before = me === undefined ? memory : [];
67
+ me = told;
68
+ memory = [];
69
+ save([...waiting(), ...before]);
70
+ });