-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (38 loc) · 1.05 KB
/
index.js
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
import * as Y from 'yjs';
import { WebsocketProvider } from 'y-websocket';
import * as awarenessProtocol from 'y-protocols/awareness';
import { IndexeddbPersistence } from 'y-indexeddb';
import Room from './Room.js';
/**
* @classdesc Client object that can be used to connect to a Room.
*/
export class SymphonyClient {
#websocketUrl;
#rooms;
constructor(websocketUrl) {
this.#rooms = {};
this.#websocketUrl = websocketUrl;
}
/** Enters a room and returns it.
*
* @param {string} roomId
* @returns A new Room object.
*/
enter(roomId) {
const ydoc = new Y.Doc();
const wsProviderOptions = {
awareness: new awarenessProtocol.Awareness(ydoc),
}
new IndexeddbPersistence(roomId, ydoc);
const wsProvider = new WebsocketProvider(this.#websocketUrl, roomId, ydoc, wsProviderOptions);
this.#rooms[roomId] = wsProvider;
return new Room(wsProvider, ydoc, roomId);
}
/** Leaves a room.*/
leave(roomId) {
const wsProvider = this.#rooms[roomId];
if (wsProvider) {
wsProvider.disconnect();
}
}
}