-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
113 lines (107 loc) · 2.45 KB
/
index.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* 2D Vector type
*/
export type Vector2D = {
x: number;
y: number;
};
/**
* Type with callbacks for Peer channel manipulation
*/
type ConnectedPeer = {
connection: {
send: <T>(data: T) => void;
close: () => void;
};
};
/**
* Self-tracking info such as Peer Relay ID and Peer channel setup callback,
*/
type ConnectedSelf = {
id: PeerID;
connect: (peerId: PeerID) => void;
};
/**
* Message Template for items on the Data Channel
*/
export type Message<T extends string, D> = D & {
type: T;
};
/**
* Type Alias for Peer IDs to keep things consistent
*/
export type PeerID = string;
/**
* Generic Type representing the structure of data stored within each Client.
*/
export type RoomData<
PeerExtension = object,
RoomExtension = object,
ObjectExtension = object,
> = RoomExtension & {
/**
* Attributes stored about the User,
*/
self: ConnectedSelf & PeerExtension;
/**
* Attributes stored for every Peer in the Session
*/
peers: Record<PeerID, ConnectedPeer & PeerExtension>;
/**
* Attributes on objects shared throughout the Session
*/
objects: Record<number, ObjectExtension>;
};
/**
* Custom Plugin Definition
*/
export type RoomPlugin<
PeerExtension = object,
RoomExtension = object,
ObjectExtension = object,
MessageType = never,
> = {
/**
* Name used to identify the plugin
*/
name: string;
/**
* Optional list of plugin dependencies
*/
dependencies?: string[];
/**
* Cleanup procedure for the plugin
*/
cleanup?(room: RoomData<PeerExtension, RoomExtension, ObjectExtension>): void;
/**
* Setup for self upon connecting
* @param room RoomData reference
*/
initialize?(room: RoomData<PeerExtension, RoomExtension, ObjectExtension>): void;
/**
* Setup for peers when connecting to self
* @param room RoomData reference
* @param peerId ID of the connecting peer
*/
peerSetup?(room: RoomData<PeerExtension, RoomExtension, ObjectExtension>, peerId: PeerID): void;
/**
* Handler for incoming messages from peers
* @param room RoomData reference
* @param data peer message
* @param peerId ID of the peer which the data comes from
*/
processMessage?(
room: RoomData<PeerExtension, RoomExtension, ObjectExtension>,
data: MessageType,
peerId: PeerID,
): void;
/**
* Handler for when a peer has disconnected
* @param room RoomData reference
* @param peerId ID the peer
*/
handlePeerDisconnect?(
room: RoomData<PeerExtension, RoomExtension, ObjectExtension>,
peerId: PeerID,
): void;
};