@blueshed/joined 0.1.19 → 0.2.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 (71) hide show
  1. package/.claude/skills/joined/SKILL.md +1 -1
  2. package/.claude/skills/joined/reference.md +96 -17
  3. package/docs/design.md +31 -8
  4. package/examples/cafe/kitchen.ts +2 -2
  5. package/examples/desk/card.ts +43 -0
  6. package/examples/desk/desk.css +17 -0
  7. package/examples/desk/desk.json +41 -0
  8. package/examples/desk/generated/elements.ts +69 -0
  9. package/examples/desk/generated/queries.ts +33 -0
  10. package/examples/desk/generated/rules.ts +94 -0
  11. package/examples/desk/index.html +34 -0
  12. package/examples/desk/lanes.css +126 -0
  13. package/examples/desk/lanes.ts +133 -0
  14. package/examples/desk/migrations/001-desk.ts +8 -0
  15. package/examples/desk/page.ts +29 -0
  16. package/examples/desk/pulse.ts +20 -0
  17. package/examples/desk/routes.ts +9 -0
  18. package/examples/desk/steps.ts +32 -0
  19. package/examples/desk/stories.jsonl +18 -0
  20. package/examples/desk/tally.css +62 -0
  21. package/examples/desk/tally.ts +76 -0
  22. package/examples/desk/thumbs.ts +45 -0
  23. package/examples/desk/trust.ts +24 -0
  24. package/examples/desk/words.ts +17 -0
  25. package/examples/trip/bar.ts +1 -1
  26. package/examples/trip/calendar-map.css +2 -0
  27. package/examples/trip/drags.ts +3 -3
  28. package/examples/trip/drawer.ts +5 -5
  29. package/examples/trip/event-form.ts +8 -7
  30. package/examples/trip/holding.ts +1 -1
  31. package/examples/trip/ics.ts +22 -112
  32. package/examples/trip/line-stops.ts +1 -1
  33. package/examples/trip/line-words.ts +1 -1
  34. package/examples/trip/line.ts +1 -1
  35. package/examples/trip/map.ts +1 -1
  36. package/examples/trip/month.ts +1 -1
  37. package/examples/trip/other-weeks.ts +1 -1
  38. package/examples/trip/page.ts +2 -2
  39. package/examples/trip/peek.ts +1 -1
  40. package/examples/trip/routes.ts +3 -3
  41. package/examples/trip/shell.ts +3 -3
  42. package/examples/trip/stays.ts +1 -1
  43. package/examples/trip/week.ts +1 -1
  44. package/examples/trip/writes.ts +1 -1
  45. package/examples/trips/card.ts +1 -1
  46. package/examples/trips/map.ts +1 -1
  47. package/examples/weeks/derive.ts +1 -1
  48. package/package.json +11 -1
  49. package/src/attachment.ts +7 -2
  50. package/src/demo.css +29 -26
  51. package/src/demo.ts +2 -2
  52. package/src/draft.ts +23 -8
  53. package/src/files.ts +24 -8
  54. package/{examples/trip/mercator.ts → src/geo.ts} +13 -3
  55. package/src/icon-names.ts +4 -0
  56. package/src/ics.ts +130 -0
  57. package/src/members/panel.css +208 -201
  58. package/src/members/panel.ts +17 -8
  59. package/src/outbox.ts +70 -0
  60. package/src/page.css +76 -70
  61. package/src/page.ts +79 -14
  62. package/src/parts.css +575 -428
  63. package/src/parts.ts +18 -5
  64. package/{examples/trip → src}/place-search.ts +5 -5
  65. package/{examples/trip → src}/places.ts +9 -7
  66. package/src/rank.ts +20 -0
  67. package/src/scrim.ts +81 -0
  68. package/src/server.ts +28 -1
  69. package/{examples/trip → src}/time.ts +37 -5
  70. package/src/tokens.css +5 -0
  71. package/src/tokens.json +35 -0
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,
@@ -33,6 +35,7 @@
33
35
 
34
36
  import { unsaved } from "./draft.ts";
35
37
  import { attribute, 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
  };
@@ -153,13 +161,24 @@ export function letGo(el: HTMLElement) {
153
161
  type Part = { doc: string; ops: Op[]; why?: string };
154
162
 
155
163
  // 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
164
+ // several documents' (writes), sent as it is: the server holds each to the page's documents. Away, one that only adds
165
+ // is kept for when it's back, its sender answered as it's kept
157
166
  document.addEventListener("ops", (e) => {
158
167
  const { doc, ops, why, writes, answer } = (
159
168
  e as CustomEvent<{ doc: string; ops: Op[]; why?: string; writes?: Part[]; answer?: Answered }>
160
169
  ).detail;
161
- if (writes) send(undefined, { writes }, answer);
162
- else if (docs.has(doc)) send(doc, { ops, why }, answer);
170
+ if (!isOpen() && (writes ?? [{ ops }]).every((p) => adds(p.ops)) && (writes || docs.has(doc)))
171
+ return (keep(writes ? { writes } : { doc, ops, why }), answer?.());
172
+ // landed, its sender is told the version it made, from the write's own answer (a page's undo keeps it: `undo`)
173
+ const told: Answered = (why, version) => {
174
+ if (why === undefined && version !== undefined)
175
+ e.target!.dispatchEvent(
176
+ new CustomEvent("landed", { bubbles: true, composed: true, detail: { doc: doc ?? writes![0]!.doc, version } }),
177
+ );
178
+ answer?.(why);
179
+ };
180
+ if (writes) send(undefined, { writes }, told);
181
+ else if (docs.has(doc)) send(doc, { ops, why }, told);
163
182
  else answer?.(`${doc} isn't on this page`);
164
183
  });
165
184
 
@@ -174,6 +193,16 @@ export const together = (writes: Part[]) =>
174
193
  export const ask = (doc: string, asked: object) =>
175
194
  new Promise<Answer>((answer) => (docs.has(doc) ? send(doc, asked, answer) : answer(`${doc} isn't on this page`)));
176
195
 
196
+ /** Undo a change of a document the page holds, its own, by the version it made (a write's `landed` says it): the
197
+ * server puts back what it changed, unless something it changed has changed since. Its answer: why it was refused,
198
+ * or the version the undo made, which undone in turn is a redo. What it did comes as the document's change. */
199
+ export const undo = (doc: string, version: number) =>
200
+ new Promise<{ why?: string; version?: number }>((done) =>
201
+ docs.has(doc)
202
+ ? send(doc, { undo: version }, (why, made) => done(why === undefined ? { version: made } : { why }))
203
+ : done({ why: `${doc} isn't on this page` }),
204
+ );
205
+
177
206
  /** 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
207
  * away, refused at once. */
179
208
  function send(doc: string | undefined, m: object, answer: Answered = () => {}) {
@@ -182,11 +211,34 @@ function send(doc: string | undefined, m: object, answer: Answered = () => {}) {
182
211
  socket.ws!.send(JSON.stringify({ id: socket.id, doc, ...m }));
183
212
  }
184
213
 
185
- // a document subscribed to again (the page back from away) says the version it has, to be sent only what's since
214
+ // a document subscribed to again (the page back from away) says the version it has, to be sent only what's since;
215
+ // answered, it's held on this socket, and what was kept for it is sent
186
216
  const subscribe = (s: WebSocket, doc: string) => {
187
217
  const had = document.body.dataset[versionKey(doc)];
188
- s.send(JSON.stringify({ subscribe: doc, ...(had ? { since: Number(had) } : {}) }));
218
+ socket.sent.set(++socket.id, {
219
+ answer: (why) => why === undefined && s === socket.ws && (socket.ready.add(doc), flush()),
220
+ });
221
+ s.send(JSON.stringify({ subscribe: doc, id: socket.id, ...(had ? { since: Number(had) } : {}) }));
189
222
  };
223
+
224
+ /** What was kept while the page was away, sent now it's back: oldest first, each once its documents are held again;
225
+ * the signal gone again, the rest wait. One refused is let go, and said. */
226
+ async function flush() {
227
+ const ready = (k: Kept) => (k.writes?.map((p) => p.doc) ?? [k.doc!]).every((d) => socket.ready.has(d));
228
+ if (socket.sending) return;
229
+ socket.sending = true;
230
+ try {
231
+ for (let k = waiting().find(ready); k && isOpen(); k = waiting().find(ready)) {
232
+ const { id, doc, ops, why, writes } = k;
233
+ const refused = await new Promise<Answer>((answer) => send(doc, writes ? { writes } : { ops, why }, answer));
234
+ if (refused && !isOpen()) break;
235
+ sent(id);
236
+ if (refused) toast(`A change made without signal wasn't kept: ${refused}`, { hold: true });
237
+ }
238
+ } finally {
239
+ socket.sending = false;
240
+ }
241
+ }
190
242
  /** A change missed (a socket's message lost on the way): the page asks what's since the version it has. */
191
243
  function behind(s: WebSocket, doc: string, had: string) {
192
244
  if (socket.behind.has(doc)) return;
@@ -223,7 +275,10 @@ function heard(s: WebSocket, m: any) {
223
275
  /** A change to a document: it follows the version the page has (after it, where the document is told in
224
276
  * parts); one heard already is let go, and one past a change missed waits while the page asks for what it
225
277
  * 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[] }) {
278
+ function changed(
279
+ s: WebSocket,
280
+ m: { doc: string; version: number; after?: number; ops: Op[]; by?: number | null; via?: string | null },
281
+ ) {
227
282
  const { doc } = m;
228
283
  if (!docs.has(doc)) return;
229
284
  const had = document.body.dataset[versionKey(doc)],
@@ -231,8 +286,15 @@ function changed(s: WebSocket, m: { doc: string; version: number; after?: number
231
286
  if (!whole && had !== undefined && m.version <= Number(had)) return;
232
287
  if (!whole && had !== undefined && (m.after ?? m.version - 1) !== Number(had)) return behind(s, doc, had);
233
288
  socket.behind.delete(doc);
234
- for (const o of m.ops) apply(docs.get(doc)!, o);
289
+ const touched = new Set(m.ops.map((o) => apply(docs.get(doc)!, o)));
235
290
  document.body.dataset[versionKey(doc)] = String(m.version);
291
+ // each element a change left on the page says who made it, and by what (an agent's label), once the change is in
292
+ if (!whole)
293
+ for (const el of touched)
294
+ el?.isConnected &&
295
+ el.dispatchEvent(
296
+ new CustomEvent("changed", { bubbles: true, detail: { by: m.by ?? null, via: m.via ?? null } }),
297
+ );
236
298
  }
237
299
 
238
300
  /** What the page's person may do in a document, now: on each of its elements. */
@@ -245,7 +307,7 @@ function mayDo({ doc, may }: { doc: string; may: Record<string, May> }) {
245
307
  /** A request's answer: refused, its why to whoever sent it; done, nothing (what it did comes as its change).
246
308
  * A document the page holds that isn't there is told to the element holding it; a refusal no one sent (a
247
309
  * 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 }) {
310
+ function answered(m: { done?: number; refused?: number | true | null; why?: string; doc?: string; version?: number }) {
249
311
  // a document a stranger may not hold (all but what's anonymous): the page is signed out, as at the door
250
312
  if (m.refused === true && m.why === SIGN_IN) {
251
313
  document.body.toggleAttribute("data-signed-out", true);
@@ -259,7 +321,7 @@ function answered(m: { done?: number; refused?: number | true | null; why?: stri
259
321
  request = socket.sent.get(id);
260
322
  socket.sent.delete(id);
261
323
  if (m.refused !== undefined) return request ? request.answer(m.why) : console.error(`joined: refused, ${m.why}`);
262
- request?.answer();
324
+ request?.answer(undefined, m.version);
263
325
  }
264
326
 
265
327
  /** The server's build, told as the socket opens: the one the page was served by (<body data-build>), or another,
@@ -277,6 +339,7 @@ function built(build: string) {
277
339
  /** The socket gone: the page signed out, if that's why, or away, to come back by itself. */
278
340
  async function gone(code: number) {
279
341
  socket.asked = void clearTimeout(socket.asked);
342
+ socket.ready.clear();
280
343
  // what was sent and not answered may or may not have landed: when it's back, the page shows which
281
344
  for (const [id, { answer }] of socket.sent)
282
345
  (socket.sent.delete(id), answer("The connection dropped: look again once it's back."));
@@ -324,7 +387,8 @@ document.addEventListener("visibilitychange", () => document.visibilityState ===
324
387
 
325
388
  // applying a change
326
389
 
327
- function apply(held: Map<string, Held>, o: Op) {
390
+ /** An op put on the page: the element it left changed (an item, or a document's own), if one's still there. */
391
+ function apply(held: Map<string, Held>, o: Op): Element | undefined {
328
392
  if (o.path === "")
329
393
  return void held.forEach((h, coll) => whole(h, h.own ? [{ ...o.value, id: 1 }] : (o.value[coll] ?? [])));
330
394
  // a field of the document's own (/greeting): its own element's
@@ -351,6 +415,7 @@ function apply(held: Map<string, Held>, o: Op) {
351
415
  } else if (o.op === "add") placed(h, element(h, o.value));
352
416
  else if (o.op === "remove") field ? el?.removeAttribute(field) : (el?.remove(), h.items.delete(key!));
353
417
  else if (el) (set(el, h.fields[field!], field!, o.value), field === h.order && placed(h, el));
418
+ return h.items.get(o.op === "add" && field === undefined ? String(o.value.id) : key!);
354
419
  }
355
420
 
356
421
  /** An item put where it goes: in its collection's order, by the field (or, without one, its key) and then its key;