@pluv/client 0.0.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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +17 -0
- package/dist/index.d.ts +193 -0
- package/dist/index.js +1001 -0
- package/dist/index.mjs +977 -0
- package/package.json +31 -0
- package/src/CrdtManager.ts +40 -0
- package/src/CrdtNotifier.ts +36 -0
- package/src/EventNotifier.ts +38 -0
- package/src/OtherNotifier.ts +35 -0
- package/src/PluvClient.ts +81 -0
- package/src/PluvRoom.ts +993 -0
- package/src/StateNotifier.ts +55 -0
- package/src/UsersManager.ts +128 -0
- package/src/createClient.ts +9 -0
- package/src/index.ts +16 -0
- package/src/types.ts +40 -0
- package/src/y.ts +1 -0
- package/tsconfig.json +5 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,977 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
23
|
+
};
|
|
24
|
+
var __async = (__this, __arguments, generator) => {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
var fulfilled = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.next(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var rejected = (value) => {
|
|
34
|
+
try {
|
|
35
|
+
step(generator.throw(value));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/PluvRoom.ts
|
|
46
|
+
import { doc as doc2 } from "@pluv/crdt-yjs";
|
|
47
|
+
|
|
48
|
+
// src/CrdtManager.ts
|
|
49
|
+
import { doc } from "@pluv/crdt-yjs";
|
|
50
|
+
var CrdtManager = class {
|
|
51
|
+
constructor(options = {}) {
|
|
52
|
+
const { encodedState, initialStorage } = options;
|
|
53
|
+
const doc1 = encodedState ? doc().applyUpdate(encodedState) : null;
|
|
54
|
+
if (doc1 && !!Object.keys(doc1.toJSON()).length) {
|
|
55
|
+
this.doc = doc1;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
doc1 == null ? void 0 : doc1.destroy();
|
|
59
|
+
this.doc = doc(initialStorage == null ? void 0 : initialStorage());
|
|
60
|
+
}
|
|
61
|
+
destroy() {
|
|
62
|
+
this.doc.destroy();
|
|
63
|
+
}
|
|
64
|
+
get(key) {
|
|
65
|
+
return this.doc.get(key);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// src/CrdtNotifier.ts
|
|
70
|
+
import { makeSubject, subscribe } from "wonka";
|
|
71
|
+
var CrdtNotifier = class {
|
|
72
|
+
constructor() {
|
|
73
|
+
this.subjects = /* @__PURE__ */ new Map();
|
|
74
|
+
}
|
|
75
|
+
subject(key) {
|
|
76
|
+
const subject = this.subjects.get(key);
|
|
77
|
+
if (subject) {
|
|
78
|
+
return subject;
|
|
79
|
+
}
|
|
80
|
+
const newSubject = makeSubject();
|
|
81
|
+
this.subjects.set(key, newSubject);
|
|
82
|
+
return newSubject;
|
|
83
|
+
}
|
|
84
|
+
subscribe(key, callback) {
|
|
85
|
+
const subject = this.subject(key);
|
|
86
|
+
return subscribe(callback)(subject.source).unsubscribe;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// src/EventNotifier.ts
|
|
91
|
+
import { makeSubject as makeSubject2, subscribe as subscribe2 } from "wonka";
|
|
92
|
+
var EventNotifier = class {
|
|
93
|
+
constructor() {
|
|
94
|
+
this.subjects = /* @__PURE__ */ new Map();
|
|
95
|
+
}
|
|
96
|
+
subject(name) {
|
|
97
|
+
const subject = this.subjects.get(name);
|
|
98
|
+
if (subject)
|
|
99
|
+
return subject;
|
|
100
|
+
const newSubject = makeSubject2();
|
|
101
|
+
this.subjects.set(name, newSubject);
|
|
102
|
+
return newSubject;
|
|
103
|
+
}
|
|
104
|
+
subscribe(name, callback) {
|
|
105
|
+
const subject = this.subject(name);
|
|
106
|
+
return subscribe2(callback)(subject.source).unsubscribe;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// src/OtherNotifier.ts
|
|
111
|
+
import { makeSubject as makeSubject3, subscribe as subscribe3 } from "wonka";
|
|
112
|
+
var OtherNotifier = class {
|
|
113
|
+
constructor() {
|
|
114
|
+
this.subjects = /* @__PURE__ */ new Map();
|
|
115
|
+
}
|
|
116
|
+
subject(name) {
|
|
117
|
+
const subject = this.subjects.get(name);
|
|
118
|
+
if (subject)
|
|
119
|
+
return subject;
|
|
120
|
+
const newSubject = makeSubject3();
|
|
121
|
+
this.subjects.set(name, newSubject);
|
|
122
|
+
return newSubject;
|
|
123
|
+
}
|
|
124
|
+
subscribe(name, callback) {
|
|
125
|
+
const source = this.subject(name).source;
|
|
126
|
+
return subscribe3(callback)(source).unsubscribe;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// src/StateNotifier.ts
|
|
131
|
+
import { makeSubject as makeSubject4, subscribe as subscribe4 } from "wonka";
|
|
132
|
+
var StateNotifier = class {
|
|
133
|
+
constructor() {
|
|
134
|
+
this.subjects = {
|
|
135
|
+
connection: makeSubject4(),
|
|
136
|
+
"my-presence": makeSubject4(),
|
|
137
|
+
myself: makeSubject4(),
|
|
138
|
+
others: makeSubject4()
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
subscribe(name, callback) {
|
|
142
|
+
var _a;
|
|
143
|
+
const source = (_a = this.subjects[name]) == null ? void 0 : _a.source;
|
|
144
|
+
if (!source)
|
|
145
|
+
throw new Error(`Could not subscribe to source: ${name}`);
|
|
146
|
+
return subscribe4(callback)(source).unsubscribe;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// src/types.ts
|
|
151
|
+
var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
|
|
152
|
+
ConnectionState2["Closed"] = "Closed";
|
|
153
|
+
ConnectionState2["Connecting"] = "Connecting";
|
|
154
|
+
ConnectionState2["Open"] = "Open";
|
|
155
|
+
ConnectionState2["Unavailable"] = "Unavailable";
|
|
156
|
+
ConnectionState2["Untouched"] = "Untouched";
|
|
157
|
+
return ConnectionState2;
|
|
158
|
+
})(ConnectionState || {});
|
|
159
|
+
|
|
160
|
+
// src/UsersManager.ts
|
|
161
|
+
var UsersManager = class {
|
|
162
|
+
constructor(config) {
|
|
163
|
+
this._myself = null;
|
|
164
|
+
this._others = /* @__PURE__ */ new Map();
|
|
165
|
+
this._presence = null;
|
|
166
|
+
const { initialPresence, presence = null } = config;
|
|
167
|
+
this._initialPresence = initialPresence;
|
|
168
|
+
this._presence = presence;
|
|
169
|
+
}
|
|
170
|
+
get myself() {
|
|
171
|
+
return this._myself;
|
|
172
|
+
}
|
|
173
|
+
clear() {
|
|
174
|
+
this._others.clear();
|
|
175
|
+
}
|
|
176
|
+
getOther(connectionId) {
|
|
177
|
+
var _a;
|
|
178
|
+
return (_a = this._others.get(connectionId)) != null ? _a : null;
|
|
179
|
+
}
|
|
180
|
+
getOthers() {
|
|
181
|
+
return Array.from(this._others.values());
|
|
182
|
+
}
|
|
183
|
+
patchPresence(connectionId, patch) {
|
|
184
|
+
var _a, _b;
|
|
185
|
+
if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId) {
|
|
186
|
+
this._myself.presence = Object.assign(
|
|
187
|
+
/* @__PURE__ */ Object.create(null),
|
|
188
|
+
this._myself.presence,
|
|
189
|
+
patch
|
|
190
|
+
);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const other = this._others.get(connectionId);
|
|
194
|
+
if (!other)
|
|
195
|
+
return;
|
|
196
|
+
const presence = Object.assign(
|
|
197
|
+
/* @__PURE__ */ Object.create(null),
|
|
198
|
+
other.presence,
|
|
199
|
+
patch
|
|
200
|
+
);
|
|
201
|
+
(_b = this._presence) == null ? void 0 : _b.parse(presence);
|
|
202
|
+
this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
|
|
203
|
+
}
|
|
204
|
+
removeMyself() {
|
|
205
|
+
this._myself = null;
|
|
206
|
+
}
|
|
207
|
+
removeUser(connectionId) {
|
|
208
|
+
this._others.delete(connectionId);
|
|
209
|
+
}
|
|
210
|
+
setMyself(connectionId, user) {
|
|
211
|
+
this._myself = {
|
|
212
|
+
connectionId,
|
|
213
|
+
presence: this._initialPresence,
|
|
214
|
+
user
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
setPresence(connectionId, presence) {
|
|
218
|
+
var _a;
|
|
219
|
+
if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId) {
|
|
220
|
+
this._myself.presence = presence;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const other = this._others.get(connectionId);
|
|
224
|
+
if (!other)
|
|
225
|
+
return;
|
|
226
|
+
this._others.set(connectionId, __spreadProps(__spreadValues({}, other), { presence }));
|
|
227
|
+
}
|
|
228
|
+
setUser(connectionId, user) {
|
|
229
|
+
var _a;
|
|
230
|
+
if (((_a = this._myself) == null ? void 0 : _a.connectionId) === connectionId)
|
|
231
|
+
return;
|
|
232
|
+
const other = this._others.get(connectionId);
|
|
233
|
+
if (other)
|
|
234
|
+
return;
|
|
235
|
+
this._others.set(connectionId, {
|
|
236
|
+
connectionId,
|
|
237
|
+
presence: this._initialPresence,
|
|
238
|
+
user
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// src/PluvRoom.ts
|
|
244
|
+
var HEARTBEAT_INTERVAL_MS = 1e4;
|
|
245
|
+
var PONG_TIMEOUT_MS = 2e3;
|
|
246
|
+
var RECONNECT_TIMEOUT_MS = 3e4;
|
|
247
|
+
var PluvRoom = class {
|
|
248
|
+
constructor(room, options) {
|
|
249
|
+
this._crdtManager = null;
|
|
250
|
+
this._crdtNotifier = new CrdtNotifier();
|
|
251
|
+
this._eventNotifier = new EventNotifier();
|
|
252
|
+
this._initialStorage = null;
|
|
253
|
+
this._intervals = {
|
|
254
|
+
heartbeat: null
|
|
255
|
+
};
|
|
256
|
+
this._otherNotifier = new OtherNotifier();
|
|
257
|
+
this._state = {
|
|
258
|
+
authorization: {
|
|
259
|
+
token: null,
|
|
260
|
+
user: null
|
|
261
|
+
},
|
|
262
|
+
connection: {
|
|
263
|
+
id: null,
|
|
264
|
+
state: "Untouched" /* Untouched */
|
|
265
|
+
},
|
|
266
|
+
webSocket: null
|
|
267
|
+
};
|
|
268
|
+
this._stateNotifier = new StateNotifier();
|
|
269
|
+
this._subscriptions = {
|
|
270
|
+
observeCrdt: null
|
|
271
|
+
};
|
|
272
|
+
this._timeouts = {
|
|
273
|
+
pong: null,
|
|
274
|
+
reconnect: null
|
|
275
|
+
};
|
|
276
|
+
this._windowListeners = null;
|
|
277
|
+
this._wsListeners = null;
|
|
278
|
+
this.event = (name, callback) => {
|
|
279
|
+
return this._eventNotifier.subscribe(name, callback);
|
|
280
|
+
};
|
|
281
|
+
this.getMyPresence = () => {
|
|
282
|
+
var _a, _b;
|
|
283
|
+
return (_b = (_a = this._usersManager.myself) == null ? void 0 : _a.presence) != null ? _b : null;
|
|
284
|
+
};
|
|
285
|
+
this.getMyself = () => {
|
|
286
|
+
return this._usersManager.myself;
|
|
287
|
+
};
|
|
288
|
+
this.getOther = (connectionId) => {
|
|
289
|
+
return this._usersManager.getOther(connectionId);
|
|
290
|
+
};
|
|
291
|
+
this.getOthers = () => {
|
|
292
|
+
return this._usersManager.getOthers();
|
|
293
|
+
};
|
|
294
|
+
this.getStorage = (key) => {
|
|
295
|
+
var _a, _b;
|
|
296
|
+
return (_b = (_a = this._crdtManager) == null ? void 0 : _a.get(key)) != null ? _b : null;
|
|
297
|
+
};
|
|
298
|
+
this.other = (connectionId, callback) => {
|
|
299
|
+
return this._otherNotifier.subscribe(connectionId, callback);
|
|
300
|
+
};
|
|
301
|
+
this.storage = (key, fn) => {
|
|
302
|
+
return this._crdtNotifier.subscribe(key, fn);
|
|
303
|
+
};
|
|
304
|
+
this.subscribe = (name, callback) => {
|
|
305
|
+
return this._stateNotifier.subscribe(name, callback);
|
|
306
|
+
};
|
|
307
|
+
const {
|
|
308
|
+
authEndpoint,
|
|
309
|
+
debug = false,
|
|
310
|
+
initialPresence,
|
|
311
|
+
initialStorage,
|
|
312
|
+
onAuthorizationFail,
|
|
313
|
+
presence,
|
|
314
|
+
wsEndpoint
|
|
315
|
+
} = options;
|
|
316
|
+
this.room = room;
|
|
317
|
+
this._debug = debug;
|
|
318
|
+
this._endpoints = {
|
|
319
|
+
authEndpoint,
|
|
320
|
+
wsEndpoint
|
|
321
|
+
};
|
|
322
|
+
this._listeners = {
|
|
323
|
+
onAuthorizationFail: (error) => {
|
|
324
|
+
this._clearTimeout(this._timeouts.reconnect);
|
|
325
|
+
this._timeouts.reconnect = setTimeout(
|
|
326
|
+
this._reconnect.bind(this),
|
|
327
|
+
RECONNECT_TIMEOUT_MS
|
|
328
|
+
);
|
|
329
|
+
onAuthorizationFail == null ? void 0 : onAuthorizationFail(error);
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
this._usersManager = new UsersManager({
|
|
333
|
+
initialPresence,
|
|
334
|
+
presence
|
|
335
|
+
});
|
|
336
|
+
if (initialStorage)
|
|
337
|
+
this._initialStorage = initialStorage;
|
|
338
|
+
}
|
|
339
|
+
get webSocket() {
|
|
340
|
+
return this._state.webSocket;
|
|
341
|
+
}
|
|
342
|
+
broadcast(message) {
|
|
343
|
+
if (!this._state.webSocket)
|
|
344
|
+
return;
|
|
345
|
+
if (this._state.connection.state !== "Open" /* Open */)
|
|
346
|
+
return;
|
|
347
|
+
this._sendMessage(message);
|
|
348
|
+
}
|
|
349
|
+
connect() {
|
|
350
|
+
return __async(this, null, function* () {
|
|
351
|
+
if (!this._endpoints.wsEndpoint) {
|
|
352
|
+
throw new Error("Must provide an wsEndpoint.");
|
|
353
|
+
}
|
|
354
|
+
const canConnect = [
|
|
355
|
+
"Closed" /* Closed */,
|
|
356
|
+
"Untouched" /* Untouched */
|
|
357
|
+
].some((state) => this._state.connection.state === state);
|
|
358
|
+
if (!canConnect)
|
|
359
|
+
return;
|
|
360
|
+
this._clearTimeout(this._timeouts.reconnect);
|
|
361
|
+
this._updateState((oldState) => {
|
|
362
|
+
oldState.connection.state = "Connecting" /* Connecting */;
|
|
363
|
+
return oldState;
|
|
364
|
+
});
|
|
365
|
+
const url = new URL(this._getWsEndpoint(this.room));
|
|
366
|
+
let authToken = null;
|
|
367
|
+
try {
|
|
368
|
+
authToken = yield this._getAuthorization(this.room);
|
|
369
|
+
} catch (err) {
|
|
370
|
+
this._onAuthorizationFail(err);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
authToken && url.searchParams.set("token", encodeURIComponent(authToken));
|
|
374
|
+
let webSocket;
|
|
375
|
+
try {
|
|
376
|
+
webSocket = new WebSocket(url.toString());
|
|
377
|
+
} catch (err) {
|
|
378
|
+
this._onAuthorizationFail(err);
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
this._updateState((oldState) => {
|
|
382
|
+
oldState.authorization.token = authToken;
|
|
383
|
+
oldState.webSocket = webSocket;
|
|
384
|
+
return oldState;
|
|
385
|
+
});
|
|
386
|
+
this._attachWindowListeners();
|
|
387
|
+
this._attachWsListeners();
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
disconnect() {
|
|
391
|
+
var _a;
|
|
392
|
+
if (!this._endpoints.wsEndpoint) {
|
|
393
|
+
throw new Error("Must provide an wsEndpoint.");
|
|
394
|
+
}
|
|
395
|
+
if (!this._state.webSocket)
|
|
396
|
+
return;
|
|
397
|
+
const canDisconnect = [
|
|
398
|
+
"Connecting" /* Connecting */,
|
|
399
|
+
"Open" /* Open */
|
|
400
|
+
].some((state) => this._state.connection.state === state);
|
|
401
|
+
if (!canDisconnect)
|
|
402
|
+
return;
|
|
403
|
+
this._closeWs();
|
|
404
|
+
(_a = this._crdtManager) == null ? void 0 : _a.destroy();
|
|
405
|
+
this._updateState((oldState) => {
|
|
406
|
+
oldState.authorization.token = null;
|
|
407
|
+
oldState.connection.state = "Closed" /* Closed */;
|
|
408
|
+
oldState.webSocket = null;
|
|
409
|
+
return oldState;
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
getConnection() {
|
|
413
|
+
return Object.freeze(
|
|
414
|
+
JSON.parse(JSON.stringify(this._state.connection))
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
_attachWindowListeners() {
|
|
418
|
+
if (typeof window === "undefined")
|
|
419
|
+
return;
|
|
420
|
+
if (typeof document === "undefined")
|
|
421
|
+
return;
|
|
422
|
+
this._windowListeners = {
|
|
423
|
+
onNavigatorOnline: this._onNavigatorOnline.bind(this),
|
|
424
|
+
onVisibilityChange: this._onVisibilityChange.bind(this)
|
|
425
|
+
};
|
|
426
|
+
window.addEventListener(
|
|
427
|
+
"online",
|
|
428
|
+
this._windowListeners.onNavigatorOnline
|
|
429
|
+
);
|
|
430
|
+
document.addEventListener(
|
|
431
|
+
"visibilitychange",
|
|
432
|
+
this._windowListeners.onVisibilityChange
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
_attachWsListeners() {
|
|
436
|
+
if (!this._state.webSocket)
|
|
437
|
+
return;
|
|
438
|
+
this._wsListeners = {
|
|
439
|
+
onClose: this._onClose.bind(this),
|
|
440
|
+
onError: this._onError.bind(this),
|
|
441
|
+
onMessage: this._onMessage.bind(this),
|
|
442
|
+
onOpen: this._onOpen.bind(this)
|
|
443
|
+
};
|
|
444
|
+
this._state.webSocket.addEventListener(
|
|
445
|
+
"close",
|
|
446
|
+
this._wsListeners.onClose
|
|
447
|
+
);
|
|
448
|
+
this._state.webSocket.addEventListener(
|
|
449
|
+
"error",
|
|
450
|
+
this._wsListeners.onError
|
|
451
|
+
);
|
|
452
|
+
this._state.webSocket.addEventListener(
|
|
453
|
+
"message",
|
|
454
|
+
this._wsListeners.onMessage
|
|
455
|
+
);
|
|
456
|
+
this._state.webSocket.addEventListener(
|
|
457
|
+
"open",
|
|
458
|
+
this._wsListeners.onOpen
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
_clearInterval(interval) {
|
|
462
|
+
if (Number.isNaN(interval))
|
|
463
|
+
return;
|
|
464
|
+
clearInterval(interval);
|
|
465
|
+
}
|
|
466
|
+
_clearTimeout(timeout) {
|
|
467
|
+
if (Number.isNaN(timeout))
|
|
468
|
+
return;
|
|
469
|
+
clearTimeout(timeout);
|
|
470
|
+
}
|
|
471
|
+
_closeWs() {
|
|
472
|
+
var _a, _b;
|
|
473
|
+
if (!this._state.webSocket)
|
|
474
|
+
return;
|
|
475
|
+
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
476
|
+
this._clearInterval(this._intervals.heartbeat);
|
|
477
|
+
this._clearTimeout(this._timeouts.pong);
|
|
478
|
+
this._clearTimeout(this._timeouts.reconnect);
|
|
479
|
+
this._detachWindowListeners();
|
|
480
|
+
this._usersManager.removeMyself();
|
|
481
|
+
const canClose = [WebSocket.CONNECTING, WebSocket.OPEN].some(
|
|
482
|
+
(readyState) => {
|
|
483
|
+
var _a2;
|
|
484
|
+
return readyState === ((_a2 = this._state.webSocket) == null ? void 0 : _a2.readyState);
|
|
485
|
+
}
|
|
486
|
+
);
|
|
487
|
+
if (canClose)
|
|
488
|
+
this._state.webSocket.close();
|
|
489
|
+
this._detachWsListeners();
|
|
490
|
+
this._stateNotifier.subjects["my-presence"].next(null);
|
|
491
|
+
this._stateNotifier.subjects["myself"].next(null);
|
|
492
|
+
}
|
|
493
|
+
_detachWindowListeners() {
|
|
494
|
+
if (!this._windowListeners)
|
|
495
|
+
return;
|
|
496
|
+
if (typeof window === "undefined")
|
|
497
|
+
return;
|
|
498
|
+
if (typeof document === "undefined")
|
|
499
|
+
return;
|
|
500
|
+
window.removeEventListener(
|
|
501
|
+
"online",
|
|
502
|
+
this._windowListeners.onNavigatorOnline
|
|
503
|
+
);
|
|
504
|
+
document.removeEventListener(
|
|
505
|
+
"visibilitychange",
|
|
506
|
+
this._windowListeners.onVisibilityChange
|
|
507
|
+
);
|
|
508
|
+
this._windowListeners = null;
|
|
509
|
+
}
|
|
510
|
+
_detachWsListeners() {
|
|
511
|
+
if (!this._state.webSocket || !this._wsListeners)
|
|
512
|
+
return;
|
|
513
|
+
this._state.webSocket.removeEventListener(
|
|
514
|
+
"close",
|
|
515
|
+
this._wsListeners.onClose
|
|
516
|
+
);
|
|
517
|
+
this._state.webSocket.removeEventListener(
|
|
518
|
+
"error",
|
|
519
|
+
this._wsListeners.onError
|
|
520
|
+
);
|
|
521
|
+
this._state.webSocket.removeEventListener(
|
|
522
|
+
"message",
|
|
523
|
+
this._wsListeners.onMessage
|
|
524
|
+
);
|
|
525
|
+
this._state.webSocket.removeEventListener(
|
|
526
|
+
"open",
|
|
527
|
+
this._wsListeners.onOpen
|
|
528
|
+
);
|
|
529
|
+
this._wsListeners = null;
|
|
530
|
+
}
|
|
531
|
+
_getAuthorization(room) {
|
|
532
|
+
return __async(this, null, function* () {
|
|
533
|
+
if (!this._endpoints.authEndpoint)
|
|
534
|
+
return null;
|
|
535
|
+
if (typeof this._endpoints.authEndpoint === "string") {
|
|
536
|
+
const token2 = yield fetch(this._endpoints.authEndpoint).then((res) => res.text()).then((text2) => text2.trim());
|
|
537
|
+
return token2;
|
|
538
|
+
}
|
|
539
|
+
const config = this._endpoints.authEndpoint(room);
|
|
540
|
+
const endpoint = typeof config === "string" ? config : config.url;
|
|
541
|
+
const options = typeof config === "string" ? void 0 : config.options;
|
|
542
|
+
const token = yield fetch(endpoint, options).then((res) => res.text()).then((text2) => text2.trim());
|
|
543
|
+
return token;
|
|
544
|
+
});
|
|
545
|
+
}
|
|
546
|
+
_getWsEndpoint(room) {
|
|
547
|
+
if (typeof this._endpoints.wsEndpoint === "string") {
|
|
548
|
+
return this._endpoints.wsEndpoint;
|
|
549
|
+
}
|
|
550
|
+
return this._endpoints.wsEndpoint(room);
|
|
551
|
+
}
|
|
552
|
+
_handleExit(message) {
|
|
553
|
+
const { connectionId } = message;
|
|
554
|
+
if (!connectionId)
|
|
555
|
+
return;
|
|
556
|
+
if (!this._state.webSocket)
|
|
557
|
+
throw new Error("Could not find WebSocket");
|
|
558
|
+
this._usersManager.removeUser(connectionId);
|
|
559
|
+
const others = this._usersManager.getOthers();
|
|
560
|
+
this._stateNotifier.subjects.others.next(others);
|
|
561
|
+
this._otherNotifier.subject(connectionId).next(null);
|
|
562
|
+
this._otherNotifier.subjects.delete(connectionId);
|
|
563
|
+
}
|
|
564
|
+
_handlePresenceUpdatedMessage(message) {
|
|
565
|
+
var _a, _b, _c;
|
|
566
|
+
const { connectionId } = message;
|
|
567
|
+
if (!connectionId)
|
|
568
|
+
return;
|
|
569
|
+
if (!this._state.webSocket)
|
|
570
|
+
throw new Error("Could not find WebSocket");
|
|
571
|
+
const data = message.data;
|
|
572
|
+
this._usersManager.patchPresence(
|
|
573
|
+
connectionId,
|
|
574
|
+
data.presence
|
|
575
|
+
);
|
|
576
|
+
const myPresence = (_b = (_a = this._usersManager.myself) == null ? void 0 : _a.presence) != null ? _b : null;
|
|
577
|
+
const myself = (_c = this._usersManager.myself) != null ? _c : null;
|
|
578
|
+
if ((myself == null ? void 0 : myself.connectionId) === connectionId) {
|
|
579
|
+
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
580
|
+
this._stateNotifier.subjects["myself"].next(myself);
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const other = this._usersManager.getOther(connectionId);
|
|
584
|
+
const others = this._usersManager.getOthers();
|
|
585
|
+
this._otherNotifier.subject(connectionId).next(other);
|
|
586
|
+
this._stateNotifier.subjects["others"].next(others);
|
|
587
|
+
}
|
|
588
|
+
_handleReceiveOthers(message) {
|
|
589
|
+
if (!message.connectionId)
|
|
590
|
+
return;
|
|
591
|
+
if (!this._state.webSocket)
|
|
592
|
+
throw new Error("Could not find WebSocket");
|
|
593
|
+
const data = message.data;
|
|
594
|
+
Object.keys(data.others).forEach((connectionId) => {
|
|
595
|
+
const { presence, user } = data.others[connectionId];
|
|
596
|
+
if (!user)
|
|
597
|
+
return;
|
|
598
|
+
this._usersManager.setUser(connectionId, user);
|
|
599
|
+
const other = this._usersManager.getOther(connectionId);
|
|
600
|
+
!!presence && this._usersManager.patchPresence(
|
|
601
|
+
connectionId,
|
|
602
|
+
presence
|
|
603
|
+
);
|
|
604
|
+
this._otherNotifier.subject(connectionId).next(other);
|
|
605
|
+
});
|
|
606
|
+
const others = this._usersManager.getOthers();
|
|
607
|
+
this._stateNotifier.subjects["others"].next(others);
|
|
608
|
+
}
|
|
609
|
+
_handleRegisteredMessage(message) {
|
|
610
|
+
var _a, _b, _c;
|
|
611
|
+
const { connectionId, user } = message;
|
|
612
|
+
if (!connectionId)
|
|
613
|
+
return;
|
|
614
|
+
if (!this._state.webSocket)
|
|
615
|
+
throw new Error("Could not find WebSocket");
|
|
616
|
+
this._updateState((oldState) => {
|
|
617
|
+
oldState.connection.id = connectionId;
|
|
618
|
+
oldState.authorization.user = user;
|
|
619
|
+
return oldState;
|
|
620
|
+
});
|
|
621
|
+
this._usersManager.setMyself(
|
|
622
|
+
connectionId,
|
|
623
|
+
user
|
|
624
|
+
);
|
|
625
|
+
const myPresence = (_b = (_a = this._usersManager.myself) == null ? void 0 : _a.presence) != null ? _b : null;
|
|
626
|
+
const myself = (_c = this._usersManager.myself) != null ? _c : null;
|
|
627
|
+
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
628
|
+
this._stateNotifier.subjects["myself"].next(myself);
|
|
629
|
+
this._sendMessage({ type: "$GET_OTHERS", data: {} });
|
|
630
|
+
}
|
|
631
|
+
_handleStorageReceivedMessage(message) {
|
|
632
|
+
var _a;
|
|
633
|
+
const { connectionId } = message;
|
|
634
|
+
if (!connectionId)
|
|
635
|
+
return;
|
|
636
|
+
if (!this._state.webSocket)
|
|
637
|
+
throw new Error("Could not find WebSocket");
|
|
638
|
+
const data = message.data;
|
|
639
|
+
this._crdtManager = new CrdtManager({
|
|
640
|
+
encodedState: data.state,
|
|
641
|
+
initialStorage: (_a = this._initialStorage) != null ? _a : void 0
|
|
642
|
+
});
|
|
643
|
+
this._observeCrdt();
|
|
644
|
+
const sharedTypes = this._crdtManager.doc.getSharedTypes();
|
|
645
|
+
Object.keys(sharedTypes).forEach((key) => {
|
|
646
|
+
const sharedType = sharedTypes[key];
|
|
647
|
+
const serialized = sharedType.toJSON();
|
|
648
|
+
this._crdtNotifier.subject(key).next(serialized);
|
|
649
|
+
});
|
|
650
|
+
const update = this._crdtManager.doc.encodeStateAsUpdate();
|
|
651
|
+
this._sendMessage({
|
|
652
|
+
type: "$UPDATE_STORAGE",
|
|
653
|
+
data: { origin: "$STORAGE_RECEIVED", update }
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
_handleStorageUpdatedMessage(message) {
|
|
657
|
+
const { connectionId } = message;
|
|
658
|
+
if (!connectionId)
|
|
659
|
+
return;
|
|
660
|
+
if (!this._state.webSocket)
|
|
661
|
+
throw new Error("Could not find WebSocket");
|
|
662
|
+
const data = message.data;
|
|
663
|
+
if (!this._crdtManager)
|
|
664
|
+
return;
|
|
665
|
+
const test = doc2({});
|
|
666
|
+
test.applyUpdate(data.state);
|
|
667
|
+
this._crdtManager.doc.applyUpdate(data.state, "$STORAGE_UPDATED");
|
|
668
|
+
const sharedTypes = this._crdtManager.doc.getSharedTypes();
|
|
669
|
+
Object.keys(sharedTypes).forEach((key) => {
|
|
670
|
+
if (!this._crdtManager)
|
|
671
|
+
return;
|
|
672
|
+
const sharedType = sharedTypes[key];
|
|
673
|
+
if (!sharedType)
|
|
674
|
+
return;
|
|
675
|
+
const serialized = sharedType.toJSON();
|
|
676
|
+
this._crdtNotifier.subject(key).next(serialized);
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
_handleUserJoinedMessage(message) {
|
|
680
|
+
const { connectionId } = message;
|
|
681
|
+
if (!connectionId)
|
|
682
|
+
return;
|
|
683
|
+
if (!this._state.webSocket)
|
|
684
|
+
throw new Error("Could not find WebSocket");
|
|
685
|
+
if (!this._usersManager.myself)
|
|
686
|
+
return;
|
|
687
|
+
const data = message.data;
|
|
688
|
+
this._usersManager.setUser(connectionId, data.user);
|
|
689
|
+
const other = this._usersManager.getOther(connectionId);
|
|
690
|
+
const others = this._usersManager.getOthers();
|
|
691
|
+
this._otherNotifier.subject(connectionId).next(other);
|
|
692
|
+
this._stateNotifier.subjects["others"].next(others);
|
|
693
|
+
}
|
|
694
|
+
_heartbeat() {
|
|
695
|
+
if (!this._state.webSocket)
|
|
696
|
+
return;
|
|
697
|
+
if (!this._state.connection.id)
|
|
698
|
+
return;
|
|
699
|
+
if (this._state.webSocket.readyState !== this._state.webSocket.OPEN) {
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
this._clearTimeout(this._timeouts.pong);
|
|
703
|
+
this._timeouts.pong = setTimeout(
|
|
704
|
+
this._reconnect.bind(this),
|
|
705
|
+
PONG_TIMEOUT_MS
|
|
706
|
+
);
|
|
707
|
+
this._sendMessage({ type: "$PING", data: {} });
|
|
708
|
+
}
|
|
709
|
+
_logDebug(...data) {
|
|
710
|
+
var _a;
|
|
711
|
+
if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
|
|
712
|
+
return;
|
|
713
|
+
this._debug && console.log(...data);
|
|
714
|
+
}
|
|
715
|
+
_observeCrdt() {
|
|
716
|
+
var _a, _b;
|
|
717
|
+
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
718
|
+
if (!this._crdtManager)
|
|
719
|
+
return;
|
|
720
|
+
const unsubscribe = this._crdtManager.doc.subscribe(
|
|
721
|
+
(update, _origin) => {
|
|
722
|
+
const origin = _origin != null ? _origin : null;
|
|
723
|
+
if (!this._crdtManager)
|
|
724
|
+
return;
|
|
725
|
+
if (!this._state.webSocket)
|
|
726
|
+
return;
|
|
727
|
+
if (!this._state.connection.id)
|
|
728
|
+
return;
|
|
729
|
+
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
730
|
+
return;
|
|
731
|
+
if (origin === "$STORAGE_UPDATED")
|
|
732
|
+
return;
|
|
733
|
+
this._sendMessage({
|
|
734
|
+
type: "$UPDATE_STORAGE",
|
|
735
|
+
data: { origin, update }
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
);
|
|
739
|
+
this._subscriptions.observeCrdt = unsubscribe;
|
|
740
|
+
}
|
|
741
|
+
_onAuthorizationFail(error) {
|
|
742
|
+
const authError = error instanceof Error ? error : new Error("Authorization failed");
|
|
743
|
+
this._listeners.onAuthorizationFail(authError);
|
|
744
|
+
this._updateState((oldState) => {
|
|
745
|
+
oldState.connection.state = "Unavailable" /* Unavailable */;
|
|
746
|
+
return oldState;
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
_onClose(event) {
|
|
750
|
+
this._logDebug("WebSocket closed");
|
|
751
|
+
!!event.reason && this._logDebug(event.reason);
|
|
752
|
+
this._clearInterval(this._intervals.heartbeat);
|
|
753
|
+
this._clearTimeout(this._timeouts.pong);
|
|
754
|
+
this._updateState((oldState) => {
|
|
755
|
+
oldState.connection.state = "Closed" /* Closed */;
|
|
756
|
+
oldState.webSocket = null;
|
|
757
|
+
return oldState;
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
_onError() {
|
|
761
|
+
}
|
|
762
|
+
_onMessage(event) {
|
|
763
|
+
const message = this._parseMessage(event);
|
|
764
|
+
if (!message)
|
|
765
|
+
return;
|
|
766
|
+
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.output.find((value) => value === message.type);
|
|
767
|
+
if (shouldLog) {
|
|
768
|
+
this._logDebug("WebSocket event received: ", message.type, message);
|
|
769
|
+
}
|
|
770
|
+
this._eventNotifier.subject(message.type).next(message);
|
|
771
|
+
const { connectionId } = message;
|
|
772
|
+
if (!connectionId)
|
|
773
|
+
return;
|
|
774
|
+
switch (message.type) {
|
|
775
|
+
case "$EXIT": {
|
|
776
|
+
this._handleExit(message);
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
case "$OTHERS_RECEIVED": {
|
|
780
|
+
this._handleReceiveOthers(message);
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
case "$PONG": {
|
|
784
|
+
this._clearTimeout(this._timeouts.pong);
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
case "$PRESENCE_UPDATED": {
|
|
788
|
+
this._handlePresenceUpdatedMessage(message);
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
case "$REGISTERED": {
|
|
792
|
+
this._handleRegisteredMessage(message);
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
case "$STORAGE_RECEIVED": {
|
|
796
|
+
this._handleStorageReceivedMessage(message);
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
case "$STORAGE_UPDATED": {
|
|
800
|
+
this._handleStorageUpdatedMessage(message);
|
|
801
|
+
return;
|
|
802
|
+
}
|
|
803
|
+
case "$USER_JOINED": {
|
|
804
|
+
this._handleUserJoinedMessage(message);
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
default:
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
_onOpen() {
|
|
811
|
+
if (this._state.connection.state !== "Connecting" /* Connecting */)
|
|
812
|
+
return;
|
|
813
|
+
this._logDebug("WebSocket connected");
|
|
814
|
+
this._updateState((oldState) => {
|
|
815
|
+
oldState.connection.state = "Open" /* Open */;
|
|
816
|
+
return oldState;
|
|
817
|
+
});
|
|
818
|
+
this._clearInterval(this._intervals.heartbeat);
|
|
819
|
+
this._intervals.heartbeat = setInterval(
|
|
820
|
+
this._heartbeat.bind(this),
|
|
821
|
+
HEARTBEAT_INTERVAL_MS
|
|
822
|
+
);
|
|
823
|
+
}
|
|
824
|
+
_onNavigatorOnline() {
|
|
825
|
+
return __async(this, null, function* () {
|
|
826
|
+
if (this._state.connection.state !== "Unavailable" /* Unavailable */) {
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
yield this._reconnect();
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
_onVisibilityChange() {
|
|
833
|
+
return __async(this, null, function* () {
|
|
834
|
+
if (typeof document === "undefined")
|
|
835
|
+
return;
|
|
836
|
+
if (document.visibilityState !== "visible")
|
|
837
|
+
return;
|
|
838
|
+
switch (this._state.connection.state) {
|
|
839
|
+
case "Open" /* Open */:
|
|
840
|
+
this._clearInterval(this._intervals.heartbeat);
|
|
841
|
+
setInterval(this._heartbeat.bind(this), HEARTBEAT_INTERVAL_MS);
|
|
842
|
+
return;
|
|
843
|
+
case "Unavailable" /* Unavailable */:
|
|
844
|
+
yield this._reconnect();
|
|
845
|
+
return;
|
|
846
|
+
default:
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
_parseMessage(message) {
|
|
851
|
+
if (typeof message.data !== "string")
|
|
852
|
+
return null;
|
|
853
|
+
try {
|
|
854
|
+
const config = JSON.parse(message.data);
|
|
855
|
+
if (typeof config !== "object")
|
|
856
|
+
return null;
|
|
857
|
+
if (typeof config.type !== "string")
|
|
858
|
+
return null;
|
|
859
|
+
if (typeof config.data !== "object")
|
|
860
|
+
return null;
|
|
861
|
+
return config;
|
|
862
|
+
} catch (e) {
|
|
863
|
+
return null;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
_reconnect() {
|
|
867
|
+
return __async(this, null, function* () {
|
|
868
|
+
if (!this._endpoints.wsEndpoint) {
|
|
869
|
+
throw new Error("Must provide an wsEndpoint.");
|
|
870
|
+
}
|
|
871
|
+
if (!this._state.webSocket)
|
|
872
|
+
return;
|
|
873
|
+
this._closeWs();
|
|
874
|
+
this._updateState((oldState) => {
|
|
875
|
+
oldState.authorization.token = null;
|
|
876
|
+
oldState.connection.state = "Unavailable" /* Unavailable */;
|
|
877
|
+
oldState.webSocket = null;
|
|
878
|
+
return oldState;
|
|
879
|
+
});
|
|
880
|
+
yield this.connect();
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
_sendMessage(data) {
|
|
884
|
+
const webSocket = this._state.webSocket;
|
|
885
|
+
if ((webSocket == null ? void 0 : webSocket.readyState) !== WebSocket.OPEN)
|
|
886
|
+
return;
|
|
887
|
+
const shouldLog = typeof this._debug === "boolean" ? this._debug : this._debug.input.find((value) => value === data.type);
|
|
888
|
+
if (shouldLog) {
|
|
889
|
+
this._logDebug("WebSocket event sent: ", data.type, data);
|
|
890
|
+
}
|
|
891
|
+
webSocket.send(JSON.stringify(data));
|
|
892
|
+
}
|
|
893
|
+
_updateState(updater) {
|
|
894
|
+
let authorization;
|
|
895
|
+
try {
|
|
896
|
+
authorization = JSON.parse(
|
|
897
|
+
JSON.stringify(this._state.authorization)
|
|
898
|
+
);
|
|
899
|
+
} catch (e) {
|
|
900
|
+
throw new Error("User is not JSON serializable");
|
|
901
|
+
}
|
|
902
|
+
const oldState = __spreadProps(__spreadValues({}, this._state), {
|
|
903
|
+
authorization,
|
|
904
|
+
connection: JSON.parse(JSON.stringify(this._state.connection)),
|
|
905
|
+
webSocket: this._state.webSocket
|
|
906
|
+
});
|
|
907
|
+
const newState = updater(oldState);
|
|
908
|
+
this._logDebug("WebSocket new state: ", newState);
|
|
909
|
+
this._state = newState;
|
|
910
|
+
this._stateNotifier.subjects.connection.next(newState);
|
|
911
|
+
return newState;
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
|
|
915
|
+
// src/PluvClient.ts
|
|
916
|
+
var PluvClient = class {
|
|
917
|
+
constructor(options) {
|
|
918
|
+
this._rooms = /* @__PURE__ */ new Map();
|
|
919
|
+
this.createRoom = (room, options) => {
|
|
920
|
+
const oldRoom = this.getRoom(room);
|
|
921
|
+
if (oldRoom)
|
|
922
|
+
return oldRoom;
|
|
923
|
+
const newRoom = new PluvRoom(room, {
|
|
924
|
+
authEndpoint: this._authEndpoint,
|
|
925
|
+
debug: options.debug,
|
|
926
|
+
initialPresence: options.initialPresence,
|
|
927
|
+
initialStorage: options.initialStorage,
|
|
928
|
+
onAuthorizationFail: options.onAuthorizationFail,
|
|
929
|
+
wsEndpoint: this._wsEndpoint
|
|
930
|
+
});
|
|
931
|
+
this._rooms.set(room, newRoom);
|
|
932
|
+
return newRoom;
|
|
933
|
+
};
|
|
934
|
+
this.enter = (room) => __async(this, null, function* () {
|
|
935
|
+
const toEnter = this.getRoom(room);
|
|
936
|
+
if (!toEnter) {
|
|
937
|
+
throw new Error(`Could not find room: ${room}.`);
|
|
938
|
+
}
|
|
939
|
+
yield toEnter.connect();
|
|
940
|
+
});
|
|
941
|
+
this.getRoom = (room) => {
|
|
942
|
+
const found = this._rooms.get(room);
|
|
943
|
+
return found != null ? found : null;
|
|
944
|
+
};
|
|
945
|
+
this.leave = (room) => {
|
|
946
|
+
const toLeave = this.getRoom(room);
|
|
947
|
+
if (!toLeave)
|
|
948
|
+
return;
|
|
949
|
+
toLeave.disconnect();
|
|
950
|
+
this._rooms.delete(room);
|
|
951
|
+
};
|
|
952
|
+
this._authEndpoint = options.authEndpoint;
|
|
953
|
+
this._wsEndpoint = options.wsEndpoint;
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
|
|
957
|
+
// src/createClient.ts
|
|
958
|
+
var createClient = (options) => {
|
|
959
|
+
return new PluvClient(options);
|
|
960
|
+
};
|
|
961
|
+
|
|
962
|
+
// src/y.ts
|
|
963
|
+
var y_exports = {};
|
|
964
|
+
__export(y_exports, {
|
|
965
|
+
array: () => array,
|
|
966
|
+
map: () => map,
|
|
967
|
+
text: () => text,
|
|
968
|
+
unstable__object: () => unstable__object
|
|
969
|
+
});
|
|
970
|
+
import { array, map, text, unstable__object } from "@pluv/crdt-yjs";
|
|
971
|
+
export {
|
|
972
|
+
ConnectionState,
|
|
973
|
+
PluvClient,
|
|
974
|
+
PluvRoom,
|
|
975
|
+
createClient,
|
|
976
|
+
y_exports as y
|
|
977
|
+
};
|