This repository was archived by the owner on Oct 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathregistry.ts
More file actions
68 lines (59 loc) · 1.56 KB
/
registry.ts
File metadata and controls
68 lines (59 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { actor, setup } from "rivetkit";
export type CursorPosition = {
userId: string;
x: number;
y: number;
timestamp: number;
};
export type TextLabel = {
id: string;
userId: string;
text: string;
x: number;
y: number;
timestamp: number;
};
export const cursorRoom = actor({
// Persistent state that survives restarts: https://rivet.dev/docs/actors/state
state: {
cursors: {} as Record<string, CursorPosition>,
textLabels: [] as TextLabel[],
},
actions: {
// Update cursor position
updateCursor: (c, userId: string, x: number, y: number) => {
const cursor: CursorPosition = { userId, x, y, timestamp: Date.now() };
c.state.cursors[userId] = cursor;
// Send events to all connected clients: https://rivet.dev/docs/actors/events
c.broadcast("cursorMoved", cursor);
return cursor;
},
// Place text on the canvas
placeText: (c, userId: string, text: string, x: number, y: number) => {
const textLabel: TextLabel = {
id: `${userId}-${Date.now()}`,
userId,
text,
x,
y,
timestamp: Date.now(),
};
c.state.textLabels.push(textLabel);
c.broadcast("textPlaced", textLabel);
return textLabel;
},
// Get all cursors
getCursors: (c) => c.state.cursors,
// Get all text labels
getTextLabels: (c) => c.state.textLabels,
// Remove cursor when user disconnects
removeCursor: (c, userId: string) => {
delete c.state.cursors[userId];
c.broadcast("cursorRemoved", userId);
},
},
});
// Register actors for use: https://rivet.dev/docs/setup
export const registry = setup({
use: { cursorRoom },
});