@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
package/src/page.css CHANGED
@@ -3,77 +3,83 @@
3
3
  .members-only), then <main>, its documents, which a signed-out page hides and an away one dims. Where a
4
4
  refusal is said, #said. Never a value by hand: the tokens'. */
5
5
 
6
- body {
7
- font: var(--font-body);
8
- max-width: 72rem;
9
- margin: 0 auto;
10
- padding: 0 var(--gutter) var(--space-48);
11
- color: var(--ink);
12
- background: var(--paper);
13
- }
14
- /* accent is black, not blue (fewer) */
15
- a {
16
- color: var(--accent);
17
- }
18
- /* joined, at the top of every page: the way home */
19
- .brand {
20
- display: inline-flex;
21
- align-items: center;
22
- gap: var(--space-8);
23
- margin: var(--space-20) 0 var(--space-24);
24
- color: var(--ink);
25
- font: var(--font-name);
26
- text-decoration: none;
27
- }
28
- h1 {
29
- font: var(--font-section);
30
- margin: 0 0 var(--space-6);
31
- }
32
- /* what the page is for, under its name */
33
- .lead {
34
- margin: 0 0 var(--space-20);
35
- max-width: 44rem;
36
- color: var(--muted);
37
- font: var(--font-lead);
38
- }
39
- /* on a phone, at the body's size: the page's own content comes sooner */
40
- @media (max-width: 40rem) {
41
- .lead {
6
+ /* the layers, named in their order wherever joined's sheets are: an app's resets (button { font: inherit }) in
7
+ reset, beneath joined's parts; anything else of an app's, in no layer, over both */
8
+ @layer reset, joined;
9
+
10
+ @layer joined {
11
+ body {
42
12
  font: var(--font-body);
13
+ max-width: 72rem;
14
+ margin: 0 auto;
15
+ padding: 0 var(--gutter) var(--space-48);
16
+ color: var(--ink);
17
+ background: var(--paper);
18
+ }
19
+ /* accent is black, not blue (fewer) */
20
+ a {
21
+ color: var(--accent);
22
+ }
23
+ /* joined, at the top of every page: the way home */
24
+ .brand {
25
+ display: inline-flex;
26
+ align-items: center;
27
+ gap: var(--space-8);
28
+ margin: var(--space-20) 0 var(--space-24);
29
+ color: var(--ink);
30
+ font: var(--font-name);
31
+ text-decoration: none;
32
+ }
33
+ h1 {
34
+ font: var(--font-section);
35
+ margin: 0 0 var(--space-6);
36
+ }
37
+ /* what the page is for, under its name */
38
+ .lead {
39
+ margin: 0 0 var(--space-20);
40
+ max-width: 44rem;
41
+ color: var(--muted);
42
+ font: var(--font-lead);
43
+ }
44
+ /* on a phone, at the body's size: the page's own content comes sooner */
45
+ @media (max-width: 40rem) {
46
+ .lead {
47
+ font: var(--font-body);
48
+ }
49
+ }
50
+ h2 {
51
+ font: var(--font-title);
52
+ margin: 0 0 var(--space-8);
53
+ }
54
+ .note {
55
+ color: var(--muted);
56
+ margin: 0 0 var(--space-16);
57
+ font: var(--font-small);
58
+ }
59
+ #said {
60
+ color: var(--danger);
61
+ min-height: 1.5em;
62
+ }
63
+ /* signed out, a page shows the note to sign in in place of its documents */
64
+ body[data-signed-out] :is(main, .members-only) {
65
+ display: none;
66
+ }
67
+ /* away (the phone slept, the network changed, the server is being deployed): it comes back by itself;
68
+ the group's button says so, and what's shown dims, as it may be behind */
69
+ body[data-away] main {
70
+ opacity: 0.6;
43
71
  }
44
- }
45
- h2 {
46
- font: var(--font-title);
47
- margin: 0 0 var(--space-8);
48
- }
49
- .note {
50
- color: var(--muted);
51
- margin: 0 0 var(--space-16);
52
- font: var(--font-small);
53
- }
54
- #said {
55
- color: var(--danger);
56
- min-height: 1.5em;
57
- }
58
- /* signed out, a page shows the note to sign in in place of its documents */
59
- body[data-signed-out] :is(main, .members-only) {
60
- display: none;
61
- }
62
- /* away (the phone slept, the network changed, the server is being deployed): it comes back by itself;
63
- the group's button says so, and what's shown dims, as it may be behind */
64
- body[data-away] main {
65
- opacity: 0.6;
66
- }
67
72
 
68
- .bar {
69
- display: flex;
70
- flex-wrap: wrap;
71
- gap: var(--space-8);
72
- align-items: center;
73
- margin: 0 0 var(--space-12);
74
- }
75
- /* an icon before a link's words; a button is a tool, and draws its own (src/parts.css) */
76
- :is(a, label) > svg.icon {
77
- vertical-align: -3px;
78
- margin-right: var(--space-6);
73
+ .bar {
74
+ display: flex;
75
+ flex-wrap: wrap;
76
+ gap: var(--space-8);
77
+ align-items: center;
78
+ margin: 0 0 var(--space-12);
79
+ }
80
+ /* an icon before a link's words; a button is a tool, and draws its own (src/parts.css) */
81
+ :is(a, label) > svg.icon {
82
+ vertical-align: -3px;
83
+ margin-right: var(--space-6);
84
+ }
79
85
  }
package/src/page.ts CHANGED
@@ -12,7 +12,8 @@
12
12
  // document, the writer's own included. A document the page holds that isn't there is told to the element
13
13
  // holding it (a "refused" event on it). What the server tells a page of its own (who it is, the links it
14
14
  // holds, a new link of its own, a notice, a farewell) comes as "told". The two are never matched up: a
15
- // request's success is nothing to its sender, and the broadcast is no one's answer.
15
+ // request's success is nothing to its sender, and the broadcast is no one's answer. Each element a change leaves
16
+ // changed is told so, once it's in ("changed", bubbling), with who made it (`by`) and by what (`via`, an agent's label).
16
17
  //
17
18
  // What the page's person may do in a document, by the grants and their roles (the server's word, as the write is
18
19
  // held to it), is a `may` attribute, the applier's to write as every attribute of a document's elements is: on a
@@ -20,8 +21,9 @@
20
21
  // "replace:status>being%20made"), so a view, or CSS (`[may~="add"]`), offers only that. None: nothing.
21
22
  //
22
23
  // A socket drops: a phone sleeps, the wifi changes, the server is deployed again. Then the page is
23
- // away (body[data-away]): what it sends is refused at once, so nothing looks saved that isn't, and
24
- // it comes back by itself, each document sent whole again and put in place by key, so only what
24
+ // away (body[data-away]): a write that only adds (a cost, a person) is kept on this device, the page told it's
25
+ // waiting, and sent once it's back (./outbox.ts); anything else it sends is refused at once, so nothing looks saved
26
+ // that isn't, and nothing changed meanwhile is overwritten by an edit made before it. The page comes back by itself, each document sent whole again and put in place by key, so only what
25
27
  // changed while it was away moves. Only the server closing it (a link replaced or taken back, a
26
28
  // member removed) or the door refusing it (not signed in) signs the page out: "signed-out". A socket
27
29
  // can die with no close at all (the browser still calls it open, and nothing gets through): woken,
@@ -32,7 +34,8 @@
32
34
  // person has typed is waiting (a write unanswered, a draft's changes), saying so in the toast meanwhile.
33
35
 
34
36
  import { unsaved } from "./draft.ts";
35
- import { attribute, parts, valueAt, versionKey, type Op, type Type } from "./ops.ts";
37
+ import { attribute, madeBy, parts, valueAt, versionKey, type Op, type Type } from "./ops.ts";
38
+ import { adds, keep, sent, waiting, type Kept } from "./outbox.ts";
36
39
  import { toast } from "./toast.ts";
37
40
  // a display's list kept by key: its own module (joined/keep), here too for what imported it from here
38
41
  export { keep, type Row } from "./keep.ts";
@@ -54,7 +57,8 @@ type Held = {
54
57
  type May = Record<string, string>;
55
58
  /** A request's answer, as its op returns it: why it was refused, or nothing; what it did comes as its change. */
56
59
  export type Answer = string | undefined;
57
- type Answered = (why?: string) => void;
60
+ /** Where a request's answer goes: refused, why; done, nothing, or the version it made (a write's, an undo's). */
61
+ type Answered = (why?: string, version?: number) => void;
58
62
  type Timer = ReturnType<typeof setTimeout> | undefined;
59
63
 
60
64
  /** Each document on the page, and its collections, by name. */
@@ -79,6 +83,10 @@ const socket = {
79
83
  asked: undefined as Timer,
80
84
  /** The documents the page has asked what it missed of: once each, until it's heard. */
81
85
  behind: new Set<string>(),
86
+ /** The documents held on this socket, their subscribe answered: what was kept for them can be sent. */
87
+ ready: new Set<string>(),
88
+ /** Sending what was kept: one at a time. */
89
+ sending: false,
82
90
  /** Told a build it wasn't served by: it reloads, once nothing typed is waiting. */
83
91
  stale: false,
84
92
  };
@@ -140,6 +148,7 @@ export function letGo(el: HTMLElement) {
140
148
  docs.delete(doc);
141
149
  mays.delete(doc);
142
150
  socket.behind.delete(doc);
151
+ socket.ready.delete(doc);
143
152
  delete document.body.dataset[versionKey(doc)];
144
153
  if (isOpen()) socket.ws!.send(JSON.stringify({ unsubscribe: doc }));
145
154
  return;
@@ -153,13 +162,27 @@ export function letGo(el: HTMLElement) {
153
162
  type Part = { doc: string; ops: Op[]; why?: string };
154
163
 
155
164
  // every element's ops, named for its own document, with why if it's said, and where its answer goes; or a write of
156
- // several documents' (writes), sent as it is: the server holds each to the page's documents
165
+ // several documents' (writes), sent as it is: the server holds each to the page's documents. Away, one that only adds
166
+ // is kept for when it's back, its sender answered as it's kept
157
167
  document.addEventListener("ops", (e) => {
158
168
  const { doc, ops, why, writes, answer } = (
159
169
  e as CustomEvent<{ doc: string; ops: Op[]; why?: string; writes?: Part[]; answer?: Answered }>
160
170
  ).detail;
161
- if (writes) send(undefined, { writes }, answer);
162
- else if (docs.has(doc)) send(doc, { ops, why }, answer);
171
+ // a write of several: each part a document the page holds, or one the write makes (board:$1)
172
+ const off = writes?.find((p) => !docs.has(p.doc) && !madeBy(p.doc));
173
+ if (writes && (!writes.length || off)) return answer?.(off ? `${off.doc} isn't on this page` : "a write of nothing");
174
+ if (!isOpen() && (writes ?? [{ ops }]).every((p) => adds(p.ops)) && (writes || docs.has(doc)))
175
+ return (keep(writes ? { writes } : { doc, ops, why }), answer?.());
176
+ // landed, its sender is told the version it made, from the write's own answer (a page's undo keeps it: `undo`)
177
+ const told: Answered = (why, version) => {
178
+ if (why === undefined && version !== undefined)
179
+ e.target!.dispatchEvent(
180
+ new CustomEvent("landed", { bubbles: true, composed: true, detail: { doc: doc ?? writes![0]!.doc, version } }),
181
+ );
182
+ answer?.(why);
183
+ };
184
+ if (writes) send(undefined, { writes }, told);
185
+ else if (docs.has(doc)) send(doc, { ops, why }, told);
163
186
  else answer?.(`${doc} isn't on this page`);
164
187
  });
165
188
 
@@ -174,6 +197,17 @@ export const together = (writes: Part[]) =>
174
197
  export const ask = (doc: string, asked: object) =>
175
198
  new Promise<Answer>((answer) => (docs.has(doc) ? send(doc, asked, answer) : answer(`${doc} isn't on this page`)));
176
199
 
200
+ /** Undo a change of a document the page holds, its own, by the version it made (a write's `landed` says it): the
201
+ * server puts back what it changed, unless something it changed has changed since. Its answer: why it was refused,
202
+ * or the version the undo made, which undone in turn is a redo. What it did comes as the document's change. One
203
+ * document's: a write of several (together) is undone by each part's own version, its first's alone told as landed. */
204
+ export const undo = (doc: string, version: number) =>
205
+ new Promise<{ why?: string; version?: number }>((done) =>
206
+ docs.has(doc)
207
+ ? send(doc, { undo: version }, (why, made) => done(why === undefined ? { version: made } : { why }))
208
+ : done({ why: `${doc} isn't on this page` }),
209
+ );
210
+
177
211
  /** A write or an ask, for a document (none: a write of several, each part naming its own): sent, or, while the page is
178
212
  * away, refused at once. */
179
213
  function send(doc: string | undefined, m: object, answer: Answered = () => {}) {
@@ -182,11 +216,35 @@ function send(doc: string | undefined, m: object, answer: Answered = () => {}) {
182
216
  socket.ws!.send(JSON.stringify({ id: socket.id, doc, ...m }));
183
217
  }
184
218
 
185
- // a document subscribed to again (the page back from away) says the version it has, to be sent only what's since
219
+ // a document subscribed to again (the page back from away) says the version it has, to be sent only what's since;
220
+ // answered, it's held on this socket, and what was kept for it is sent
186
221
  const subscribe = (s: WebSocket, doc: string) => {
187
222
  const had = document.body.dataset[versionKey(doc)];
188
- s.send(JSON.stringify({ subscribe: doc, ...(had ? { since: Number(had) } : {}) }));
223
+ socket.sent.set(++socket.id, {
224
+ answer: (why) => why === undefined && s === socket.ws && (socket.ready.add(doc), flush()),
225
+ });
226
+ s.send(JSON.stringify({ subscribe: doc, id: socket.id, ...(had ? { since: Number(had) } : {}) }));
189
227
  };
228
+
229
+ /** What was kept while the page was away, sent now it's back: oldest first, each once its documents are held again;
230
+ * the signal gone again, the rest wait. One refused is let go, and said. */
231
+ async function flush() {
232
+ // (a document the write makes, board:$1, is held by no page until it's made)
233
+ const ready = (k: Kept) => (k.writes?.map((p) => p.doc) ?? [k.doc!]).every((d) => socket.ready.has(d) || madeBy(d));
234
+ if (socket.sending) return;
235
+ socket.sending = true;
236
+ try {
237
+ for (let k = waiting().find(ready); k && isOpen(); k = waiting().find(ready)) {
238
+ const { id, doc, ops, why, writes } = k;
239
+ const refused = await new Promise<Answer>((answer) => send(doc, writes ? { writes } : { ops, why }, answer));
240
+ if (refused && !isOpen()) break;
241
+ sent(id);
242
+ if (refused) toast(`A change made without signal wasn't kept: ${refused}`, { hold: true });
243
+ }
244
+ } finally {
245
+ socket.sending = false;
246
+ }
247
+ }
190
248
  /** A change missed (a socket's message lost on the way): the page asks what's since the version it has. */
191
249
  function behind(s: WebSocket, doc: string, had: string) {
192
250
  if (socket.behind.has(doc)) return;
@@ -223,7 +281,10 @@ function heard(s: WebSocket, m: any) {
223
281
  /** A change to a document: it follows the version the page has (after it, where the document is told in
224
282
  * parts); one heard already is let go, and one past a change missed waits while the page asks for what it
225
283
  * missed. A change to a document let go, sent before the server heard so, is let go too. */
226
- function changed(s: WebSocket, m: { doc: string; version: number; after?: number; ops: Op[] }) {
284
+ function changed(
285
+ s: WebSocket,
286
+ m: { doc: string; version: number; after?: number; ops: Op[]; by?: number | null; via?: string | null },
287
+ ) {
227
288
  const { doc } = m;
228
289
  if (!docs.has(doc)) return;
229
290
  const had = document.body.dataset[versionKey(doc)],
@@ -231,8 +292,15 @@ function changed(s: WebSocket, m: { doc: string; version: number; after?: number
231
292
  if (!whole && had !== undefined && m.version <= Number(had)) return;
232
293
  if (!whole && had !== undefined && (m.after ?? m.version - 1) !== Number(had)) return behind(s, doc, had);
233
294
  socket.behind.delete(doc);
234
- for (const o of m.ops) apply(docs.get(doc)!, o);
295
+ const touched = new Set(m.ops.map((o) => apply(docs.get(doc)!, o)));
235
296
  document.body.dataset[versionKey(doc)] = String(m.version);
297
+ // each element a change left on the page says who made it, and by what (an agent's label), once the change is in
298
+ if (!whole)
299
+ for (const el of touched)
300
+ el?.isConnected &&
301
+ el.dispatchEvent(
302
+ new CustomEvent("changed", { bubbles: true, detail: { by: m.by ?? null, via: m.via ?? null } }),
303
+ );
236
304
  }
237
305
 
238
306
  /** What the page's person may do in a document, now: on each of its elements. */
@@ -245,7 +313,7 @@ function mayDo({ doc, may }: { doc: string; may: Record<string, May> }) {
245
313
  /** A request's answer: refused, its why to whoever sent it; done, nothing (what it did comes as its change).
246
314
  * A document the page holds that isn't there is told to the element holding it; a refusal no one sent (a
247
315
  * message the server couldn't read) is the page's own mistake. */
248
- function answered(m: { done?: number; refused?: number | true | null; why?: string; doc?: string }) {
316
+ function answered(m: { done?: number; refused?: number | true | null; why?: string; doc?: string; version?: number }) {
249
317
  // a document a stranger may not hold (all but what's anonymous): the page is signed out, as at the door
250
318
  if (m.refused === true && m.why === SIGN_IN) {
251
319
  document.body.toggleAttribute("data-signed-out", true);
@@ -259,7 +327,7 @@ function answered(m: { done?: number; refused?: number | true | null; why?: stri
259
327
  request = socket.sent.get(id);
260
328
  socket.sent.delete(id);
261
329
  if (m.refused !== undefined) return request ? request.answer(m.why) : console.error(`joined: refused, ${m.why}`);
262
- request?.answer();
330
+ request?.answer(undefined, m.version);
263
331
  }
264
332
 
265
333
  /** The server's build, told as the socket opens: the one the page was served by (<body data-build>), or another,
@@ -277,6 +345,7 @@ function built(build: string) {
277
345
  /** The socket gone: the page signed out, if that's why, or away, to come back by itself. */
278
346
  async function gone(code: number) {
279
347
  socket.asked = void clearTimeout(socket.asked);
348
+ socket.ready.clear();
280
349
  // what was sent and not answered may or may not have landed: when it's back, the page shows which
281
350
  for (const [id, { answer }] of socket.sent)
282
351
  (socket.sent.delete(id), answer("The connection dropped: look again once it's back."));
@@ -324,7 +393,8 @@ document.addEventListener("visibilitychange", () => document.visibilityState ===
324
393
 
325
394
  // applying a change
326
395
 
327
- function apply(held: Map<string, Held>, o: Op) {
396
+ /** An op put on the page: the element it left changed (an item, or a document's own), if one's still there. */
397
+ function apply(held: Map<string, Held>, o: Op): Element | undefined {
328
398
  if (o.path === "")
329
399
  return void held.forEach((h, coll) => whole(h, h.own ? [{ ...o.value, id: 1 }] : (o.value[coll] ?? [])));
330
400
  // a field of the document's own (/greeting): its own element's
@@ -351,6 +421,7 @@ function apply(held: Map<string, Held>, o: Op) {
351
421
  } else if (o.op === "add") placed(h, element(h, o.value));
352
422
  else if (o.op === "remove") field ? el?.removeAttribute(field) : (el?.remove(), h.items.delete(key!));
353
423
  else if (el) (set(el, h.fields[field!], field!, o.value), field === h.order && placed(h, el));
424
+ return h.items.get(o.op === "add" && field === undefined ? String(o.value.id) : key!);
354
425
  }
355
426
 
356
427
  /** An item put where it goes: in its collection's order, by the field (or, without one, its key) and then its key;