@alecsibilia/ui-kit 0.0.0-stage → 0.1.0-alpha.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.
- package/CHANGELOG.md +7 -0
- package/LICENSE +201 -0
- package/README.md +233 -2
- package/bin/ui-kit.js +12 -0
- package/dist/alchemy/app.d.ts +34 -0
- package/dist/alchemy/app.js +88 -0
- package/dist/alchemy/index.d.ts +11 -0
- package/dist/alchemy/index.js +11 -0
- package/dist/alchemy/live-rooms.d.ts +21 -0
- package/dist/alchemy/live-rooms.js +31 -0
- package/dist/alchemy/migrations.d.ts +28 -0
- package/dist/alchemy/migrations.js +56 -0
- package/dist/alchemy/stage.d.ts +20 -0
- package/dist/alchemy/stage.js +20 -0
- package/dist/alchemy/wrangler-config.d.ts +76 -0
- package/dist/alchemy/wrangler-config.js +82 -0
- package/dist/build/app-modules.d.ts +11 -0
- package/dist/build/app-modules.js +40 -0
- package/dist/build/check.d.ts +4 -0
- package/dist/build/check.js +170 -0
- package/dist/build/cli.d.ts +1 -0
- package/dist/build/cli.js +79 -0
- package/dist/build/feature.d.ts +8 -0
- package/dist/build/feature.js +24 -0
- package/dist/build/index.d.ts +13 -0
- package/dist/build/index.js +13 -0
- package/dist/build/measure-cpu.d.ts +2 -0
- package/dist/build/measure-cpu.js +196 -0
- package/dist/build/prerender.d.ts +5 -0
- package/dist/build/prerender.js +109 -0
- package/dist/build/preview.d.ts +7 -0
- package/dist/build/preview.js +41 -0
- package/dist/build/seo-files.d.ts +27 -0
- package/dist/build/seo-files.js +24 -0
- package/dist/client/head-swap.d.ts +45 -0
- package/dist/client/head-swap.js +183 -0
- package/dist/client/hydration-pass.d.ts +56 -0
- package/dist/client/hydration-pass.js +186 -0
- package/dist/client/hydration-reset.d.ts +82 -0
- package/dist/client/hydration-reset.js +154 -0
- package/dist/client/index.d.ts +37 -0
- package/dist/client/index.js +65 -0
- package/dist/client/nav-client.d.ts +35 -0
- package/dist/client/nav-client.js +371 -0
- package/dist/live/collab/doc-provider.d.ts +74 -0
- package/dist/live/collab/doc-provider.js +412 -0
- package/dist/live/collab/index.d.ts +51 -0
- package/dist/live/collab/index.jsx +142 -0
- package/dist/live/collab-status.d.ts +25 -0
- package/dist/live/collab-status.js +17 -0
- package/dist/live/cursors/cursor-engine.d.ts +57 -0
- package/dist/live/cursors/cursor-engine.js +243 -0
- package/dist/live/cursors/index.d.ts +46 -0
- package/dist/live/cursors/index.jsx +110 -0
- package/dist/live/declaration.d.ts +113 -0
- package/dist/live/declaration.js +94 -0
- package/dist/live/index.d.ts +8 -0
- package/dist/live/index.js +8 -0
- package/dist/live/protocol.d.ts +224 -0
- package/dist/live/protocol.js +272 -0
- package/dist/live/server/doc-store.d.ts +68 -0
- package/dist/live/server/doc-store.js +187 -0
- package/dist/live/server/index.d.ts +60 -0
- package/dist/live/server/index.js +77 -0
- package/dist/live/server/room-presence.d.ts +24 -0
- package/dist/live/server/room-presence.js +123 -0
- package/dist/live/server/room-sockets.d.ts +65 -0
- package/dist/live/server/room-sockets.js +97 -0
- package/dist/live/server/room.d.ts +20 -0
- package/dist/live/server/room.js +348 -0
- package/dist/live/server/route.d.ts +56 -0
- package/dist/live/server/route.js +159 -0
- package/dist/live/server/types.d.ts +163 -0
- package/dist/live/server/types.js +2 -0
- package/dist/logic/agent-actions.d.ts +133 -0
- package/dist/logic/agent-actions.js +150 -0
- package/dist/logic/command-types.d.ts +53 -0
- package/dist/logic/command-types.js +0 -0
- package/dist/logic/dev.d.ts +9 -0
- package/dist/logic/dev.js +10 -0
- package/dist/logic/draft-rebase.d.ts +59 -0
- package/dist/logic/draft-rebase.js +71 -0
- package/dist/logic/form-state.d.ts +105 -0
- package/dist/logic/form-state.js +93 -0
- package/dist/logic/head.d.ts +145 -0
- package/dist/logic/head.js +140 -0
- package/dist/logic/index.d.ts +27 -0
- package/dist/logic/index.js +25 -0
- package/dist/logic/island-def.d.ts +89 -0
- package/dist/logic/island-def.js +59 -0
- package/dist/logic/kit-commands.d.ts +104 -0
- package/dist/logic/kit-commands.js +72 -0
- package/dist/logic/kit-errors.d.ts +25 -0
- package/dist/logic/kit-errors.js +6 -0
- package/dist/logic/listen.d.ts +99 -0
- package/dist/logic/listen.js +92 -0
- package/dist/logic/mutation-def.d.ts +65 -0
- package/dist/logic/mutation-def.js +43 -0
- package/dist/logic/navigation-data.d.ts +50 -0
- package/dist/logic/navigation-data.js +55 -0
- package/dist/logic/navigation-policy.d.ts +74 -0
- package/dist/logic/navigation-policy.js +127 -0
- package/dist/logic/page-model-def.d.ts +108 -0
- package/dist/logic/page-model-def.js +31 -0
- package/dist/logic/path.d.ts +31 -0
- package/dist/logic/path.js +48 -0
- package/dist/logic/query-def.d.ts +56 -0
- package/dist/logic/query-def.js +37 -0
- package/dist/logic/query-key.d.ts +24 -0
- package/dist/logic/query-key.js +35 -0
- package/dist/logic/query-types.d.ts +28 -0
- package/dist/logic/query-types.js +2 -0
- package/dist/logic/resources.d.ts +163 -0
- package/dist/logic/resources.js +62 -0
- package/dist/logic/result.d.ts +30 -0
- package/dist/logic/result.js +23 -0
- package/dist/logic/route-model.d.ts +139 -0
- package/dist/logic/route-model.js +207 -0
- package/dist/logic/route-params.d.ts +80 -0
- package/dist/logic/route-params.js +114 -0
- package/dist/logic/route-table.d.ts +308 -0
- package/dist/logic/route-table.js +206 -0
- package/dist/logic/seo.d.ts +61 -0
- package/dist/logic/seo.js +101 -0
- package/dist/logic/site.d.ts +12 -0
- package/dist/logic/site.js +0 -0
- package/dist/logic/subscriptions.d.ts +177 -0
- package/dist/logic/subscriptions.js +152 -0
- package/dist/render/index.d.ts +32 -0
- package/dist/render/index.js +52 -0
- package/dist/runtime/island-wake.d.ts +38 -0
- package/dist/runtime/island-wake.js +147 -0
- package/dist/runtime/query-scope.d.ts +224 -0
- package/dist/runtime/query-scope.js +222 -0
- package/dist/runtime/room-hub.d.ts +107 -0
- package/dist/runtime/room-hub.js +307 -0
- package/dist/runtime/subscription-runner.d.ts +71 -0
- package/dist/runtime/subscription-runner.js +240 -0
- package/dist/server/bindings.d.ts +41 -0
- package/dist/server/bindings.js +74 -0
- package/dist/server/browser.d.ts +9 -0
- package/dist/server/browser.js +9 -0
- package/dist/server/edge-cache.d.ts +28 -0
- package/dist/server/edge-cache.js +35 -0
- package/dist/server/errors.d.ts +45 -0
- package/dist/server/errors.js +36 -0
- package/dist/server/form-actions-setup.d.ts +1 -0
- package/dist/server/form-actions-setup.js +15 -0
- package/dist/server/form-actions.d.ts +121 -0
- package/dist/server/form-actions.js +211 -0
- package/dist/server/index.d.ts +36 -0
- package/dist/server/index.js +35 -0
- package/dist/server/inputs.d.ts +45 -0
- package/dist/server/inputs.js +77 -0
- package/dist/server/rate-limit.d.ts +29 -0
- package/dist/server/rate-limit.js +32 -0
- package/dist/server/read-cache.d.ts +74 -0
- package/dist/server/read-cache.js +58 -0
- package/dist/server/request-info.d.ts +17 -0
- package/dist/server/request-info.js +11 -0
- package/dist/server/request.d.ts +52 -0
- package/dist/server/request.js +73 -0
- package/dist/server/response-cookies.d.ts +23 -0
- package/dist/server/response-cookies.js +24 -0
- package/dist/server/run-effect.d.ts +42 -0
- package/dist/server/run-effect.js +92 -0
- package/dist/server/run.d.ts +63 -0
- package/dist/server/run.js +66 -0
- package/dist/server/session.d.ts +125 -0
- package/dist/server/session.js +247 -0
- package/dist/test/fake-bindings.d.ts +32 -0
- package/dist/test/fake-bindings.js +108 -0
- package/dist/test/fake-room.d.ts +66 -0
- package/dist/test/fake-room.js +216 -0
- package/dist/test/index.d.ts +19 -0
- package/dist/test/index.js +44 -0
- package/dist/test/probe.d.ts +11 -0
- package/dist/test/probe.js +11 -0
- package/dist/test/solid-jsx.d.ts +23 -0
- package/dist/test/solid-jsx.js +28 -0
- package/dist/test/solid-preload.d.ts +1 -0
- package/dist/test/solid-preload.js +29 -0
- package/dist/ui/code-editor/index.d.ts +21 -0
- package/dist/ui/code-editor/index.jsx +286 -0
- package/dist/ui/collab-text/index.d.ts +29 -0
- package/dist/ui/collab-text/index.jsx +96 -0
- package/dist/ui/components/controls.d.ts +69 -0
- package/dist/ui/components/controls.jsx +81 -0
- package/dist/ui/components/feedback.d.ts +26 -0
- package/dist/ui/components/feedback.jsx +80 -0
- package/dist/ui/components/layout.d.ts +74 -0
- package/dist/ui/components/layout.jsx +146 -0
- package/dist/ui/components/lists.d.ts +69 -0
- package/dist/ui/components/lists.jsx +133 -0
- package/dist/ui/components/navigation.d.ts +73 -0
- package/dist/ui/components/navigation.jsx +112 -0
- package/dist/ui/components/text.d.ts +34 -0
- package/dist/ui/components/text.jsx +163 -0
- package/dist/ui/index.d.ts +7 -0
- package/dist/ui/index.js +7 -0
- package/dist/ui/jsx-runtime.d.ts +19 -0
- package/dist/ui/jsx-runtime.js +0 -0
- package/dist/ui/list-keys.d.ts +31 -0
- package/dist/ui/list-keys.js +45 -0
- package/dist/ui/styles/base.css +38 -0
- package/dist/ui/styles/components.css +608 -0
- package/dist/ui/styles/tokens.css +27 -0
- package/dist/ui/styles/transitions.css +23 -0
- package/dist/ui/styles/workbench.css +428 -0
- package/dist/ui/styles.css +9 -0
- package/dist/view/agent-actions-view.d.ts +30 -0
- package/dist/view/agent-actions-view.js +27 -0
- package/dist/view/agent-surface.d.ts +42 -0
- package/dist/view/agent-surface.js +123 -0
- package/dist/view/app-scope.d.ts +51 -0
- package/dist/view/app-scope.js +75 -0
- package/dist/view/boundary.d.ts +18 -0
- package/dist/view/boundary.jsx +26 -0
- package/dist/view/command-runner.d.ts +16 -0
- package/dist/view/command-runner.js +245 -0
- package/dist/view/form.d.ts +82 -0
- package/dist/view/form.jsx +109 -0
- package/dist/view/head-view.d.ts +31 -0
- package/dist/view/head-view.js +46 -0
- package/dist/view/index.d.ts +41 -0
- package/dist/view/index.js +40 -0
- package/dist/view/island-root.d.ts +30 -0
- package/dist/view/island-root.js +36 -0
- package/dist/view/island.d.ts +102 -0
- package/dist/view/island.jsx +154 -0
- package/dist/view/match.d.ts +41 -0
- package/dist/view/match.js +111 -0
- package/dist/view/model.d.ts +92 -0
- package/dist/view/model.js +162 -0
- package/dist/view/navigation-head.d.ts +29 -0
- package/dist/view/navigation-head.jsx +38 -0
- package/dist/view/page-model.d.ts +46 -0
- package/dist/view/page-model.js +146 -0
- package/dist/view/query-effects.d.ts +87 -0
- package/dist/view/query-effects.js +147 -0
- package/dist/view/query-ok.d.ts +45 -0
- package/dist/view/query-ok.js +96 -0
- package/dist/view/query.d.ts +115 -0
- package/dist/view/query.jsx +179 -0
- package/dist/view/result-view.d.ts +14 -0
- package/dist/view/result-view.jsx +15 -0
- package/dist/view/route-pages.d.ts +43 -0
- package/dist/view/route-pages.js +59 -0
- package/dist/view/router.d.ts +44 -0
- package/dist/view/router.js +123 -0
- package/dist/vite/dev-middleware.d.ts +12 -0
- package/dist/vite/dev-middleware.js +39 -0
- package/dist/vite/dev-objects.d.ts +25 -0
- package/dist/vite/dev-objects.js +127 -0
- package/dist/vite/dev-platform.d.ts +19 -0
- package/dist/vite/dev-platform.js +77 -0
- package/dist/vite/dev-seo-files.d.ts +11 -0
- package/dist/vite/dev-seo-files.js +40 -0
- package/dist/vite/index.d.ts +53 -0
- package/dist/vite/index.js +148 -0
- package/dist/vite/local-config.d.ts +34 -0
- package/dist/vite/local-config.js +72 -0
- package/dist/vite/worker-build.d.ts +24 -0
- package/dist/vite/worker-build.js +84 -0
- package/dist/worker/create-worker.d.ts +52 -0
- package/dist/worker/create-worker.js +90 -0
- package/dist/worker/index.d.ts +12 -0
- package/dist/worker/index.js +12 -0
- package/dist/worker/page-routes.d.ts +88 -0
- package/dist/worker/page-routes.js +100 -0
- package/dist/worker/page-status.d.ts +41 -0
- package/dist/worker/page-status.js +23 -0
- package/dist/worker/request-prelude.d.ts +69 -0
- package/dist/worker/request-prelude.js +107 -0
- package/dist/worker/security.d.ts +76 -0
- package/dist/worker/security.js +137 -0
- package/dist/worker/server-app.d.ts +54 -0
- package/dist/worker/server-app.js +76 -0
- package/guards/browser-only.js +5 -0
- package/guards/node-only.js +6 -0
- package/guards/server-only.js +5 -0
- package/migrations/0001-sessions.sql +22 -0
- package/package.json +233 -5
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The data side of `<LiveCursors>`: what to send over the room socket and
|
|
3
|
+
* which signal to write for each received cursor. No DOM (the component
|
|
4
|
+
* feeds it pointer positions, visibility and the surface width), so it is
|
|
5
|
+
* probed on Solid's browser build (`tests/probes/live-cursors.probe.ts`).
|
|
6
|
+
*
|
|
7
|
+
* Cursors are high-frequency, ephemeral, and nothing decides anything from
|
|
8
|
+
* them: they never become Messages (no `update`, no trace entry). Each peer
|
|
9
|
+
* cursor is one signal holding its position; the view reads it in one
|
|
10
|
+
* `style` attribute, so a received frame costs one attribute write per
|
|
11
|
+
* moved cursor and nothing else re-runs.
|
|
12
|
+
*
|
|
13
|
+
* Client contract (`docs/guides/ui-kit-live-rooms.md`, "Client"):
|
|
14
|
+
* - send only when the room's `welcome` says `cursors: true`;
|
|
15
|
+
* - at most one frame per `sendEveryMs` (default 50 ms), trailing, latest
|
|
16
|
+
* position wins;
|
|
17
|
+
* - nothing while alone (no peer other than you in the latest presence);
|
|
18
|
+
* - on hide: `l` (if a position was sent) and nothing until visible;
|
|
19
|
+
* - drop any peer missing from the latest presence list;
|
|
20
|
+
* - clear everything when the socket drops (a reconnect re-announces).
|
|
21
|
+
*/
|
|
22
|
+
import { type Accessor } from 'solid-js';
|
|
23
|
+
import type { RoomStatus } from '../../runtime/room-hub.ts';
|
|
24
|
+
import { type RoomServerFrame } from '../protocol.ts';
|
|
25
|
+
export interface CursorEngineDeps {
|
|
26
|
+
readonly now: () => number;
|
|
27
|
+
readonly setTimeout: (fn: () => void, ms: number) => unknown;
|
|
28
|
+
readonly clearTimeout: (handle: unknown) => void;
|
|
29
|
+
}
|
|
30
|
+
export interface CursorEngine {
|
|
31
|
+
/** Peer ids with a cursor, in arrival order (the view's keyed list). */
|
|
32
|
+
readonly ids: Accessor<readonly string[]>;
|
|
33
|
+
/** The one `style` attribute of a peer's cursor (position, hue, shown). */
|
|
34
|
+
readonly styleOf: (id: string) => string;
|
|
35
|
+
/** A peer's display name. */
|
|
36
|
+
readonly nameOf: (id: string) => string;
|
|
37
|
+
/** The surface's width in CSS px (cursor x is a fraction of it). */
|
|
38
|
+
readonly setWidth: (px: number) => void;
|
|
39
|
+
/** Where frames go (the room handle's `send`); until set, nothing is sent. */
|
|
40
|
+
readonly attach: (send: (text: string) => boolean) => void;
|
|
41
|
+
readonly onFrame: (frame: RoomServerFrame) => void;
|
|
42
|
+
readonly onStatus: (status: RoomStatus) => void;
|
|
43
|
+
/** This viewer's pointer over the surface (x: 0..1 of its width, y: px from its top). */
|
|
44
|
+
readonly move: (xFraction: number, yPx: number) => void;
|
|
45
|
+
/** The pointer left the surface. */
|
|
46
|
+
readonly leave: () => void;
|
|
47
|
+
readonly setHidden: (hidden: boolean) => void;
|
|
48
|
+
readonly dispose: () => void;
|
|
49
|
+
/** Frames handed to `send` (tests). */
|
|
50
|
+
readonly sent: () => number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates the engine. Call inside a Solid owner (it creates signals).
|
|
54
|
+
*
|
|
55
|
+
* @param sendEveryMs - Client send cap (default `LIVE_TIMING.CURSOR_SEND_MS`).
|
|
56
|
+
*/
|
|
57
|
+
export declare function createCursorEngine(sendEveryMs?: number, deps?: CursorEngineDeps): CursorEngine;
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The data side of `<LiveCursors>`: what to send over the room socket and
|
|
3
|
+
* which signal to write for each received cursor. No DOM (the component
|
|
4
|
+
* feeds it pointer positions, visibility and the surface width), so it is
|
|
5
|
+
* probed on Solid's browser build (`tests/probes/live-cursors.probe.ts`).
|
|
6
|
+
*
|
|
7
|
+
* Cursors are high-frequency, ephemeral, and nothing decides anything from
|
|
8
|
+
* them: they never become Messages (no `update`, no trace entry). Each peer
|
|
9
|
+
* cursor is one signal holding its position; the view reads it in one
|
|
10
|
+
* `style` attribute, so a received frame costs one attribute write per
|
|
11
|
+
* moved cursor and nothing else re-runs.
|
|
12
|
+
*
|
|
13
|
+
* Client contract (`docs/guides/ui-kit-live-rooms.md`, "Client"):
|
|
14
|
+
* - send only when the room's `welcome` says `cursors: true`;
|
|
15
|
+
* - at most one frame per `sendEveryMs` (default 50 ms), trailing, latest
|
|
16
|
+
* position wins;
|
|
17
|
+
* - nothing while alone (no peer other than you in the latest presence);
|
|
18
|
+
* - on hide: `l` (if a position was sent) and nothing until visible;
|
|
19
|
+
* - drop any peer missing from the latest presence list;
|
|
20
|
+
* - clear everything when the socket drops (a reconnect re-announces).
|
|
21
|
+
*/
|
|
22
|
+
import { createSignal } from 'solid-js';
|
|
23
|
+
import { CURSOR_LEAVE_FRAME, encodeCursor, LIVE_TIMING, } from '../protocol.js';
|
|
24
|
+
/** Largest x in a cursor frame (x is 1/10000 of the surface width). */
|
|
25
|
+
const X_SCALE = 10_000;
|
|
26
|
+
const browserDeps = {
|
|
27
|
+
now: () => performance.now(),
|
|
28
|
+
setTimeout: (fn, ms) => globalThis.setTimeout(fn, ms),
|
|
29
|
+
clearTimeout: (h) => globalThis.clearTimeout(h),
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Creates the engine. Call inside a Solid owner (it creates signals).
|
|
33
|
+
*
|
|
34
|
+
* @param sendEveryMs - Client send cap (default `LIVE_TIMING.CURSOR_SEND_MS`).
|
|
35
|
+
*/
|
|
36
|
+
export function createCursorEngine(sendEveryMs = LIVE_TIMING.CURSOR_SEND_MS, deps = browserDeps) {
|
|
37
|
+
const slots = new Map();
|
|
38
|
+
let order = [];
|
|
39
|
+
const [ids, setIds] = createSignal([]);
|
|
40
|
+
const [width, setWidthSignal] = createSignal(1);
|
|
41
|
+
let send;
|
|
42
|
+
let self = '';
|
|
43
|
+
let cursorsOn = false;
|
|
44
|
+
let peers = new Map();
|
|
45
|
+
let hidden = false;
|
|
46
|
+
let sentPosition = false;
|
|
47
|
+
let lastSentAt = Number.NEGATIVE_INFINITY;
|
|
48
|
+
let pending;
|
|
49
|
+
let timer;
|
|
50
|
+
let sentCount = 0;
|
|
51
|
+
const publishIds = () => setIds(order.slice());
|
|
52
|
+
const slotFor = (id, peer) => {
|
|
53
|
+
const existing = slots.get(id);
|
|
54
|
+
if (existing)
|
|
55
|
+
return existing;
|
|
56
|
+
const [position, setPosition] = createSignal({
|
|
57
|
+
x: 0,
|
|
58
|
+
y: 0,
|
|
59
|
+
shown: false,
|
|
60
|
+
});
|
|
61
|
+
const [label, setLabel] = createSignal({
|
|
62
|
+
name: peer.name,
|
|
63
|
+
hue: peer.hue,
|
|
64
|
+
});
|
|
65
|
+
const slot = {
|
|
66
|
+
id,
|
|
67
|
+
position,
|
|
68
|
+
setPosition,
|
|
69
|
+
label,
|
|
70
|
+
setLabel,
|
|
71
|
+
name: peer.name,
|
|
72
|
+
hue: peer.hue,
|
|
73
|
+
};
|
|
74
|
+
slots.set(id, slot);
|
|
75
|
+
order.push(id);
|
|
76
|
+
publishIds();
|
|
77
|
+
return slot;
|
|
78
|
+
};
|
|
79
|
+
const dropAll = () => {
|
|
80
|
+
if (slots.size === 0)
|
|
81
|
+
return;
|
|
82
|
+
slots.clear();
|
|
83
|
+
order = [];
|
|
84
|
+
publishIds();
|
|
85
|
+
};
|
|
86
|
+
const hasOthers = () => {
|
|
87
|
+
for (const id of peers.keys())
|
|
88
|
+
if (id !== self)
|
|
89
|
+
return true;
|
|
90
|
+
return false;
|
|
91
|
+
};
|
|
92
|
+
const mayMove = () => send !== undefined && cursorsOn && !hidden && hasOthers();
|
|
93
|
+
const transmit = (text) => {
|
|
94
|
+
if (!send)
|
|
95
|
+
return;
|
|
96
|
+
if (send(text)) {
|
|
97
|
+
sentCount += 1;
|
|
98
|
+
lastSentAt = deps.now();
|
|
99
|
+
sentPosition = text !== CURSOR_LEAVE_FRAME;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const cancelPending = () => {
|
|
103
|
+
pending = undefined;
|
|
104
|
+
if (timer !== undefined)
|
|
105
|
+
deps.clearTimeout(timer);
|
|
106
|
+
timer = undefined;
|
|
107
|
+
};
|
|
108
|
+
const flushPending = () => {
|
|
109
|
+
timer = undefined;
|
|
110
|
+
const frame = pending;
|
|
111
|
+
pending = undefined;
|
|
112
|
+
if (frame === undefined)
|
|
113
|
+
return;
|
|
114
|
+
if (frame === CURSOR_LEAVE_FRAME ? sentPosition : mayMove())
|
|
115
|
+
transmit(frame);
|
|
116
|
+
};
|
|
117
|
+
const queue = (frame) => {
|
|
118
|
+
pending = frame;
|
|
119
|
+
const wait = sendEveryMs - (deps.now() - lastSentAt);
|
|
120
|
+
if (wait <= 0) {
|
|
121
|
+
if (timer !== undefined)
|
|
122
|
+
deps.clearTimeout(timer);
|
|
123
|
+
flushPending();
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
timer ??= deps.setTimeout(flushPending, wait);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const applyPresence = (list) => {
|
|
130
|
+
peers = new Map(list.map((peer) => [peer.id, peer]));
|
|
131
|
+
let dropped = false;
|
|
132
|
+
for (const [id, slot] of slots) {
|
|
133
|
+
const peer = peers.get(id);
|
|
134
|
+
if (!peer) {
|
|
135
|
+
slots.delete(id);
|
|
136
|
+
dropped = true;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (peer.name !== slot.name || peer.hue !== slot.hue) {
|
|
140
|
+
slot.name = peer.name;
|
|
141
|
+
slot.hue = peer.hue;
|
|
142
|
+
slot.setLabel({ name: peer.name, hue: peer.hue });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (dropped) {
|
|
146
|
+
order = order.filter((id) => slots.has(id));
|
|
147
|
+
publishIds();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
const applyCursors = (entries) => {
|
|
151
|
+
for (const entry of entries) {
|
|
152
|
+
const id = entry[0];
|
|
153
|
+
if (id === self)
|
|
154
|
+
continue;
|
|
155
|
+
if (entry.length === 1) {
|
|
156
|
+
const slot = slots.get(id);
|
|
157
|
+
if (slot)
|
|
158
|
+
slot.setPosition({ x: 0, y: 0, shown: false });
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const peer = peers.get(id);
|
|
162
|
+
// Not in the latest presence (left already, or not announced yet).
|
|
163
|
+
if (!peer)
|
|
164
|
+
continue;
|
|
165
|
+
slotFor(id, peer).setPosition({
|
|
166
|
+
x: entry[1] / X_SCALE,
|
|
167
|
+
y: entry[2],
|
|
168
|
+
shown: true,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
return {
|
|
173
|
+
ids,
|
|
174
|
+
styleOf: (id) => {
|
|
175
|
+
const slot = slots.get(id);
|
|
176
|
+
if (!slot)
|
|
177
|
+
return '';
|
|
178
|
+
const p = slot.position();
|
|
179
|
+
const label = slot.label();
|
|
180
|
+
return `transform:translate3d(${Math.round(p.x * width())}px,${Math.round(p.y)}px,0);--cursor-hue:${label.hue};opacity:${p.shown ? 1 : 0}`;
|
|
181
|
+
},
|
|
182
|
+
nameOf: (id) => slots.get(id)?.label().name ?? '',
|
|
183
|
+
setWidth: (px) => setWidthSignal(px > 0 ? px : 1),
|
|
184
|
+
attach: (fn) => {
|
|
185
|
+
send = fn;
|
|
186
|
+
},
|
|
187
|
+
onFrame: (frame) => {
|
|
188
|
+
switch (frame.t) {
|
|
189
|
+
case 'welcome':
|
|
190
|
+
self = frame.self;
|
|
191
|
+
cursorsOn = frame.cursors;
|
|
192
|
+
return;
|
|
193
|
+
case 'presence':
|
|
194
|
+
applyPresence(frame.peers);
|
|
195
|
+
return;
|
|
196
|
+
case 'cursors':
|
|
197
|
+
applyCursors(frame.c);
|
|
198
|
+
return;
|
|
199
|
+
default:
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
onStatus: (status) => {
|
|
204
|
+
if (status === 'Open')
|
|
205
|
+
return;
|
|
206
|
+
// The socket dropped: everything re-announces on the next welcome.
|
|
207
|
+
cancelPending();
|
|
208
|
+
self = '';
|
|
209
|
+
cursorsOn = false;
|
|
210
|
+
peers = new Map();
|
|
211
|
+
sentPosition = false;
|
|
212
|
+
dropAll();
|
|
213
|
+
},
|
|
214
|
+
move: (xFraction, yPx) => {
|
|
215
|
+
if (!mayMove())
|
|
216
|
+
return;
|
|
217
|
+
const frame = encodeCursor(xFraction, yPx);
|
|
218
|
+
if (frame !== undefined)
|
|
219
|
+
queue(frame);
|
|
220
|
+
},
|
|
221
|
+
leave: () => {
|
|
222
|
+
if (sentPosition)
|
|
223
|
+
queue(CURSOR_LEAVE_FRAME);
|
|
224
|
+
else
|
|
225
|
+
cancelPending();
|
|
226
|
+
},
|
|
227
|
+
setHidden: (next) => {
|
|
228
|
+
if (next === hidden)
|
|
229
|
+
return;
|
|
230
|
+
hidden = next;
|
|
231
|
+
if (!next)
|
|
232
|
+
return;
|
|
233
|
+
cancelPending();
|
|
234
|
+
if (sentPosition)
|
|
235
|
+
transmit(CURSOR_LEAVE_FRAME);
|
|
236
|
+
},
|
|
237
|
+
dispose: () => {
|
|
238
|
+
cancelPending();
|
|
239
|
+
send = undefined;
|
|
240
|
+
},
|
|
241
|
+
sent: () => sentCount,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** @jsxImportSource @solidjs/web */
|
|
2
|
+
/**
|
|
3
|
+
* `<LiveCursors room>`: the kit's live cursor overlay, an island part
|
|
4
|
+
* (`@alecsibilia/ui-kit/live/cursors`). Its default export is the island component,
|
|
5
|
+
* so an app adds cursors to a route with one island table entry and a
|
|
6
|
+
* one-line island module:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* // src/islands.ts (hydrated at load: the server renders an empty overlay)
|
|
10
|
+
* liveCursors: { wake: "load", load: () => import("./features/presence/live-cursors-island.tsx") },
|
|
11
|
+
* // live-cursors-island.tsx
|
|
12
|
+
* export { default } from "@alecsibilia/ui-kit/live/cursors";
|
|
13
|
+
* // the page (static):
|
|
14
|
+
* <CursorSurface>
|
|
15
|
+
* ...the page...
|
|
16
|
+
* <Island of={islands.liveCursors} props={{ room: planRoomOf(route) }} />
|
|
17
|
+
* </CursorSurface>
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* Why hydrated, not a lazy wake: hydrating an empty overlay in the page's
|
|
21
|
+
* one pass is measured free; a lazy (`idle`) client render costs ~5 ms of
|
|
22
|
+
* main thread at 4x CPU throttle (`docs/guides/ui-kit-live-rooms.md`, "Client").
|
|
23
|
+
*
|
|
24
|
+
* Opt-in per room type: `room` must be a {@link CursorRoomRef} (a room type
|
|
25
|
+
* declared with `cursors: true`), so a room without cursors is a type
|
|
26
|
+
* error; the room's `welcome` frame says the same at runtime.
|
|
27
|
+
*
|
|
28
|
+
* The overlay covers the nearest `[data-live-cursors-surface]` ancestor
|
|
29
|
+
* (`@alecsibilia/ui-kit/ui` `<CursorSurface>`), reports this viewer's pointer over it and
|
|
30
|
+
* draws the others'. It shares the room's one socket with the page's
|
|
31
|
+
* `presence(...)` subscriptions. Cursor data never becomes a Message
|
|
32
|
+
* (`cursor-engine.ts`).
|
|
33
|
+
*/
|
|
34
|
+
import { type Element as SolidElement } from 'solid-js';
|
|
35
|
+
import type { CursorRoomRef } from '../protocol.ts';
|
|
36
|
+
export interface LiveCursorsProps {
|
|
37
|
+
/** A room whose type declares `cursors: true`. */
|
|
38
|
+
readonly room: CursorRoomRef;
|
|
39
|
+
/** Client send cap in ms (default 50: at most 20 frames/s while moving). */
|
|
40
|
+
readonly sendEveryMs?: number;
|
|
41
|
+
}
|
|
42
|
+
/** The attribute that marks the surface cursors are drawn over. */
|
|
43
|
+
export declare const CURSOR_SURFACE_ATTR = "data-live-cursors-surface";
|
|
44
|
+
export declare function LiveCursors(props: LiveCursorsProps): SolidElement;
|
|
45
|
+
/** The island module's component (`@alecsibilia/ui-kit/live/cursors` default export). */
|
|
46
|
+
export default function LiveCursorsIsland(props: LiveCursorsProps): SolidElement;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/** @jsxImportSource @solidjs/web */
|
|
2
|
+
import { isServer } from '@solidjs/web';
|
|
3
|
+
/**
|
|
4
|
+
* `<LiveCursors room>`: the kit's live cursor overlay, an island part
|
|
5
|
+
* (`@alecsibilia/ui-kit/live/cursors`). Its default export is the island component,
|
|
6
|
+
* so an app adds cursors to a route with one island table entry and a
|
|
7
|
+
* one-line island module:
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* // src/islands.ts (hydrated at load: the server renders an empty overlay)
|
|
11
|
+
* liveCursors: { wake: "load", load: () => import("./features/presence/live-cursors-island.tsx") },
|
|
12
|
+
* // live-cursors-island.tsx
|
|
13
|
+
* export { default } from "@alecsibilia/ui-kit/live/cursors";
|
|
14
|
+
* // the page (static):
|
|
15
|
+
* <CursorSurface>
|
|
16
|
+
* ...the page...
|
|
17
|
+
* <Island of={islands.liveCursors} props={{ room: planRoomOf(route) }} />
|
|
18
|
+
* </CursorSurface>
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* Why hydrated, not a lazy wake: hydrating an empty overlay in the page's
|
|
22
|
+
* one pass is measured free; a lazy (`idle`) client render costs ~5 ms of
|
|
23
|
+
* main thread at 4x CPU throttle (`docs/guides/ui-kit-live-rooms.md`, "Client").
|
|
24
|
+
*
|
|
25
|
+
* Opt-in per room type: `room` must be a {@link CursorRoomRef} (a room type
|
|
26
|
+
* declared with `cursors: true`), so a room without cursors is a type
|
|
27
|
+
* error; the room's `welcome` frame says the same at runtime.
|
|
28
|
+
*
|
|
29
|
+
* The overlay covers the nearest `[data-live-cursors-surface]` ancestor
|
|
30
|
+
* (`@alecsibilia/ui-kit/ui` `<CursorSurface>`), reports this viewer's pointer over it and
|
|
31
|
+
* draws the others'. It shares the room's one socket with the page's
|
|
32
|
+
* `presence(...)` subscriptions. Cursor data never becomes a Message
|
|
33
|
+
* (`cursor-engine.ts`).
|
|
34
|
+
*/
|
|
35
|
+
import { For, onCleanup, onSettled, untrack, } from 'solid-js';
|
|
36
|
+
import { createCursorEngine } from './cursor-engine.js';
|
|
37
|
+
import { useKitScope } from '../../view/app-scope.js';
|
|
38
|
+
/** The attribute that marks the surface cursors are drawn over. */
|
|
39
|
+
export const CURSOR_SURFACE_ATTR = 'data-live-cursors-surface';
|
|
40
|
+
export function LiveCursors(props) {
|
|
41
|
+
const scope = useKitScope();
|
|
42
|
+
const engine = createCursorEngine(untrack(() => props.sendEveryMs));
|
|
43
|
+
let overlay;
|
|
44
|
+
onSettled(() => {
|
|
45
|
+
if (isServer || !scope || !overlay)
|
|
46
|
+
return;
|
|
47
|
+
if (!props.room.cursors) {
|
|
48
|
+
console.error(`[LiveCursors] room "${props.room.id}" does not declare cursors: true`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const layer = overlay;
|
|
52
|
+
const surface = layer.closest(`[${CURSOR_SURFACE_ATTR}]`) ??
|
|
53
|
+
layer.parentElement;
|
|
54
|
+
if (!surface)
|
|
55
|
+
return;
|
|
56
|
+
const handle = scope.rooms().acquire(props.room.path);
|
|
57
|
+
engine.attach(handle.send);
|
|
58
|
+
const welcome = handle.welcome();
|
|
59
|
+
if (welcome)
|
|
60
|
+
engine.onFrame(welcome);
|
|
61
|
+
const known = handle.presence();
|
|
62
|
+
if (known)
|
|
63
|
+
engine.onFrame(known);
|
|
64
|
+
handle.onFrame(engine.onFrame);
|
|
65
|
+
handle.onStatus(engine.onStatus);
|
|
66
|
+
const onMove = (event) => {
|
|
67
|
+
const rect = surface.getBoundingClientRect();
|
|
68
|
+
if (rect.width <= 0)
|
|
69
|
+
return;
|
|
70
|
+
engine.move((event.clientX - rect.left) / rect.width, event.clientY - rect.top);
|
|
71
|
+
};
|
|
72
|
+
const onLeave = () => engine.leave();
|
|
73
|
+
const onVisibility = () => engine.setHidden(document.visibilityState === 'hidden');
|
|
74
|
+
const resize = new ResizeObserver((entries) => {
|
|
75
|
+
const box = entries[0]?.contentRect;
|
|
76
|
+
if (box)
|
|
77
|
+
engine.setWidth(box.width);
|
|
78
|
+
});
|
|
79
|
+
engine.setWidth(surface.getBoundingClientRect().width);
|
|
80
|
+
onVisibility();
|
|
81
|
+
resize.observe(surface);
|
|
82
|
+
surface.addEventListener('pointermove', onMove, { passive: true });
|
|
83
|
+
surface.addEventListener('pointerleave', onLeave, { passive: true });
|
|
84
|
+
document.addEventListener('visibilitychange', onVisibility);
|
|
85
|
+
return () => {
|
|
86
|
+
surface.removeEventListener('pointermove', onMove);
|
|
87
|
+
surface.removeEventListener('pointerleave', onLeave);
|
|
88
|
+
document.removeEventListener('visibilitychange', onVisibility);
|
|
89
|
+
resize.disconnect();
|
|
90
|
+
engine.dispose();
|
|
91
|
+
handle.release();
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
onCleanup(() => engine.dispose());
|
|
95
|
+
return (<div class="ui-live-cursors" aria-hidden="true" data-live-cursors={props.room.id} data-cursor-count={engine.ids().length} ref={(el) => {
|
|
96
|
+
overlay = el;
|
|
97
|
+
}}>
|
|
98
|
+
<For each={engine.ids()} keyed={(id) => id}>
|
|
99
|
+
{(id) => (<div class="ui-live-cursor" data-cursor-peer={id()} style={engine.styleOf(id())}>
|
|
100
|
+
<span class="ui-live-cursor__label">
|
|
101
|
+
{engine.nameOf(id())}
|
|
102
|
+
</span>
|
|
103
|
+
</div>)}
|
|
104
|
+
</For>
|
|
105
|
+
</div>);
|
|
106
|
+
}
|
|
107
|
+
/** The island module's component (`@alecsibilia/ui-kit/live/cursors` default export). */
|
|
108
|
+
export default function LiveCursorsIsland(props) {
|
|
109
|
+
return <LiveCursors room={props.room} sendEveryMs={props.sendEveryMs}/>;
|
|
110
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live rooms: the one declaration an app writes. Plain data (no Effect, no
|
|
3
|
+
* Solid, no DOM, no Cloudflare types), so the browser, the Worker, the room
|
|
4
|
+
* Durable Object, the Alchemy stack and the tests all read the same object.
|
|
5
|
+
*
|
|
6
|
+
* It says, once:
|
|
7
|
+
* - which room types exist and what each one does (presence, cursors,
|
|
8
|
+
* collaborative docs), so a client can't name a room type or a doc that
|
|
9
|
+
* isn't declared (a type error), and the server refuses one at runtime;
|
|
10
|
+
* - what Cloudflare needs for them (the Durable Object class and binding,
|
|
11
|
+
* its local SQLite migration tag, the connect rate limit, the D1 tables
|
|
12
|
+
* the app's hooks touch). `@alecsibilia/ui-kit/alchemy` turns that into Alchemy
|
|
13
|
+
* bindings; `.ui-kit/wrangler.json` (local workerd) is checked against it by a
|
|
14
|
+
* test; the server reads its bindings through it (`liveRoomsEnv`).
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* export const LIVE_ROOMS = declareLiveRooms({
|
|
19
|
+
* durableObject: { binding: "LIVE_ROOM", className: "LiveRoom", migrationTag: "v2" },
|
|
20
|
+
* connectLimit: { binding: "LIVE_CONNECT_LIMIT", namespaceId: "1002", limit: 30, period: 60 },
|
|
21
|
+
* types: {
|
|
22
|
+
* page: { presence: true },
|
|
23
|
+
* plan: { presence: true, cursors: true, docs: ["notes"] },
|
|
24
|
+
* },
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
/** What one room type does. Every capability is opt-in except presence. */
|
|
29
|
+
export interface RoomTypeDecl {
|
|
30
|
+
/** "N people here" (join / leave / list). Default `true`; cheap (rooms sleep between joins). */
|
|
31
|
+
readonly presence?: boolean;
|
|
32
|
+
/** Live cursors (client ≤ 20 frames/s, server fan-out batched every 50 ms). Default `false`. */
|
|
33
|
+
readonly cursors?: boolean;
|
|
34
|
+
/** Collaborative Yjs documents by name (each one persisted in the room's SQLite). Default none. */
|
|
35
|
+
readonly docs?: readonly string[];
|
|
36
|
+
/** Most sockets (presence + doc) one room of this type accepts. Default {@link DEFAULT_MAX_ROOM_SOCKETS}. */
|
|
37
|
+
readonly maxSockets?: number;
|
|
38
|
+
}
|
|
39
|
+
/** The Durable Object every room runs in (one class; one object per room id). */
|
|
40
|
+
export interface RoomObjectDecl {
|
|
41
|
+
/** The Worker `env` key of the namespace (and Alchemy's logical id). */
|
|
42
|
+
readonly binding: string;
|
|
43
|
+
/** The exported class name the Worker entry must export. */
|
|
44
|
+
readonly className: string;
|
|
45
|
+
/**
|
|
46
|
+
* The `migrations[].tag` that adds the class in `.ui-kit/wrangler.json` (local
|
|
47
|
+
* workerd only; Alchemy writes its own migrations on deploy).
|
|
48
|
+
*/
|
|
49
|
+
readonly migrationTag: string;
|
|
50
|
+
}
|
|
51
|
+
/** A Workers Rate Limiting binding on room upgrades, keyed by client IP. */
|
|
52
|
+
export interface ConnectLimitDecl {
|
|
53
|
+
readonly binding: string;
|
|
54
|
+
/** Unique per Cloudflare account. */
|
|
55
|
+
readonly namespaceId: string;
|
|
56
|
+
readonly limit: number;
|
|
57
|
+
/** Cloudflare allows 10 or 60 seconds. */
|
|
58
|
+
readonly period: 10 | 60;
|
|
59
|
+
}
|
|
60
|
+
export interface LiveRoomsDecl<Types extends Readonly<Record<string, RoomTypeDecl>>> {
|
|
61
|
+
readonly durableObject: RoomObjectDecl;
|
|
62
|
+
readonly connectLimit?: ConnectLimitDecl;
|
|
63
|
+
readonly types: Types;
|
|
64
|
+
/**
|
|
65
|
+
* D1 tables the app's room hooks read or write (`authorize`, doc
|
|
66
|
+
* `seed` / `persist`). Documentation plus a test hook: the app's
|
|
67
|
+
* migrations must create them.
|
|
68
|
+
*/
|
|
69
|
+
readonly d1Tables?: readonly string[];
|
|
70
|
+
}
|
|
71
|
+
/** Any declaration (for generic kit code). */
|
|
72
|
+
export type AnyLiveRoomsDecl = LiveRoomsDecl<Readonly<Record<string, RoomTypeDecl>>>;
|
|
73
|
+
/** The declared room type names. */
|
|
74
|
+
export type RoomTypeOf<D extends AnyLiveRoomsDecl> = keyof D['types'] & string;
|
|
75
|
+
/** The declared doc names of one room type (`never` when it has none). */
|
|
76
|
+
export type DocNameOf<D extends AnyLiveRoomsDecl, T extends RoomTypeOf<D>> = D['types'][T] extends {
|
|
77
|
+
readonly docs: readonly (infer N)[];
|
|
78
|
+
} ? N & string : never;
|
|
79
|
+
/** Whether a declared room type takes live cursors (`true` / `false` literal). */
|
|
80
|
+
export type CursorsOf<D extends AnyLiveRoomsDecl, T extends RoomTypeOf<D>> = D['types'][T] extends {
|
|
81
|
+
readonly cursors: true;
|
|
82
|
+
} ? true : false;
|
|
83
|
+
/** Room types that declare `cursors: true`. */
|
|
84
|
+
export type CursorRoomTypeOf<D extends AnyLiveRoomsDecl> = {
|
|
85
|
+
[T in RoomTypeOf<D>]: CursorsOf<D, T> extends true ? T : never;
|
|
86
|
+
}[RoomTypeOf<D>];
|
|
87
|
+
/** Room types that declare at least one doc. */
|
|
88
|
+
export type DocRoomTypeOf<D extends AnyLiveRoomsDecl> = {
|
|
89
|
+
[T in RoomTypeOf<D>]: [DocNameOf<D, T>] extends [never] ? never : T;
|
|
90
|
+
}[RoomTypeOf<D>];
|
|
91
|
+
export declare const DEFAULT_MAX_ROOM_SOCKETS = 64;
|
|
92
|
+
/** Room type names: lowercase, short (they are part of room ids and paths). */
|
|
93
|
+
export declare const ROOM_TYPE_PATTERN: RegExp;
|
|
94
|
+
/** Doc names: same shape as room types. */
|
|
95
|
+
export declare const DOC_NAME_PATTERN: RegExp;
|
|
96
|
+
/**
|
|
97
|
+
* Checks and freezes a live rooms declaration. Throws at module load (the
|
|
98
|
+
* Worker's startup, the build, the test run) with a message naming the bad
|
|
99
|
+
* part, so a typo never reaches a deploy.
|
|
100
|
+
*
|
|
101
|
+
* @throws Error when a type, doc, binding or class name is malformed, or a
|
|
102
|
+
* room type declares a doc twice.
|
|
103
|
+
*/
|
|
104
|
+
export declare function declareLiveRooms<const Types extends Readonly<Record<string, RoomTypeDecl>>>(decl: LiveRoomsDecl<Types>): LiveRoomsDecl<Types>;
|
|
105
|
+
/** A room type's capabilities with the defaults applied. */
|
|
106
|
+
export interface RoomCapabilities {
|
|
107
|
+
readonly presence: boolean;
|
|
108
|
+
readonly cursors: boolean;
|
|
109
|
+
readonly docs: readonly string[];
|
|
110
|
+
readonly maxSockets: number;
|
|
111
|
+
}
|
|
112
|
+
/** {@link RoomCapabilities} of a declared type, or `undefined` for an undeclared one. */
|
|
113
|
+
export declare function roomCapabilities(decl: AnyLiveRoomsDecl, type: string): RoomCapabilities | undefined;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live rooms: the one declaration an app writes. Plain data (no Effect, no
|
|
3
|
+
* Solid, no DOM, no Cloudflare types), so the browser, the Worker, the room
|
|
4
|
+
* Durable Object, the Alchemy stack and the tests all read the same object.
|
|
5
|
+
*
|
|
6
|
+
* It says, once:
|
|
7
|
+
* - which room types exist and what each one does (presence, cursors,
|
|
8
|
+
* collaborative docs), so a client can't name a room type or a doc that
|
|
9
|
+
* isn't declared (a type error), and the server refuses one at runtime;
|
|
10
|
+
* - what Cloudflare needs for them (the Durable Object class and binding,
|
|
11
|
+
* its local SQLite migration tag, the connect rate limit, the D1 tables
|
|
12
|
+
* the app's hooks touch). `@alecsibilia/ui-kit/alchemy` turns that into Alchemy
|
|
13
|
+
* bindings; `.ui-kit/wrangler.json` (local workerd) is checked against it by a
|
|
14
|
+
* test; the server reads its bindings through it (`liveRoomsEnv`).
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* export const LIVE_ROOMS = declareLiveRooms({
|
|
19
|
+
* durableObject: { binding: "LIVE_ROOM", className: "LiveRoom", migrationTag: "v2" },
|
|
20
|
+
* connectLimit: { binding: "LIVE_CONNECT_LIMIT", namespaceId: "1002", limit: 30, period: 60 },
|
|
21
|
+
* types: {
|
|
22
|
+
* page: { presence: true },
|
|
23
|
+
* plan: { presence: true, cursors: true, docs: ["notes"] },
|
|
24
|
+
* },
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export const DEFAULT_MAX_ROOM_SOCKETS = 64;
|
|
29
|
+
/** Room type names: lowercase, short (they are part of room ids and paths). */
|
|
30
|
+
export const ROOM_TYPE_PATTERN = /^[a-z][a-z0-9-]{0,31}$/;
|
|
31
|
+
/** Doc names: same shape as room types. */
|
|
32
|
+
export const DOC_NAME_PATTERN = /^[a-z][a-z0-9-]{0,31}$/;
|
|
33
|
+
/** Binding / class names Cloudflare accepts. */
|
|
34
|
+
const BINDING_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
35
|
+
/**
|
|
36
|
+
* Checks and freezes a live rooms declaration. Throws at module load (the
|
|
37
|
+
* Worker's startup, the build, the test run) with a message naming the bad
|
|
38
|
+
* part, so a typo never reaches a deploy.
|
|
39
|
+
*
|
|
40
|
+
* @throws Error when a type, doc, binding or class name is malformed, or a
|
|
41
|
+
* room type declares a doc twice.
|
|
42
|
+
*/
|
|
43
|
+
export function declareLiveRooms(decl) {
|
|
44
|
+
const fail = (what) => {
|
|
45
|
+
throw new Error(`declareLiveRooms: ${what}`);
|
|
46
|
+
};
|
|
47
|
+
const { binding, className, migrationTag } = decl.durableObject;
|
|
48
|
+
if (!BINDING_PATTERN.test(binding))
|
|
49
|
+
fail(`bad Durable Object binding name "${binding}"`);
|
|
50
|
+
if (!BINDING_PATTERN.test(className))
|
|
51
|
+
fail(`bad Durable Object class name "${className}"`);
|
|
52
|
+
if (migrationTag.length === 0)
|
|
53
|
+
fail('empty migration tag');
|
|
54
|
+
if (decl.connectLimit) {
|
|
55
|
+
const limit = decl.connectLimit;
|
|
56
|
+
if (!BINDING_PATTERN.test(limit.binding))
|
|
57
|
+
fail(`bad rate limit binding name "${limit.binding}"`);
|
|
58
|
+
if (!(limit.limit > 0))
|
|
59
|
+
fail('connect limit must be positive');
|
|
60
|
+
if (limit.period !== 10 && limit.period !== 60)
|
|
61
|
+
fail('connect limit period must be 10 or 60');
|
|
62
|
+
}
|
|
63
|
+
const names = Object.keys(decl.types);
|
|
64
|
+
if (names.length === 0)
|
|
65
|
+
fail('declare at least one room type');
|
|
66
|
+
for (const name of names) {
|
|
67
|
+
if (!ROOM_TYPE_PATTERN.test(name))
|
|
68
|
+
fail(`bad room type name "${name}" (use ${ROOM_TYPE_PATTERN})`);
|
|
69
|
+
const docs = decl.types[name]?.docs ?? [];
|
|
70
|
+
if (new Set(docs).size !== docs.length)
|
|
71
|
+
fail(`room type "${name}" declares a doc twice`);
|
|
72
|
+
for (const doc of docs) {
|
|
73
|
+
if (!DOC_NAME_PATTERN.test(doc))
|
|
74
|
+
fail(`bad doc name "${doc}" in room type "${name}"`);
|
|
75
|
+
}
|
|
76
|
+
const max = decl.types[name]?.maxSockets;
|
|
77
|
+
if (max !== undefined && !(Number.isInteger(max) && max > 0)) {
|
|
78
|
+
fail(`room type "${name}": maxSockets must be a positive integer`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return Object.freeze(decl);
|
|
82
|
+
}
|
|
83
|
+
/** {@link RoomCapabilities} of a declared type, or `undefined` for an undeclared one. */
|
|
84
|
+
export function roomCapabilities(decl, type) {
|
|
85
|
+
if (!Object.hasOwn(decl.types, type))
|
|
86
|
+
return undefined;
|
|
87
|
+
const spec = decl.types[type] ?? {};
|
|
88
|
+
return {
|
|
89
|
+
presence: spec.presence ?? true,
|
|
90
|
+
cursors: spec.cursors ?? false,
|
|
91
|
+
docs: spec.docs ?? [],
|
|
92
|
+
maxSockets: spec.maxSockets ?? DEFAULT_MAX_ROOM_SOCKETS,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@alecsibilia/ui-kit/live`: live rooms, the parts every side shares. The
|
|
3
|
+
* declaration (`declareLiveRooms`), the wire protocol (paths, frames,
|
|
4
|
+
* timings, room and doc refs) and the collaborative text status. Plain
|
|
5
|
+
* data and pure functions: no Solid, no Effect, no DOM.
|
|
6
|
+
*/
|
|
7
|
+
export * from './protocol.ts';
|
|
8
|
+
export * from './collab-status.ts';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@alecsibilia/ui-kit/live`: live rooms, the parts every side shares. The
|
|
3
|
+
* declaration (`declareLiveRooms`), the wire protocol (paths, frames,
|
|
4
|
+
* timings, room and doc refs) and the collaborative text status. Plain
|
|
5
|
+
* data and pure functions: no Solid, no Effect, no DOM.
|
|
6
|
+
*/
|
|
7
|
+
export * from './protocol.js';
|
|
8
|
+
export * from './collab-status.js';
|