@blueshed/joined 0.0.0-stage → 0.1.19
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.
- package/.claude/skills/joined/SKILL.md +17 -0
- package/.claude/skills/joined/reference.md +768 -0
- package/LICENSE +19 -0
- package/README.md +115 -2
- package/docs/design.md +148 -0
- package/docs/introduction.md +193 -0
- package/docs/learning.md +76 -0
- package/docs/stories.md +82 -0
- package/examples/app.css +27 -0
- package/examples/cafe/books.html +38 -0
- package/examples/cafe/books.json +13 -0
- package/examples/cafe/books.ts +64 -0
- package/examples/cafe/cafe.css +512 -0
- package/examples/cafe/front.html +115 -0
- package/examples/cafe/front.ts +7 -0
- package/examples/cafe/generated/books.elements.ts +52 -0
- package/examples/cafe/generated/books.queries.ts +11 -0
- package/examples/cafe/generated/books.rules.ts +42 -0
- package/examples/cafe/generated/kitchen.elements.ts +87 -0
- package/examples/cafe/generated/kitchen.queries.ts +47 -0
- package/examples/cafe/generated/kitchen.rules.ts +98 -0
- package/examples/cafe/generated/menu.elements.ts +61 -0
- package/examples/cafe/generated/menu.queries.ts +28 -0
- package/examples/cafe/generated/menu.rules.ts +65 -0
- package/examples/cafe/generated/order.elements.ts +87 -0
- package/examples/cafe/generated/order.queries.ts +47 -0
- package/examples/cafe/generated/order.rules.ts +95 -0
- package/examples/cafe/generated/staff.elements.ts +55 -0
- package/examples/cafe/generated/staff.queries.ts +27 -0
- package/examples/cafe/generated/staff.rules.ts +59 -0
- package/examples/cafe/generated/supplies.elements.ts +62 -0
- package/examples/cafe/generated/supplies.queries.ts +43 -0
- package/examples/cafe/generated/supplies.rules.ts +84 -0
- package/examples/cafe/generated/till.elements.ts +93 -0
- package/examples/cafe/generated/till.queries.ts +57 -0
- package/examples/cafe/generated/till.rules.ts +113 -0
- package/examples/cafe/index.html +70 -0
- package/examples/cafe/kitchen.css +107 -0
- package/examples/cafe/kitchen.html +39 -0
- package/examples/cafe/kitchen.ts +125 -0
- package/examples/cafe/menu.html +55 -0
- package/examples/cafe/menu.json +41 -0
- package/examples/cafe/menu.ts +84 -0
- package/examples/cafe/migrations/001-cafe.ts +11 -0
- package/examples/cafe/order.html +37 -0
- package/examples/cafe/order.ts +45 -0
- package/examples/cafe/rail-drag.ts +30 -0
- package/examples/cafe/routes.ts +32 -0
- package/examples/cafe/staff.json +20 -0
- package/examples/cafe/stories.jsonl +43 -0
- package/examples/cafe/supplies.html +77 -0
- package/examples/cafe/supplies.json +33 -0
- package/examples/cafe/supplies.ts +170 -0
- package/examples/cafe/till.json +95 -0
- package/examples/cafe/till.ts +122 -0
- package/examples/cafe/trust.ts +20 -0
- package/examples/cafe/views.ts +171 -0
- package/examples/docs.ts +28 -0
- package/examples/film.jpg +0 -0
- package/examples/hello/generated/elements.ts +45 -0
- package/examples/hello/generated/queries.ts +24 -0
- package/examples/hello/generated/rules.ts +45 -0
- package/examples/hello/hello.css +27 -0
- package/examples/hello/hello.json +1 -0
- package/examples/hello/index.html +32 -0
- package/examples/hello/migrations/001-hello.ts +6 -0
- package/examples/hello/page.ts +49 -0
- package/examples/hello/routes.ts +6 -0
- package/examples/hello/stories.jsonl +3 -0
- package/examples/index.html +146 -0
- package/examples/page.ts +11 -0
- package/examples/people/adding.ts +20 -0
- package/examples/people/generated/elements.ts +68 -0
- package/examples/people/generated/queries.ts +28 -0
- package/examples/people/generated/rules.ts +85 -0
- package/examples/people/links.ts +124 -0
- package/examples/people/migrations/001-people.ts +6 -0
- package/examples/people/migrations/002-add-people-member-and-guard-people.ts +7 -0
- package/examples/people/migrations/003-add-people-rank-and-guard-people.ts +7 -0
- package/examples/people/order.ts +61 -0
- package/examples/people/people.css +65 -0
- package/examples/people/people.json +26 -0
- package/examples/people/routes.ts +12 -0
- package/examples/people/stories.jsonl +20 -0
- package/examples/people/trust.ts +14 -0
- package/examples/people/view.ts +115 -0
- package/examples/routes.ts +11 -0
- package/examples/trip/answer-rows.ts +70 -0
- package/examples/trip/answers.ts +11 -0
- package/examples/trip/bar.ts +89 -0
- package/examples/trip/calendar-map.css +709 -0
- package/examples/trip/calendar-map.ts +249 -0
- package/examples/trip/drags.ts +143 -0
- package/examples/trip/drawer.ts +250 -0
- package/examples/trip/event-form.ts +199 -0
- package/examples/trip/generated/elements.ts +144 -0
- package/examples/trip/generated/queries.ts +46 -0
- package/examples/trip/generated/rules.ts +239 -0
- package/examples/trip/holding.ts +61 -0
- package/examples/trip/ics.ts +117 -0
- package/examples/trip/index.html +82 -0
- package/examples/trip/line-stops.ts +92 -0
- package/examples/trip/line-words.ts +79 -0
- package/examples/trip/line.ts +121 -0
- package/examples/trip/map.ts +234 -0
- package/examples/trip/mercator.ts +28 -0
- package/examples/trip/migrations/001-trip.ts +7 -0
- package/examples/trip/migrations/002-add-events-zone-and-guard-events.ts +7 -0
- package/examples/trip/migrations/003-guard-events-and-create-events_asked-and-more.ts +9 -0
- package/examples/trip/migrations/004-create-stays.ts +6 -0
- package/examples/trip/migrations/005-guard-events-and-create-events_arrives-and-more.ts +8 -0
- package/examples/trip/month.ts +87 -0
- package/examples/trip/other-weeks.ts +67 -0
- package/examples/trip/page.ts +121 -0
- package/examples/trip/peek.ts +71 -0
- package/examples/trip/people-chips.ts +74 -0
- package/examples/trip/place-search.ts +51 -0
- package/examples/trip/places.ts +34 -0
- package/examples/trip/routes.ts +27 -0
- package/examples/trip/shell.ts +39 -0
- package/examples/trip/stays.css +51 -0
- package/examples/trip/stays.ts +68 -0
- package/examples/trip/stories.jsonl +72 -0
- package/examples/trip/time.ts +46 -0
- package/examples/trip/told.ts +48 -0
- package/examples/trip/trip.json +69 -0
- package/examples/trip/trust.ts +51 -0
- package/examples/trip/week.ts +237 -0
- package/examples/trip/words.ts +4 -0
- package/examples/trip/writes.ts +91 -0
- package/examples/trip/zones.ts +16 -0
- package/examples/trips/card.ts +149 -0
- package/examples/trips/generated/elements.ts +60 -0
- package/examples/trips/generated/queries.ts +28 -0
- package/examples/trips/generated/rules.ts +62 -0
- package/examples/trips/index.html +47 -0
- package/examples/trips/map.ts +52 -0
- package/examples/trips/migrations/001-trips.ts +8 -0
- package/examples/trips/new-trip.ts +46 -0
- package/examples/trips/page.ts +9 -0
- package/examples/trips/routes.ts +7 -0
- package/examples/trips/stories.jsonl +12 -0
- package/examples/trips/trips.css +183 -0
- package/examples/trips/trips.json +7 -0
- package/examples/weeks/derive.ts +23 -0
- package/examples/weeks/generated/elements.ts +52 -0
- package/examples/weeks/generated/rules.ts +33 -0
- package/examples/weeks/routes.ts +4 -0
- package/examples/weeks/weeks.json +1 -0
- package/examples/welcome.css +299 -0
- package/package.json +51 -4
- package/src/attachment.ts +70 -0
- package/src/cli.ts +52 -0
- package/src/client.ts +98 -0
- package/src/compile.ts +2034 -0
- package/src/css.d.ts +5 -0
- package/src/definitions.ts +268 -0
- package/src/demo.css +29 -0
- package/src/demo.ts +42 -0
- package/src/draft.ts +69 -0
- package/src/engine.ts +288 -0
- package/src/file-value.ts +13 -0
- package/src/files.ts +187 -0
- package/src/follow.ts +56 -0
- package/src/hues.ts +3 -0
- package/src/icon-names.ts +77 -0
- package/src/icon.png +0 -0
- package/src/icons.ts +32 -0
- package/src/keep.ts +33 -0
- package/src/link.ts +31 -0
- package/src/mcp.ts +252 -0
- package/src/members/demo.ts +53 -0
- package/src/members/generated/elements.ts +68 -0
- package/src/members/generated/queries.ts +29 -0
- package/src/members/generated/rules.ts +65 -0
- package/src/members/group.ts +294 -0
- package/src/members/keys.ts +221 -0
- package/src/members/members.json +9 -0
- package/src/members/migrations/001-members.ts +6 -0
- package/src/members/migrations/002-guard-members.ts +11 -0
- package/src/members/migrations/003-add-members-act-and-guard-members.ts +7 -0
- package/src/members/panel.css +208 -0
- package/src/members/panel.ts +398 -0
- package/src/members/routes.ts +4 -0
- package/src/members/stories.jsonl +27 -0
- package/src/members/trust.ts +116 -0
- package/src/migrate.ts +597 -0
- package/src/migrations/001-joined.ts +13 -0
- package/src/migrations/002-group-keys.ts +16 -0
- package/src/migrations/003-change-inverse.ts +10 -0
- package/src/migrations/004-change-followed.ts +6 -0
- package/src/ops.ts +61 -0
- package/src/page.css +79 -0
- package/src/page.ts +413 -0
- package/src/parts.css +471 -0
- package/src/parts.ts +152 -0
- package/src/server.ts +862 -0
- package/src/settings.ts +94 -0
- package/src/starter.ts +89 -0
- package/src/store.ts +815 -0
- package/src/stories.ts +387 -0
- package/src/toast.ts +52 -0
- package/src/together.ts +167 -0
- package/src/tokens.css +85 -0
- package/src/tokens.json +457 -0
- package/src/upkeep.ts +86 -0
- package/src/visitors.ts +43 -0
- package/src/write.ts +567 -0
- package/starter/CLAUDE.md +37 -0
- package/starter/gitignore +3 -0
- package/starter/tests/hello-page.test.ts +14 -0
- package/starter/tests/hello.test.ts +8 -0
- package/starter/tsconfig.json +17 -0
package/src/ops.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// An op on a path, as joined's wire carries it (RFC 6902's add, replace and remove), and its path's
|
|
2
|
+
// parts: /events/3/start is the collection events, the item keyed 3, and its field start; in a list of ids,
|
|
3
|
+
// /events/3/people/2 is the link to 2 (and /events/3/people/- one added).
|
|
4
|
+
|
|
5
|
+
export type Op = { op: "add" | "replace" | "remove"; path: string; value?: any };
|
|
6
|
+
|
|
7
|
+
/** A field's type, read off what the stories give it: a string, or one read off its form (a date-time, a time
|
|
8
|
+
* zone, an instant, a duration, a place); a boolean; a number, an integer or a decimal; or a list of ids. */
|
|
9
|
+
export type Type =
|
|
10
|
+
| "string"
|
|
11
|
+
| "date-time"
|
|
12
|
+
| "zone"
|
|
13
|
+
| "instant"
|
|
14
|
+
| "duration"
|
|
15
|
+
| "place"
|
|
16
|
+
| "file"
|
|
17
|
+
| "boolean"
|
|
18
|
+
| "integer"
|
|
19
|
+
| "decimal"
|
|
20
|
+
| "ids";
|
|
21
|
+
|
|
22
|
+
/** A write, committed: its document, the version it made, its ops as applied, and who wrote it (and by
|
|
23
|
+
* which agent's link): what every page holding the document is sent. */
|
|
24
|
+
export type Change = { doc: string; version: number; ops: Op[]; by: number | null; via: string | null };
|
|
25
|
+
|
|
26
|
+
/** A field's value as its item's attribute, as the server writes it into a page and the page's applier sets it: a
|
|
27
|
+
* boolean is there or not (""), a list of ids its ids with a space between, none nothing (null); else its text. */
|
|
28
|
+
export const attribute = (type: Type | undefined, value: unknown): string | null =>
|
|
29
|
+
type === "boolean"
|
|
30
|
+
? value === true
|
|
31
|
+
? ""
|
|
32
|
+
: null
|
|
33
|
+
: value === undefined || value === null
|
|
34
|
+
? null
|
|
35
|
+
: Array.isArray(value)
|
|
36
|
+
? value.join(" ")
|
|
37
|
+
: String(value);
|
|
38
|
+
|
|
39
|
+
/** A field's value in an item, as a document holds it: one of an object's fields (`terms.days`), in its object. */
|
|
40
|
+
export const valueAt = (item: Record<string, unknown>, field: string): unknown => {
|
|
41
|
+
const [f, sub] = field.split(".") as [string, string?];
|
|
42
|
+
return sub === undefined ? item[f] : (item[f] as Record<string, unknown> | undefined)?.[sub];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Where a page keeps a document's version on <body>, as a data attribute may be named: a window's name with its
|
|
46
|
+
* parameters joined, anything but a letter or a digit an underscore (`data-trip_week_2026_11_02`). */
|
|
47
|
+
export const versionKey = (doc: string) => doc.replace(/[^a-zA-Z0-9]/g, "_");
|
|
48
|
+
|
|
49
|
+
/** A document's name in its parts: the document it is (`shelf:1`: an instance of a kind, by its key), its kind's
|
|
50
|
+
* name or its own (`shelf`), its key, and its window's parameters (`trip?week=2026-11-02`), as a topic names it. */
|
|
51
|
+
export function docParts(doc: string) {
|
|
52
|
+
const [base = "", query] = doc.split("?") as [string, string?];
|
|
53
|
+
const [name = "", key] = base.split(":");
|
|
54
|
+
return { base, name, key, params: Object.fromEntries(new URLSearchParams(query ?? "")) as Record<string, string> };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 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
|
+
export function parts(path: string) {
|
|
59
|
+
const [, coll = "", key = "", field, link, ...more] = path.split("/");
|
|
60
|
+
return { coll, key, field, link, more };
|
|
61
|
+
}
|
package/src/page.css
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/* joined's page (joined/page.css), linked by every page after joined/tokens.css and before its own: every
|
|
2
|
+
page is laid out alike, its title, its note, one bar (the group, then the page's own buttons,
|
|
3
|
+
.members-only), then <main>, its documents, which a signed-out page hides and an away one dims. Where a
|
|
4
|
+
refusal is said, #said. Never a value by hand: the tokens'. */
|
|
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 {
|
|
42
|
+
font: var(--font-body);
|
|
43
|
+
}
|
|
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
|
+
|
|
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);
|
|
79
|
+
}
|
package/src/page.ts
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
// The page's side of its documents. A collection's compiled element (<j-events>) holds its
|
|
2
|
+
// document by being on the page: when it connects it calls hold(), and the page's socket subscribes
|
|
3
|
+
// to the document, which gives the element its items. The socket is the page's, one whatever it
|
|
4
|
+
// holds: a browser keeps few connections to a host, and every tab is one more. Taken off the page,
|
|
5
|
+
// the element lets the document go, and the page is told it no more. An op's path names
|
|
6
|
+
// an item's element inside its collection by key: add makes one, replace sets an attribute, remove
|
|
7
|
+
// takes it away (or, at a field, clears its attribute). A list of ids is an attribute of them
|
|
8
|
+
// ("1 3"), as HTML's IDREFS are. Ops an element sends go on the socket, named for its document;
|
|
9
|
+
// what comes back is the only thing applied. Two routes, each with its own job: a request's answer
|
|
10
|
+
// matters only when it's refused, and only to whoever sent it, so a write or an ask returns its refusal's
|
|
11
|
+
// why, or nothing; what a write did comes on the broadcast, as every change does, to every page holding the
|
|
12
|
+
// document, the writer's own included. A document the page holds that isn't there is told to the element
|
|
13
|
+
// holding it (a "refused" event on it). What the server tells a page of its own (who it is, the links it
|
|
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.
|
|
16
|
+
//
|
|
17
|
+
// 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
|
+
// held to it), is a `may` attribute, the applier's to write as every attribute of a document's elements is: on a
|
|
19
|
+
// collection's element what may be added ("add"), on an item's what may be done to it ("remove replace:price",
|
|
20
|
+
// "replace:status>being%20made"), so a view, or CSS (`[may~="add"]`), offers only that. None: nothing.
|
|
21
|
+
//
|
|
22
|
+
// 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
|
|
25
|
+
// changed while it was away moves. Only the server closing it (a link replaced or taken back, a
|
|
26
|
+
// member removed) or the door refusing it (not signed in) signs the page out: "signed-out". A socket
|
|
27
|
+
// can die with no close at all (the browser still calls it open, and nothing gets through): woken,
|
|
28
|
+
// or its network back, the page asks it, and one that doesn't answer is let go, and the page is away.
|
|
29
|
+
//
|
|
30
|
+
// Each socket is told the server's build first. A page served by another (a deploy, a restart with the stories
|
|
31
|
+
// compiled anew) would run its elements against rules the server no longer holds: it reloads, once nothing a
|
|
32
|
+
// person has typed is waiting (a write unanswered, a draft's changes), saying so in the toast meanwhile.
|
|
33
|
+
|
|
34
|
+
import { unsaved } from "./draft.ts";
|
|
35
|
+
import { attribute, parts, valueAt, versionKey, type Op, type Type } from "./ops.ts";
|
|
36
|
+
import { toast } from "./toast.ts";
|
|
37
|
+
// a display's list kept by key: its own module (joined/keep), here too for what imported it from here
|
|
38
|
+
export { keep, type Row } from "./keep.ts";
|
|
39
|
+
|
|
40
|
+
type Fields = { readonly [field: string]: Type };
|
|
41
|
+
/** A collection's element on the page, and its items by key; or a document's own element (<j-hello>), its one item. */
|
|
42
|
+
type Held = {
|
|
43
|
+
el: HTMLElement;
|
|
44
|
+
tag: string;
|
|
45
|
+
fields: Fields;
|
|
46
|
+
items: Map<string, Element>;
|
|
47
|
+
own?: boolean;
|
|
48
|
+
/** The field its items are in order of (definitions' order), then by key; none, as they came. */
|
|
49
|
+
order?: string;
|
|
50
|
+
/** What the page's person may do, by key ("": the collection's element). */
|
|
51
|
+
may: May;
|
|
52
|
+
};
|
|
53
|
+
/** What the page's person may do in a collection, as the server says: each element's `may`, by its key. */
|
|
54
|
+
type May = Record<string, string>;
|
|
55
|
+
/** A request's answer, as its op returns it: why it was refused, or nothing; what it did comes as its change. */
|
|
56
|
+
export type Answer = string | undefined;
|
|
57
|
+
type Answered = (why?: string) => void;
|
|
58
|
+
type Timer = ReturnType<typeof setTimeout> | undefined;
|
|
59
|
+
|
|
60
|
+
/** Each document on the page, and its collections, by name. */
|
|
61
|
+
const docs = new Map<string, Map<string, Held>>();
|
|
62
|
+
/** What the page's person may do in each document it holds, by collection, as the server last said. */
|
|
63
|
+
const mays = new Map<string, Record<string, May>>();
|
|
64
|
+
/** Collections let go while another of their document kept it held (the trip's events, its stays staying): put back,
|
|
65
|
+
* each holds none of it, so it's asked for whole. */
|
|
66
|
+
const away = new Set<string>();
|
|
67
|
+
|
|
68
|
+
/** The page's one socket, and what it's in the middle of. */
|
|
69
|
+
const socket = {
|
|
70
|
+
ws: undefined as WebSocket | undefined,
|
|
71
|
+
/** Each write or ask not yet answered, by its id: where its answer goes. */
|
|
72
|
+
sent: new Map<number, { answer: Answered }>(),
|
|
73
|
+
/** The last id given: 0 is never a write's, it's the page asking whether the socket's there. */
|
|
74
|
+
id: 0,
|
|
75
|
+
/** How long until the page tries again, while it's away: a second, doubling to half a minute. */
|
|
76
|
+
wait: 1000,
|
|
77
|
+
again: undefined as Timer,
|
|
78
|
+
/** The socket asked, on waking, and not yet answered. */
|
|
79
|
+
asked: undefined as Timer,
|
|
80
|
+
/** The documents the page has asked what it missed of: once each, until it's heard. */
|
|
81
|
+
behind: new Set<string>(),
|
|
82
|
+
/** Told a build it wasn't served by: it reloads, once nothing typed is waiting. */
|
|
83
|
+
stale: false,
|
|
84
|
+
};
|
|
85
|
+
/** Why a stranger's page is refused a document, the server's words. */
|
|
86
|
+
const SIGN_IN = "sign in with your link first";
|
|
87
|
+
/** How long a socket asked, on waking, has to answer before it's taken as gone. */
|
|
88
|
+
const ANSWER = 5000;
|
|
89
|
+
const isOpen = () => socket.ws?.readyState === WebSocket.OPEN;
|
|
90
|
+
|
|
91
|
+
// holding a document
|
|
92
|
+
|
|
93
|
+
/** A collection's element holds its document: its items are put inside it, and kept so. */
|
|
94
|
+
export function hold(
|
|
95
|
+
el: HTMLElement,
|
|
96
|
+
doc: string,
|
|
97
|
+
collection: string,
|
|
98
|
+
tag: string,
|
|
99
|
+
fields: Fields,
|
|
100
|
+
own = false,
|
|
101
|
+
order?: string,
|
|
102
|
+
) {
|
|
103
|
+
// put back after its document's other collection kept it (or after that went too): it holds none of it, so the
|
|
104
|
+
// document's asked for whole, with no version, once (now, or on opening)
|
|
105
|
+
const back = away.delete(`${doc} ${collection}`);
|
|
106
|
+
if (!docs.has(doc)) (docs.set(doc, new Map()), isOpen() && subscribe(socket.ws!, doc));
|
|
107
|
+
else if (back) (delete document.body.dataset[versionKey(doc)], isOpen() && subscribe(socket.ws!, doc));
|
|
108
|
+
const held = docs.get(doc)!;
|
|
109
|
+
// moved, it keeps its items; held anew, what's in it already (served with the page) is its items, by key, so
|
|
110
|
+
// a change finds them and nothing is made twice
|
|
111
|
+
if (held.get(collection)?.el !== el) {
|
|
112
|
+
const served = own ? [] : [...el.children].filter((c) => c.localName === tag);
|
|
113
|
+
held.set(collection, {
|
|
114
|
+
el,
|
|
115
|
+
tag,
|
|
116
|
+
fields,
|
|
117
|
+
own,
|
|
118
|
+
order,
|
|
119
|
+
items: new Map(own ? [["1", el]] : served.map((c) => [c.getAttribute("key")!, c])),
|
|
120
|
+
may: mays.get(doc)?.[collection] ?? {},
|
|
121
|
+
});
|
|
122
|
+
// a document held already: what's said of it is this collection's too (else, it's as it was served)
|
|
123
|
+
if (mays.has(doc)) allow(held.get(collection)!);
|
|
124
|
+
}
|
|
125
|
+
socket.ws ??= open();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A collection's element taken off the page lets its document go, unless it's put back in the same turn (a
|
|
129
|
+
* move takes it off and puts it back): its items go with it, so held again it's filled afresh, and with its
|
|
130
|
+
* document's last collection gone, the page unsubscribes from the document and is told it no more. */
|
|
131
|
+
export function letGo(el: HTMLElement) {
|
|
132
|
+
queueMicrotask(() => {
|
|
133
|
+
if (el.isConnected) return;
|
|
134
|
+
for (const [doc, held] of docs)
|
|
135
|
+
for (const [collection, h] of held) {
|
|
136
|
+
if (h.el !== el) continue;
|
|
137
|
+
for (const item of h.items.values()) item.remove();
|
|
138
|
+
held.delete(collection);
|
|
139
|
+
if (held.size) return void away.add(`${doc} ${collection}`);
|
|
140
|
+
docs.delete(doc);
|
|
141
|
+
mays.delete(doc);
|
|
142
|
+
socket.behind.delete(doc);
|
|
143
|
+
delete document.body.dataset[versionKey(doc)];
|
|
144
|
+
if (isOpen()) socket.ws!.send(JSON.stringify({ unsubscribe: doc }));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// sending
|
|
151
|
+
|
|
152
|
+
/** One document's part of a write of several (src/together.ts): its ops, and why if it's said. */
|
|
153
|
+
type Part = { doc: string; ops: Op[]; why?: string };
|
|
154
|
+
|
|
155
|
+
// 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
|
|
157
|
+
document.addEventListener("ops", (e) => {
|
|
158
|
+
const { doc, ops, why, writes, answer } = (
|
|
159
|
+
e as CustomEvent<{ doc: string; ops: Op[]; why?: string; writes?: Part[]; answer?: Answered }>
|
|
160
|
+
).detail;
|
|
161
|
+
if (writes) send(undefined, { writes }, answer);
|
|
162
|
+
else if (docs.has(doc)) send(doc, { ops, why }, answer);
|
|
163
|
+
else answer?.(`${doc} isn't on this page`);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
/** One write of several documents' ops, all of it or none (a board made with its first sections and posts): each part
|
|
167
|
+
* `{ doc, ops, why? }`, a document the page holds, or one the write makes, named by its add (`board:$1`); "$n" in a
|
|
168
|
+
* value, the write's nth add, whichever part made it. Its answer, as one document's write's: why it was refused, or
|
|
169
|
+
* nothing; what it did comes as each document's change. */
|
|
170
|
+
export const together = (writes: Part[]) =>
|
|
171
|
+
new Promise<Answer>((answer) => document.dispatchEvent(new CustomEvent("ops", { detail: { writes, answer } })));
|
|
172
|
+
|
|
173
|
+
/** Ask a document's server for something besides ops: { relink: member }, { agent: label }, { takeBack: id }, { closeNotice: true }. */
|
|
174
|
+
export const ask = (doc: string, asked: object) =>
|
|
175
|
+
new Promise<Answer>((answer) => (docs.has(doc) ? send(doc, asked, answer) : answer(`${doc} isn't on this page`)));
|
|
176
|
+
|
|
177
|
+
/** 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
|
+
* away, refused at once. */
|
|
179
|
+
function send(doc: string | undefined, m: object, answer: Answered = () => {}) {
|
|
180
|
+
if (!isOpen()) return answer("Not connected just now: try again in a moment.");
|
|
181
|
+
socket.sent.set(++socket.id, { answer });
|
|
182
|
+
socket.ws!.send(JSON.stringify({ id: socket.id, doc, ...m }));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// a document subscribed to again (the page back from away) says the version it has, to be sent only what's since
|
|
186
|
+
const subscribe = (s: WebSocket, doc: string) => {
|
|
187
|
+
const had = document.body.dataset[versionKey(doc)];
|
|
188
|
+
s.send(JSON.stringify({ subscribe: doc, ...(had ? { since: Number(had) } : {}) }));
|
|
189
|
+
};
|
|
190
|
+
/** A change missed (a socket's message lost on the way): the page asks what's since the version it has. */
|
|
191
|
+
function behind(s: WebSocket, doc: string, had: string) {
|
|
192
|
+
if (socket.behind.has(doc)) return;
|
|
193
|
+
socket.behind.add(doc);
|
|
194
|
+
s.send(JSON.stringify({ subscribe: doc, since: Number(had) }));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// the socket's life: open, heard, gone, and back
|
|
198
|
+
|
|
199
|
+
function open() {
|
|
200
|
+
const s = new WebSocket(`${location.origin.replace(/^http/, "ws")}/ws`);
|
|
201
|
+
s.onopen = () => {
|
|
202
|
+
socket.wait = 1000;
|
|
203
|
+
if (document.body.hasAttribute("data-away")) document.dispatchEvent(new Event("back"));
|
|
204
|
+
document.body.toggleAttribute("data-away", false);
|
|
205
|
+
for (const doc of docs.keys()) subscribe(s, doc);
|
|
206
|
+
};
|
|
207
|
+
s.onmessage = (e) => heard(s, JSON.parse(e.data));
|
|
208
|
+
s.onclose = (e) => gone(e.code);
|
|
209
|
+
return s;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** What the server says: a request's answer, a change, or its own news to this page. Anything heard is the
|
|
213
|
+
* socket there: asked on waking, it's answered. */
|
|
214
|
+
function heard(s: WebSocket, m: any) {
|
|
215
|
+
socket.asked = void clearTimeout(socket.asked);
|
|
216
|
+
if (m.ops) changed(s, m);
|
|
217
|
+
else if ("done" in m || "refused" in m) answered(m);
|
|
218
|
+
else if (m.build) built(m.build);
|
|
219
|
+
else if (m.may) mayDo(m);
|
|
220
|
+
else document.dispatchEvent(new CustomEvent("told", { detail: m }));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** A change to a document: it follows the version the page has (after it, where the document is told in
|
|
224
|
+
* parts); one heard already is let go, and one past a change missed waits while the page asks for what it
|
|
225
|
+
* 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[] }) {
|
|
227
|
+
const { doc } = m;
|
|
228
|
+
if (!docs.has(doc)) return;
|
|
229
|
+
const had = document.body.dataset[versionKey(doc)],
|
|
230
|
+
whole = m.ops[0]?.path === "";
|
|
231
|
+
if (!whole && had !== undefined && m.version <= Number(had)) return;
|
|
232
|
+
if (!whole && had !== undefined && (m.after ?? m.version - 1) !== Number(had)) return behind(s, doc, had);
|
|
233
|
+
socket.behind.delete(doc);
|
|
234
|
+
for (const o of m.ops) apply(docs.get(doc)!, o);
|
|
235
|
+
document.body.dataset[versionKey(doc)] = String(m.version);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** What the page's person may do in a document, now: on each of its elements. */
|
|
239
|
+
function mayDo({ doc, may }: { doc: string; may: Record<string, May> }) {
|
|
240
|
+
if (!docs.has(doc)) return;
|
|
241
|
+
mays.set(doc, may);
|
|
242
|
+
for (const [coll, h] of docs.get(doc)!) ((h.may = may[coll] ?? {}), allow(h));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** A request's answer: refused, its why to whoever sent it; done, nothing (what it did comes as its change).
|
|
246
|
+
* A document the page holds that isn't there is told to the element holding it; a refusal no one sent (a
|
|
247
|
+
* 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 }) {
|
|
249
|
+
// a document a stranger may not hold (all but what's anonymous): the page is signed out, as at the door
|
|
250
|
+
if (m.refused === true && m.why === SIGN_IN) {
|
|
251
|
+
document.body.toggleAttribute("data-signed-out", true);
|
|
252
|
+
document.dispatchEvent(new CustomEvent("signed-out", { detail: { code: 401 } }));
|
|
253
|
+
}
|
|
254
|
+
if (m.refused === true)
|
|
255
|
+
return docs
|
|
256
|
+
.get(m.doc!)
|
|
257
|
+
?.forEach(({ el }) => el.dispatchEvent(new CustomEvent("refused", { bubbles: true, detail: { why: m.why } })));
|
|
258
|
+
const id = (m.done ?? m.refused)!,
|
|
259
|
+
request = socket.sent.get(id);
|
|
260
|
+
socket.sent.delete(id);
|
|
261
|
+
if (m.refused !== undefined) return request ? request.answer(m.why) : console.error(`joined: refused, ${m.why}`);
|
|
262
|
+
request?.answer();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The server's build, told as the socket opens: the one the page was served by (<body data-build>), or another,
|
|
266
|
+
* and the page reloads; but never over what someone's typed: while a write is unanswered, or a draft shown holds
|
|
267
|
+
* changes, it says so, and looks again each second. */
|
|
268
|
+
function built(build: string) {
|
|
269
|
+
if ((document.body.dataset.build ??= build) === build || socket.stale) return;
|
|
270
|
+
socket.stale = true;
|
|
271
|
+
const free = () => !socket.sent.size && !unsaved();
|
|
272
|
+
if (free()) return location.reload();
|
|
273
|
+
toast("There's a new version: the page reloads once you save or close what's open.", { hold: true });
|
|
274
|
+
setInterval(() => free() && location.reload(), 1000);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** The socket gone: the page signed out, if that's why, or away, to come back by itself. */
|
|
278
|
+
async function gone(code: number) {
|
|
279
|
+
socket.asked = void clearTimeout(socket.asked);
|
|
280
|
+
// what was sent and not answered may or may not have landed: when it's back, the page shows which
|
|
281
|
+
for (const [id, { answer }] of socket.sent)
|
|
282
|
+
(socket.sent.delete(id), answer("The connection dropped: look again once it's back."));
|
|
283
|
+
// closed by the server on purpose (4001 replaced, 4002 taken back, 4003 removed), or refused at the
|
|
284
|
+
// door: a browser tells neither apart from a drop by its code, so the door is asked as a page
|
|
285
|
+
const out =
|
|
286
|
+
(code >= 4001 && code <= 4003) ||
|
|
287
|
+
(await fetch("/ws").then(
|
|
288
|
+
(r) => r.status === 401,
|
|
289
|
+
() => false,
|
|
290
|
+
));
|
|
291
|
+
if (out) {
|
|
292
|
+
document.body.toggleAttribute("data-signed-out", true);
|
|
293
|
+
return document.dispatchEvent(new CustomEvent("signed-out", { detail: { code } }));
|
|
294
|
+
}
|
|
295
|
+
document.body.toggleAttribute("data-away", true);
|
|
296
|
+
document.dispatchEvent(new Event("away"));
|
|
297
|
+
// a quarter either side, so pages away together (a deploy) don't all come back at once
|
|
298
|
+
socket.again = setTimeout(back, socket.wait * (0.75 + Math.random() / 2));
|
|
299
|
+
socket.wait = Math.min(socket.wait * 2, 30_000);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** A socket the browser calls open, asked whether it is: a subscribe to a document it holds, with id 0, which the
|
|
303
|
+
* server answers done. Unanswered in time, it's let go. */
|
|
304
|
+
function stillThere(s: WebSocket) {
|
|
305
|
+
const [doc] = docs.keys();
|
|
306
|
+
if (socket.asked || !doc) return;
|
|
307
|
+
socket.asked = setTimeout(() => {
|
|
308
|
+
((s.onclose = s.onmessage = null), s.close());
|
|
309
|
+
gone(1006);
|
|
310
|
+
}, ANSWER);
|
|
311
|
+
s.send(JSON.stringify({ subscribe: doc, id: 0 }));
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** The socket again, now: when it's time, or as soon as the phone wakes or the network comes back; one that
|
|
315
|
+
* says it's open is asked first. */
|
|
316
|
+
function back() {
|
|
317
|
+
if (isOpen()) return stillThere(socket.ws!);
|
|
318
|
+
if (!document.body.hasAttribute("data-away") || socket.ws?.readyState === WebSocket.CONNECTING) return;
|
|
319
|
+
clearTimeout(socket.again);
|
|
320
|
+
socket.ws = open();
|
|
321
|
+
}
|
|
322
|
+
addEventListener("online", back);
|
|
323
|
+
document.addEventListener("visibilitychange", () => document.visibilityState === "visible" && back());
|
|
324
|
+
|
|
325
|
+
// applying a change
|
|
326
|
+
|
|
327
|
+
function apply(held: Map<string, Held>, o: Op) {
|
|
328
|
+
if (o.path === "")
|
|
329
|
+
return void held.forEach((h, coll) => whole(h, h.own ? [{ ...o.value, id: 1 }] : (o.value[coll] ?? [])));
|
|
330
|
+
// a field of the document's own (/greeting): its own element's
|
|
331
|
+
const own = [...held].find(([, h]) => h.own)?.[0];
|
|
332
|
+
if (own && /^\/[a-z][a-z0-9-]*$/.test(o.path)) o = { ...o, path: `/${own}/1${o.path}` };
|
|
333
|
+
const { coll, key, field, link } = parts(o.path);
|
|
334
|
+
const h = held.get(coll);
|
|
335
|
+
if (!h) return;
|
|
336
|
+
const el = h.items.get(key!);
|
|
337
|
+
// one of an object's fields (/orders/1/terms/days), its attribute named for both (terms.days)
|
|
338
|
+
if (link !== undefined && `${field}.${link}` in h.fields) {
|
|
339
|
+
const f = `${field}.${link}`;
|
|
340
|
+
if (el) set(el, h.fields[f], f, o.op === "remove" ? undefined : o.value);
|
|
341
|
+
}
|
|
342
|
+
// an object whole: each of its fields
|
|
343
|
+
else if (link === undefined && field !== undefined && !(field in h.fields)) {
|
|
344
|
+
for (const f of Object.keys(h.fields).filter((f) => f.startsWith(`${field}.`)))
|
|
345
|
+
if (el) set(el, h.fields[f], f, o.op === "remove" ? undefined : valueAt({ [field]: o.value }, f));
|
|
346
|
+
}
|
|
347
|
+
// an id added to a list, or taken from it: the rest of it as it was
|
|
348
|
+
else if (link !== undefined) {
|
|
349
|
+
const ids = (el?.getAttribute(field!) ?? "").split(" ").filter(Boolean).map(Number);
|
|
350
|
+
if (el) set(el, "ids", field!, o.op === "add" ? [...ids, o.value] : ids.filter((id) => id !== Number(link)));
|
|
351
|
+
} else if (o.op === "add") placed(h, element(h, o.value));
|
|
352
|
+
else if (o.op === "remove") field ? el?.removeAttribute(field) : (el?.remove(), h.items.delete(key!));
|
|
353
|
+
else if (el) (set(el, h.fields[field!], field!, o.value), field === h.order && placed(h, el));
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** An item put where it goes: in its collection's order, by the field (or, without one, its key) and then its key;
|
|
357
|
+
* or, with no order, last. */
|
|
358
|
+
function placed(h: Held, el: Element) {
|
|
359
|
+
if (!h.order) return void (el.parentNode !== h.el && h.el.append(el));
|
|
360
|
+
// without one, an item ranks by its key
|
|
361
|
+
const at = (e: Element) =>
|
|
362
|
+
[Number(e.getAttribute(h.order!) ?? e.getAttribute("key")), Number(e.getAttribute("key"))] as const;
|
|
363
|
+
const [rank, key] = at(el);
|
|
364
|
+
const after = [...h.items.values()].find((e) => {
|
|
365
|
+
if (e === el || e.parentNode !== h.el) return false;
|
|
366
|
+
const [r, k] = at(e);
|
|
367
|
+
return r > rank || (r === rank && k > key);
|
|
368
|
+
});
|
|
369
|
+
// put before the first that goes after it, unless it's there already
|
|
370
|
+
if (after ? el.nextElementSibling !== after : el.parentNode !== h.el || h.el.lastElementChild !== el)
|
|
371
|
+
h.el.insertBefore(el, after ?? null);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** A collection, whole, as the document has it: each item kept by key and changed only where it
|
|
375
|
+
* differs, those gone taken away, and those new added. */
|
|
376
|
+
function whole(h: Held, items: Record<string, unknown>[]) {
|
|
377
|
+
const gone = new Set(h.items.keys());
|
|
378
|
+
for (const item of items) {
|
|
379
|
+
const el = h.items.get(String(item.id));
|
|
380
|
+
if (!el) placed(h, element(h, item));
|
|
381
|
+
else {
|
|
382
|
+
for (const [field, type] of Object.entries(h.fields)) set(el, type, field, valueAt(item, field));
|
|
383
|
+
if (h.order) placed(h, el);
|
|
384
|
+
}
|
|
385
|
+
gone.delete(String(item.id));
|
|
386
|
+
}
|
|
387
|
+
for (const key of gone) (h.items.get(key)!.remove(), h.items.delete(key));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function element({ tag, fields, items, may }: Held, item: Record<string, unknown>) {
|
|
391
|
+
const el = document.createElement(tag);
|
|
392
|
+
el.setAttribute("key", String(item.id));
|
|
393
|
+
set(el, "string", "may", may[String(item.id)]);
|
|
394
|
+
for (const [field, type] of Object.entries(fields)) set(el, type, field, valueAt(item, field));
|
|
395
|
+
items.set(String(item.id), el);
|
|
396
|
+
return el;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** What the page's person may do, on a collection's element and each of its items' (a document's own element is
|
|
400
|
+
* its one item, and says it as the collection's). */
|
|
401
|
+
function allow(h: Held) {
|
|
402
|
+
set(h.el, "string", "may", h.may[""]);
|
|
403
|
+
if (!h.own) for (const [key, el] of h.items) set(el, "string", "may", h.may[key]);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** A field as an attribute, by the rule the server writes a page by (attribute(), src/ops.ts), set only if it
|
|
407
|
+
* differs: a display hears only what changed. */
|
|
408
|
+
function set(el: Element, type: Type | undefined, field: string, value: unknown) {
|
|
409
|
+
const text = attribute(type, value);
|
|
410
|
+
if (el.getAttribute(field) === text) return;
|
|
411
|
+
if (text === null) el.removeAttribute(field);
|
|
412
|
+
else el.setAttribute(field, text);
|
|
413
|
+
}
|