@pluv/client 0.14.1 → 0.16.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/.turbo/turbo-build.log +9 -9
- package/CHANGELOG.md +189 -37
- package/README.md +9 -12
- package/dist/index.d.mts +65 -77
- package/dist/index.d.ts +65 -77
- package/dist/index.js +271 -303
- package/dist/index.mjs +270 -313
- package/package.json +7 -11
- package/src/AbstractRoom.ts +13 -8
- package/src/CrdtManager.ts +55 -57
- package/src/CrdtNotifier.ts +12 -12
- package/src/MockedRoom.ts +44 -40
- package/src/OtherNotifier.ts +1 -1
- package/src/PluvClient.ts +4 -4
- package/src/PluvRoom.ts +67 -76
- package/src/StateNotifier.ts +8 -5
- package/src/index.ts +6 -11
- package/src/y.ts +0 -9
package/dist/index.mjs
CHANGED
|
@@ -17,10 +17,6 @@ var __spreadValues = (a, b) => {
|
|
|
17
17
|
return a;
|
|
18
18
|
};
|
|
19
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
20
|
var __async = (__this, __arguments, generator) => {
|
|
25
21
|
return new Promise((resolve, reject) => {
|
|
26
22
|
var fulfilled = (value) => {
|
|
@@ -57,25 +53,16 @@ var AbstractStorageStore = class {
|
|
|
57
53
|
};
|
|
58
54
|
|
|
59
55
|
// src/CrdtManager.ts
|
|
60
|
-
import {
|
|
56
|
+
import { noop } from "@pluv/crdt";
|
|
61
57
|
var CrdtManager = class {
|
|
62
|
-
constructor(options
|
|
58
|
+
constructor(options) {
|
|
63
59
|
this.initialized = false;
|
|
64
60
|
const {
|
|
65
|
-
captureTimeout,
|
|
66
61
|
encodedState,
|
|
67
|
-
initialStorage
|
|
68
|
-
trackedOrigins = []
|
|
62
|
+
initialStorage = noop.doc({})
|
|
69
63
|
} = options;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.doc = _doc;
|
|
73
|
-
this.trackOrigins({ captureTimeout, trackedOrigins });
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
_doc == null ? void 0 : _doc.destroy();
|
|
77
|
-
this.doc = doc(initialStorage == null ? void 0 : initialStorage());
|
|
78
|
-
this.trackOrigins({ captureTimeout, trackedOrigins });
|
|
64
|
+
this._docFactory = initialStorage;
|
|
65
|
+
this.doc = encodedState ? initialStorage.getFresh().applyEncodedState({ update: encodedState }) : initialStorage.getFresh();
|
|
79
66
|
}
|
|
80
67
|
/**
|
|
81
68
|
* !HACK
|
|
@@ -97,41 +84,43 @@ var CrdtManager = class {
|
|
|
97
84
|
return this.doc.get(key);
|
|
98
85
|
}
|
|
99
86
|
initialize(params) {
|
|
100
|
-
const {
|
|
101
|
-
captureTimeout,
|
|
102
|
-
onInitialized,
|
|
103
|
-
origin,
|
|
104
|
-
trackedOrigins,
|
|
105
|
-
update
|
|
106
|
-
} = params;
|
|
87
|
+
const { onInitialized, origin, update } = params;
|
|
107
88
|
const updates = typeof update === "string" ? [update] : update;
|
|
108
|
-
this.trackOrigins({ captureTimeout, trackedOrigins });
|
|
109
89
|
if (!updates.length)
|
|
110
90
|
return this;
|
|
111
91
|
if (this.initialized) {
|
|
112
|
-
this._applyDocUpdates(this.doc, updates, origin);
|
|
92
|
+
this.doc = this._applyDocUpdates(this.doc, updates, origin).track();
|
|
113
93
|
return this;
|
|
114
94
|
}
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
95
|
+
const onEmpty = this._applyDocUpdates(
|
|
96
|
+
this._docFactory.getEmpty(),
|
|
97
|
+
updates,
|
|
98
|
+
origin
|
|
99
|
+
);
|
|
100
|
+
if (!onEmpty.isEmpty()) {
|
|
101
|
+
onEmpty.destroy();
|
|
102
|
+
this.doc = this._applyDocUpdates(
|
|
103
|
+
this._docFactory.getFresh(),
|
|
104
|
+
updates,
|
|
105
|
+
origin
|
|
106
|
+
).track();
|
|
107
|
+
this.initialized = true;
|
|
108
|
+
onInitialized == null ? void 0 : onInitialized(this.doc.getEncodedState());
|
|
109
|
+
return this;
|
|
121
110
|
}
|
|
122
|
-
|
|
123
|
-
this.
|
|
111
|
+
onEmpty.destroy();
|
|
112
|
+
this.doc = this._docFactory.getInitialized().track();
|
|
113
|
+
this.initialized = true;
|
|
114
|
+
onInitialized == null ? void 0 : onInitialized(this.doc.getEncodedState());
|
|
124
115
|
return this;
|
|
125
116
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
yjsDoc.transact(() => {
|
|
132
|
-
updates.reduce((acc, _update) => acc.applyUpdate(_update), yjsDoc);
|
|
117
|
+
_applyDocUpdates(doc, updates, origin) {
|
|
118
|
+
doc.transact(() => {
|
|
119
|
+
doc.batchApplyEncodedState(
|
|
120
|
+
updates.map((update) => ({ origin, update }))
|
|
121
|
+
);
|
|
133
122
|
}, origin);
|
|
134
|
-
return
|
|
123
|
+
return doc;
|
|
135
124
|
}
|
|
136
125
|
};
|
|
137
126
|
|
|
@@ -208,7 +197,8 @@ var StateNotifier = class {
|
|
|
208
197
|
connection: makeSubject4(),
|
|
209
198
|
"my-presence": makeSubject4(),
|
|
210
199
|
myself: makeSubject4(),
|
|
211
|
-
others: makeSubject4()
|
|
200
|
+
others: makeSubject4(),
|
|
201
|
+
"storage-loaded": makeSubject4()
|
|
212
202
|
};
|
|
213
203
|
}
|
|
214
204
|
subscribe(name, callback) {
|
|
@@ -220,41 +210,6 @@ var StateNotifier = class {
|
|
|
220
210
|
}
|
|
221
211
|
};
|
|
222
212
|
|
|
223
|
-
// src/StorageStore.ts
|
|
224
|
-
var StorageStore = class extends AbstractStorageStore {
|
|
225
|
-
addUpdate() {
|
|
226
|
-
return Promise.resolve();
|
|
227
|
-
}
|
|
228
|
-
applyUpdates() {
|
|
229
|
-
return Promise.resolve();
|
|
230
|
-
}
|
|
231
|
-
destroy() {
|
|
232
|
-
return Promise.resolve();
|
|
233
|
-
}
|
|
234
|
-
flatten() {
|
|
235
|
-
return Promise.resolve();
|
|
236
|
-
}
|
|
237
|
-
getShouldFlatten() {
|
|
238
|
-
return Promise.resolve(false);
|
|
239
|
-
}
|
|
240
|
-
getUpdates() {
|
|
241
|
-
return Promise.resolve([]);
|
|
242
|
-
}
|
|
243
|
-
initialize() {
|
|
244
|
-
return Promise.resolve();
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
// src/types.ts
|
|
249
|
-
var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
|
|
250
|
-
ConnectionState2["Closed"] = "Closed";
|
|
251
|
-
ConnectionState2["Connecting"] = "Connecting";
|
|
252
|
-
ConnectionState2["Open"] = "Open";
|
|
253
|
-
ConnectionState2["Unavailable"] = "Unavailable";
|
|
254
|
-
ConnectionState2["Untouched"] = "Untouched";
|
|
255
|
-
return ConnectionState2;
|
|
256
|
-
})(ConnectionState || {});
|
|
257
|
-
|
|
258
213
|
// src/UsersManager.ts
|
|
259
214
|
var UsersManager = class {
|
|
260
215
|
constructor(config) {
|
|
@@ -350,6 +305,195 @@ var UsersManager = class {
|
|
|
350
305
|
}
|
|
351
306
|
};
|
|
352
307
|
|
|
308
|
+
// src/types.ts
|
|
309
|
+
var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
|
|
310
|
+
ConnectionState2["Closed"] = "Closed";
|
|
311
|
+
ConnectionState2["Connecting"] = "Connecting";
|
|
312
|
+
ConnectionState2["Open"] = "Open";
|
|
313
|
+
ConnectionState2["Unavailable"] = "Unavailable";
|
|
314
|
+
ConnectionState2["Untouched"] = "Untouched";
|
|
315
|
+
return ConnectionState2;
|
|
316
|
+
})(ConnectionState || {});
|
|
317
|
+
|
|
318
|
+
// src/MockedRoom.ts
|
|
319
|
+
var MockedRoom = class extends AbstractRoom {
|
|
320
|
+
constructor(room, options) {
|
|
321
|
+
const { events, initialPresence, initialStorage, presence } = options;
|
|
322
|
+
super(room);
|
|
323
|
+
this._crdtNotifier = new CrdtNotifier();
|
|
324
|
+
this._eventNotifier = new EventNotifier();
|
|
325
|
+
this._otherNotifier = new OtherNotifier();
|
|
326
|
+
this._state = {
|
|
327
|
+
authorization: {
|
|
328
|
+
token: null,
|
|
329
|
+
user: null
|
|
330
|
+
},
|
|
331
|
+
connection: {
|
|
332
|
+
count: 1,
|
|
333
|
+
id: null,
|
|
334
|
+
state: "Untouched" /* Untouched */
|
|
335
|
+
},
|
|
336
|
+
webSocket: null
|
|
337
|
+
};
|
|
338
|
+
this._stateNotifier = new StateNotifier();
|
|
339
|
+
this._subscriptions = {
|
|
340
|
+
observeCrdt: null
|
|
341
|
+
};
|
|
342
|
+
this.canRedo = () => {
|
|
343
|
+
return !!this._crdtManager.doc.canRedo();
|
|
344
|
+
};
|
|
345
|
+
this.canUndo = () => {
|
|
346
|
+
return !!this._crdtManager.doc.canUndo();
|
|
347
|
+
};
|
|
348
|
+
this.event = (event, callback) => {
|
|
349
|
+
return this._eventNotifier.subscribe(event, callback);
|
|
350
|
+
};
|
|
351
|
+
this.getMyPresence = () => {
|
|
352
|
+
return this._usersManager.myPresence;
|
|
353
|
+
};
|
|
354
|
+
this.getMyself = () => {
|
|
355
|
+
return this._usersManager.myself;
|
|
356
|
+
};
|
|
357
|
+
this.getOther = (connectionId) => {
|
|
358
|
+
return this._usersManager.getOther(connectionId);
|
|
359
|
+
};
|
|
360
|
+
this.getOthers = () => {
|
|
361
|
+
return this._usersManager.getOthers();
|
|
362
|
+
};
|
|
363
|
+
this.getStorage = (key) => {
|
|
364
|
+
const sharedType = this._crdtManager.get(key);
|
|
365
|
+
if (typeof sharedType === "undefined")
|
|
366
|
+
return null;
|
|
367
|
+
return sharedType.value;
|
|
368
|
+
};
|
|
369
|
+
this.other = (connectionId, callback) => {
|
|
370
|
+
return this._otherNotifier.subscribe(connectionId, callback);
|
|
371
|
+
};
|
|
372
|
+
this.redo = () => {
|
|
373
|
+
this._crdtManager.doc.redo();
|
|
374
|
+
};
|
|
375
|
+
this.storage = (key, fn) => {
|
|
376
|
+
return this._crdtNotifier.subscribe(key, fn);
|
|
377
|
+
};
|
|
378
|
+
this.storageRoot = (fn) => {
|
|
379
|
+
return this._crdtNotifier.subcribeRoot(fn);
|
|
380
|
+
};
|
|
381
|
+
this.subscribe = (name, callback) => {
|
|
382
|
+
return this._stateNotifier.subscribe(name, callback);
|
|
383
|
+
};
|
|
384
|
+
this.transact = (fn, origin) => {
|
|
385
|
+
const _origin = origin != null ? origin : this._state.connection.id;
|
|
386
|
+
const crdtManager = this._crdtManager;
|
|
387
|
+
if (typeof _origin !== "string")
|
|
388
|
+
return;
|
|
389
|
+
if (!crdtManager)
|
|
390
|
+
return;
|
|
391
|
+
crdtManager.doc.transact(() => {
|
|
392
|
+
const storage = crdtManager.doc.get();
|
|
393
|
+
fn(storage);
|
|
394
|
+
}, _origin);
|
|
395
|
+
};
|
|
396
|
+
this.undo = () => {
|
|
397
|
+
this._crdtManager.doc.undo();
|
|
398
|
+
};
|
|
399
|
+
this.updateMyPresence = (presence) => {
|
|
400
|
+
var _a;
|
|
401
|
+
this._usersManager.updateMyPresence(presence);
|
|
402
|
+
const myPresence = this._usersManager.myPresence;
|
|
403
|
+
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
404
|
+
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
405
|
+
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
406
|
+
};
|
|
407
|
+
this._events = events;
|
|
408
|
+
this._usersManager = new UsersManager({
|
|
409
|
+
initialPresence,
|
|
410
|
+
presence
|
|
411
|
+
});
|
|
412
|
+
this._crdtManager = new CrdtManager({
|
|
413
|
+
initialStorage
|
|
414
|
+
});
|
|
415
|
+
this._observeCrdt();
|
|
416
|
+
}
|
|
417
|
+
get storageLoaded() {
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
broadcast(event, data) {
|
|
421
|
+
if (!this._events)
|
|
422
|
+
return;
|
|
423
|
+
const resolver = this._events[event];
|
|
424
|
+
if (!resolver)
|
|
425
|
+
return;
|
|
426
|
+
const result = resolver(data);
|
|
427
|
+
Object.keys(result).forEach((type) => {
|
|
428
|
+
const _type = type.toString();
|
|
429
|
+
const data2 = result[_type];
|
|
430
|
+
if (!data2)
|
|
431
|
+
return;
|
|
432
|
+
this._eventNotifier.subject(_type).next(data2);
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
getConnection() {
|
|
436
|
+
return Object.freeze(
|
|
437
|
+
JSON.parse(JSON.stringify(this._state.connection))
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
getDoc() {
|
|
441
|
+
return this._crdtManager.doc;
|
|
442
|
+
}
|
|
443
|
+
_observeCrdt() {
|
|
444
|
+
var _a, _b;
|
|
445
|
+
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
446
|
+
if (!this._crdtManager)
|
|
447
|
+
return;
|
|
448
|
+
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
449
|
+
var _a2;
|
|
450
|
+
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
451
|
+
if (!this._crdtManager)
|
|
452
|
+
return;
|
|
453
|
+
if (origin === "$STORAGE_UPDATED")
|
|
454
|
+
return;
|
|
455
|
+
const sharedTypes = this._crdtManager.doc.get();
|
|
456
|
+
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
457
|
+
(acc, [prop, sharedType]) => {
|
|
458
|
+
if (!this._crdtManager)
|
|
459
|
+
return acc;
|
|
460
|
+
const serialized = sharedType.toJson();
|
|
461
|
+
this._crdtNotifier.subject(prop).next(serialized);
|
|
462
|
+
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
463
|
+
},
|
|
464
|
+
{}
|
|
465
|
+
);
|
|
466
|
+
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
467
|
+
});
|
|
468
|
+
this._subscriptions.observeCrdt = unsubscribe;
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
// src/StorageStore.ts
|
|
473
|
+
var StorageStore = class extends AbstractStorageStore {
|
|
474
|
+
addUpdate() {
|
|
475
|
+
return Promise.resolve();
|
|
476
|
+
}
|
|
477
|
+
applyUpdates() {
|
|
478
|
+
return Promise.resolve();
|
|
479
|
+
}
|
|
480
|
+
destroy() {
|
|
481
|
+
return Promise.resolve();
|
|
482
|
+
}
|
|
483
|
+
flatten() {
|
|
484
|
+
return Promise.resolve();
|
|
485
|
+
}
|
|
486
|
+
getShouldFlatten() {
|
|
487
|
+
return Promise.resolve(false);
|
|
488
|
+
}
|
|
489
|
+
getUpdates() {
|
|
490
|
+
return Promise.resolve([]);
|
|
491
|
+
}
|
|
492
|
+
initialize() {
|
|
493
|
+
return Promise.resolve();
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
|
|
353
497
|
// src/utils/debounce.ts
|
|
354
498
|
var debounce = (fn, options) => {
|
|
355
499
|
const { wait, immediate = false } = options != null ? options : {};
|
|
@@ -386,17 +530,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
386
530
|
const {
|
|
387
531
|
addons = [],
|
|
388
532
|
authEndpoint,
|
|
389
|
-
captureTimeout,
|
|
390
533
|
debug = false,
|
|
391
534
|
initialPresence,
|
|
392
535
|
initialStorage,
|
|
393
536
|
onAuthorizationFail,
|
|
394
537
|
presence,
|
|
395
|
-
trackedOrigins,
|
|
396
538
|
wsEndpoint
|
|
397
539
|
} = options;
|
|
398
540
|
super(room);
|
|
399
|
-
this._captureTimeout = null;
|
|
400
541
|
this._crdtNotifier = new CrdtNotifier();
|
|
401
542
|
this._eventNotifier = new EventNotifier();
|
|
402
543
|
this._intervals = {
|
|
@@ -423,7 +564,6 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
423
564
|
pong: null,
|
|
424
565
|
reconnect: null
|
|
425
566
|
};
|
|
426
|
-
this._trackedOrigins = null;
|
|
427
567
|
this._windowListeners = null;
|
|
428
568
|
this._wsListeners = null;
|
|
429
569
|
this.canRedo = () => {
|
|
@@ -449,9 +589,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
449
589
|
};
|
|
450
590
|
this.getStorage = (key) => {
|
|
451
591
|
const sharedType = this._crdtManager.get(key);
|
|
452
|
-
if (typeof sharedType === "undefined")
|
|
453
|
-
|
|
454
|
-
}
|
|
592
|
+
if (typeof sharedType === "undefined")
|
|
593
|
+
return null;
|
|
455
594
|
return sharedType;
|
|
456
595
|
};
|
|
457
596
|
this.other = (connectionId, callback) => {
|
|
@@ -474,7 +613,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
474
613
|
if (typeof _origin !== "string")
|
|
475
614
|
return;
|
|
476
615
|
this._crdtManager.doc.transact(() => {
|
|
477
|
-
|
|
616
|
+
const storage = this._crdtManager.doc.get();
|
|
617
|
+
fn(storage);
|
|
478
618
|
}, _origin);
|
|
479
619
|
};
|
|
480
620
|
this.undo = () => {
|
|
@@ -499,7 +639,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
499
639
|
return;
|
|
500
640
|
const updates = yield this._storageStore.getUpdates();
|
|
501
641
|
this._crdtManager.applyUpdate(updates, this);
|
|
502
|
-
const encodedState = this._crdtManager.doc.
|
|
642
|
+
const encodedState = this._crdtManager.doc.getEncodedState();
|
|
503
643
|
yield this._storageStore.flatten(encodedState);
|
|
504
644
|
}),
|
|
505
645
|
{ wait: ADD_TO_STORAGE_STATE_DEBOUNCE_MS }
|
|
@@ -530,12 +670,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
530
670
|
presence
|
|
531
671
|
});
|
|
532
672
|
this._crdtManager = new CrdtManager({
|
|
533
|
-
|
|
534
|
-
initialStorage,
|
|
535
|
-
trackedOrigins
|
|
673
|
+
initialStorage
|
|
536
674
|
});
|
|
537
|
-
|
|
538
|
-
|
|
675
|
+
}
|
|
676
|
+
get storageLoaded() {
|
|
677
|
+
return this._crdtManager.initialized;
|
|
539
678
|
}
|
|
540
679
|
get webSocket() {
|
|
541
680
|
return this._state.webSocket;
|
|
@@ -613,7 +752,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
613
752
|
);
|
|
614
753
|
}
|
|
615
754
|
getDoc() {
|
|
616
|
-
return this._crdtManager.doc
|
|
755
|
+
return this._crdtManager.doc;
|
|
617
756
|
}
|
|
618
757
|
_addToStorageStore(update) {
|
|
619
758
|
return __async(this, null, function* () {
|
|
@@ -623,16 +762,13 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
623
762
|
}
|
|
624
763
|
_applyStorageStore() {
|
|
625
764
|
return __async(this, null, function* () {
|
|
626
|
-
var _a;
|
|
627
765
|
const updates = yield this._storageStore.getUpdates();
|
|
628
|
-
const trackedOrigins = this._getTrackedOrigins();
|
|
629
766
|
this._crdtManager.initialize({
|
|
630
|
-
captureTimeout: (_a = this._captureTimeout) != null ? _a : void 0,
|
|
631
767
|
onInitialized: () => {
|
|
768
|
+
this._stateNotifier.subjects["storage-loaded"].next(true);
|
|
632
769
|
this._observeCrdt();
|
|
633
770
|
},
|
|
634
771
|
origin: ORIGIN_INITIALIZED,
|
|
635
|
-
trackedOrigins: trackedOrigins != null ? trackedOrigins : void 0,
|
|
636
772
|
update: updates
|
|
637
773
|
});
|
|
638
774
|
this._emitSharedTypes();
|
|
@@ -750,10 +886,10 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
750
886
|
this._wsListeners = null;
|
|
751
887
|
}
|
|
752
888
|
_emitSharedTypes() {
|
|
753
|
-
const sharedTypes = this._crdtManager.doc.
|
|
889
|
+
const sharedTypes = this._crdtManager.doc.get();
|
|
754
890
|
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
755
891
|
(acc, [prop, sharedType]) => {
|
|
756
|
-
const serialized = sharedType.
|
|
892
|
+
const serialized = sharedType.toJson();
|
|
757
893
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
758
894
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
759
895
|
},
|
|
@@ -790,7 +926,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
790
926
|
throw new Error("Room is unauthorized");
|
|
791
927
|
}
|
|
792
928
|
try {
|
|
793
|
-
return yield res.text().then((
|
|
929
|
+
return yield res.text().then((text) => text.trim());
|
|
794
930
|
} catch (err) {
|
|
795
931
|
throw new Error(
|
|
796
932
|
err instanceof Error ? err.message : "Room is unauthorized"
|
|
@@ -798,11 +934,6 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
798
934
|
}
|
|
799
935
|
});
|
|
800
936
|
}
|
|
801
|
-
_getTrackedOrigins() {
|
|
802
|
-
var _a;
|
|
803
|
-
const connectionId = this._state.connection.id;
|
|
804
|
-
return !connectionId ? this._trackedOrigins : [connectionId, ...(_a = this._trackedOrigins) != null ? _a : []];
|
|
805
|
-
}
|
|
806
937
|
_getWsEndpoint(room) {
|
|
807
938
|
switch (typeof this._endpoints.wsEndpoint) {
|
|
808
939
|
case "undefined":
|
|
@@ -891,7 +1022,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
891
1022
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
892
1023
|
this._stateNotifier.subjects["my-presence"].next(presence);
|
|
893
1024
|
this._stateNotifier.subjects["myself"].next(myself);
|
|
894
|
-
const update = this._state.connection.count > 1 ? (_b = this._crdtManager.doc.
|
|
1025
|
+
const update = this._state.connection.count > 1 ? (_b = this._crdtManager.doc.getEncodedState()) != null ? _b : null : null;
|
|
895
1026
|
this._sendMessage({
|
|
896
1027
|
type: "$INITIALIZE_SESSION",
|
|
897
1028
|
data: { presence, update }
|
|
@@ -899,27 +1030,26 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
899
1030
|
}
|
|
900
1031
|
_handleStorageReceivedMessage(message) {
|
|
901
1032
|
return __async(this, null, function* () {
|
|
902
|
-
var _a;
|
|
903
1033
|
const { connectionId } = message;
|
|
904
1034
|
if (!connectionId)
|
|
905
1035
|
return;
|
|
906
1036
|
if (!this._state.webSocket)
|
|
907
1037
|
throw new Error("Could not find WebSocket");
|
|
908
1038
|
const data = message.data;
|
|
909
|
-
const trackedOrigins = this._getTrackedOrigins();
|
|
910
1039
|
this._crdtManager.initialize({
|
|
911
|
-
|
|
912
|
-
|
|
1040
|
+
onInitialized: (update2) => __async(this, null, function* () {
|
|
1041
|
+
this._addToStorageStore(update2);
|
|
1042
|
+
this._emitSharedTypes();
|
|
913
1043
|
this._observeCrdt();
|
|
1044
|
+
this._stateNotifier.subjects["storage-loaded"].next(true);
|
|
914
1045
|
}),
|
|
915
1046
|
origin: ORIGIN_INITIALIZED,
|
|
916
|
-
trackedOrigins: trackedOrigins != null ? trackedOrigins : void 0,
|
|
917
1047
|
update: data.state
|
|
918
1048
|
});
|
|
919
|
-
const encodedState = this._crdtManager.doc.
|
|
1049
|
+
const encodedState = this._crdtManager.doc.getEncodedState();
|
|
920
1050
|
yield this._storageStore.addUpdate(encodedState);
|
|
921
1051
|
this._emitSharedTypes();
|
|
922
|
-
const update = this._crdtManager.doc.
|
|
1052
|
+
const update = this._crdtManager.doc.getEncodedState();
|
|
923
1053
|
this._sendMessage({
|
|
924
1054
|
type: "$UPDATE_STORAGE",
|
|
925
1055
|
data: { origin: ORIGIN_INITIALIZED, update }
|
|
@@ -935,13 +1065,16 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
935
1065
|
const data = message.data;
|
|
936
1066
|
if (!this._crdtManager)
|
|
937
1067
|
return;
|
|
938
|
-
this._crdtManager.doc.
|
|
939
|
-
|
|
1068
|
+
this._crdtManager.doc.applyEncodedState({
|
|
1069
|
+
origin: ORIGIN_STORAGE_UPDATED,
|
|
1070
|
+
update: data.state
|
|
1071
|
+
});
|
|
1072
|
+
const sharedTypes = this._crdtManager.doc.get();
|
|
940
1073
|
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
941
1074
|
(acc, [prop, sharedType]) => {
|
|
942
1075
|
if (!this._crdtManager)
|
|
943
1076
|
return acc;
|
|
944
|
-
const serialized = sharedType.
|
|
1077
|
+
const serialized = sharedType.toJson();
|
|
945
1078
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
946
1079
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
947
1080
|
},
|
|
@@ -991,27 +1124,25 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
991
1124
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
992
1125
|
if (!this._crdtManager)
|
|
993
1126
|
return;
|
|
994
|
-
const unsubscribe = this._crdtManager.doc.subscribe(
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
if (!this._state.webSocket)
|
|
1002
|
-
return;
|
|
1003
|
-
if (!this._state.connection.id)
|
|
1004
|
-
return;
|
|
1005
|
-
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
1006
|
-
return;
|
|
1007
|
-
if (origin === ORIGIN_STORAGE_UPDATED)
|
|
1008
|
-
return;
|
|
1009
|
-
this._sendMessage({
|
|
1010
|
-
type: "$UPDATE_STORAGE",
|
|
1011
|
-
data: { origin, update }
|
|
1012
|
-
});
|
|
1127
|
+
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
1128
|
+
var _a2;
|
|
1129
|
+
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
1130
|
+
this._addToStorageStore(event.update);
|
|
1131
|
+
this._emitSharedTypes();
|
|
1132
|
+
if (origin === ORIGIN_INITIALIZED || origin === ORIGIN_STORAGE_UPDATED) {
|
|
1133
|
+
return;
|
|
1013
1134
|
}
|
|
1014
|
-
|
|
1135
|
+
if (!this._state.webSocket)
|
|
1136
|
+
return;
|
|
1137
|
+
if (!this._state.connection.id)
|
|
1138
|
+
return;
|
|
1139
|
+
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
1140
|
+
return;
|
|
1141
|
+
this._sendMessage({
|
|
1142
|
+
type: "$UPDATE_STORAGE",
|
|
1143
|
+
data: { origin, update: event.update }
|
|
1144
|
+
});
|
|
1145
|
+
});
|
|
1015
1146
|
this._subscriptions.observeCrdt = unsubscribe;
|
|
1016
1147
|
}
|
|
1017
1148
|
_onAuthorizationFail(error) {
|
|
@@ -1209,7 +1340,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1209
1340
|
var PluvClient = class {
|
|
1210
1341
|
constructor(options) {
|
|
1211
1342
|
this._rooms = /* @__PURE__ */ new Map();
|
|
1212
|
-
this.createRoom = (room, options
|
|
1343
|
+
this.createRoom = (room, options) => {
|
|
1213
1344
|
const oldRoom = this.getRoom(room);
|
|
1214
1345
|
if (oldRoom)
|
|
1215
1346
|
return oldRoom;
|
|
@@ -1268,179 +1399,6 @@ var PluvClient = class {
|
|
|
1268
1399
|
var createClient = (options) => {
|
|
1269
1400
|
return new PluvClient(options);
|
|
1270
1401
|
};
|
|
1271
|
-
|
|
1272
|
-
// src/MockedRoom.ts
|
|
1273
|
-
var MockedRoom = class extends AbstractRoom {
|
|
1274
|
-
constructor(room, options) {
|
|
1275
|
-
const { events, initialPresence, initialStorage, presence } = options;
|
|
1276
|
-
super(room);
|
|
1277
|
-
this._crdtNotifier = new CrdtNotifier();
|
|
1278
|
-
this._eventNotifier = new EventNotifier();
|
|
1279
|
-
this._otherNotifier = new OtherNotifier();
|
|
1280
|
-
this._state = {
|
|
1281
|
-
authorization: {
|
|
1282
|
-
token: null,
|
|
1283
|
-
user: null
|
|
1284
|
-
},
|
|
1285
|
-
connection: {
|
|
1286
|
-
count: 1,
|
|
1287
|
-
id: null,
|
|
1288
|
-
state: "Untouched" /* Untouched */
|
|
1289
|
-
},
|
|
1290
|
-
webSocket: null
|
|
1291
|
-
};
|
|
1292
|
-
this._stateNotifier = new StateNotifier();
|
|
1293
|
-
this._subscriptions = {
|
|
1294
|
-
observeCrdt: null
|
|
1295
|
-
};
|
|
1296
|
-
this.canRedo = () => {
|
|
1297
|
-
return !!this._crdtManager.doc.canRedo();
|
|
1298
|
-
};
|
|
1299
|
-
this.canUndo = () => {
|
|
1300
|
-
return !!this._crdtManager.doc.canUndo();
|
|
1301
|
-
};
|
|
1302
|
-
this.event = (event, callback) => {
|
|
1303
|
-
return this._eventNotifier.subscribe(event, callback);
|
|
1304
|
-
};
|
|
1305
|
-
this.getMyPresence = () => {
|
|
1306
|
-
return this._usersManager.myPresence;
|
|
1307
|
-
};
|
|
1308
|
-
this.getMyself = () => {
|
|
1309
|
-
return this._usersManager.myself;
|
|
1310
|
-
};
|
|
1311
|
-
this.getOther = (connectionId) => {
|
|
1312
|
-
return this._usersManager.getOther(connectionId);
|
|
1313
|
-
};
|
|
1314
|
-
this.getOthers = () => {
|
|
1315
|
-
return this._usersManager.getOthers();
|
|
1316
|
-
};
|
|
1317
|
-
this.getStorage = (key) => {
|
|
1318
|
-
const sharedType = this._crdtManager.get(key);
|
|
1319
|
-
if (typeof sharedType === "undefined") {
|
|
1320
|
-
throw new Error(`Could not find storege: ${key.toString()}`);
|
|
1321
|
-
}
|
|
1322
|
-
return sharedType;
|
|
1323
|
-
};
|
|
1324
|
-
this.other = (connectionId, callback) => {
|
|
1325
|
-
return this._otherNotifier.subscribe(connectionId, callback);
|
|
1326
|
-
};
|
|
1327
|
-
this.redo = () => {
|
|
1328
|
-
this._crdtManager.doc.redo();
|
|
1329
|
-
};
|
|
1330
|
-
this.storage = (key, fn) => {
|
|
1331
|
-
return this._crdtNotifier.subscribe(key, fn);
|
|
1332
|
-
};
|
|
1333
|
-
this.storageRoot = (fn) => {
|
|
1334
|
-
return this._crdtNotifier.subcribeRoot(fn);
|
|
1335
|
-
};
|
|
1336
|
-
this.subscribe = (name, callback) => {
|
|
1337
|
-
return this._stateNotifier.subscribe(name, callback);
|
|
1338
|
-
};
|
|
1339
|
-
this.transact = (fn, origin) => {
|
|
1340
|
-
const _origin = origin != null ? origin : this._state.connection.id;
|
|
1341
|
-
const crdtManager = this._crdtManager;
|
|
1342
|
-
if (typeof _origin !== "string")
|
|
1343
|
-
return;
|
|
1344
|
-
if (!crdtManager)
|
|
1345
|
-
return;
|
|
1346
|
-
crdtManager.doc.transact(() => {
|
|
1347
|
-
fn(crdtManager.doc.storage);
|
|
1348
|
-
}, _origin);
|
|
1349
|
-
};
|
|
1350
|
-
this.undo = () => {
|
|
1351
|
-
this._crdtManager.doc.undo();
|
|
1352
|
-
};
|
|
1353
|
-
this.updateMyPresence = (presence) => {
|
|
1354
|
-
var _a;
|
|
1355
|
-
this._usersManager.updateMyPresence(presence);
|
|
1356
|
-
const myPresence = this._usersManager.myPresence;
|
|
1357
|
-
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
1358
|
-
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
1359
|
-
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
1360
|
-
};
|
|
1361
|
-
this._events = events;
|
|
1362
|
-
this._usersManager = new UsersManager({
|
|
1363
|
-
initialPresence,
|
|
1364
|
-
presence
|
|
1365
|
-
});
|
|
1366
|
-
this._crdtManager = new CrdtManager({
|
|
1367
|
-
initialStorage
|
|
1368
|
-
});
|
|
1369
|
-
this._observeCrdt();
|
|
1370
|
-
}
|
|
1371
|
-
broadcast(event, data) {
|
|
1372
|
-
if (!this._events)
|
|
1373
|
-
return;
|
|
1374
|
-
const resolver = this._events[event];
|
|
1375
|
-
if (!resolver)
|
|
1376
|
-
return;
|
|
1377
|
-
const result = resolver(data);
|
|
1378
|
-
Object.keys(result).forEach((type) => {
|
|
1379
|
-
const _type = type.toString();
|
|
1380
|
-
const data2 = result[_type];
|
|
1381
|
-
if (!data2)
|
|
1382
|
-
return;
|
|
1383
|
-
this._eventNotifier.subject(_type).next(data2);
|
|
1384
|
-
});
|
|
1385
|
-
}
|
|
1386
|
-
getConnection() {
|
|
1387
|
-
return Object.freeze(
|
|
1388
|
-
JSON.parse(JSON.stringify(this._state.connection))
|
|
1389
|
-
);
|
|
1390
|
-
}
|
|
1391
|
-
getDoc() {
|
|
1392
|
-
return this._crdtManager.doc.value;
|
|
1393
|
-
}
|
|
1394
|
-
_observeCrdt() {
|
|
1395
|
-
var _a, _b;
|
|
1396
|
-
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
1397
|
-
if (!this._crdtManager)
|
|
1398
|
-
return;
|
|
1399
|
-
const unsubscribe = this._crdtManager.doc.subscribe(
|
|
1400
|
-
(update, _origin) => {
|
|
1401
|
-
const origin = _origin != null ? _origin : null;
|
|
1402
|
-
if (!this._crdtManager)
|
|
1403
|
-
return;
|
|
1404
|
-
if (origin === "$STORAGE_UPDATED")
|
|
1405
|
-
return;
|
|
1406
|
-
const sharedTypes = this._crdtManager.doc.getSharedTypes();
|
|
1407
|
-
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
1408
|
-
(acc, [prop, sharedType]) => {
|
|
1409
|
-
if (!this._crdtManager)
|
|
1410
|
-
return acc;
|
|
1411
|
-
const serialized = sharedType.toJSON();
|
|
1412
|
-
this._crdtNotifier.subject(prop).next(serialized);
|
|
1413
|
-
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
1414
|
-
},
|
|
1415
|
-
{}
|
|
1416
|
-
);
|
|
1417
|
-
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
1418
|
-
}
|
|
1419
|
-
);
|
|
1420
|
-
this._subscriptions.observeCrdt = unsubscribe;
|
|
1421
|
-
}
|
|
1422
|
-
};
|
|
1423
|
-
|
|
1424
|
-
// src/y.ts
|
|
1425
|
-
var y_exports = {};
|
|
1426
|
-
__export(y_exports, {
|
|
1427
|
-
array: () => array,
|
|
1428
|
-
map: () => map,
|
|
1429
|
-
object: () => object,
|
|
1430
|
-
text: () => text,
|
|
1431
|
-
xmlElement: () => xmlElement,
|
|
1432
|
-
xmlFragment: () => xmlFragment,
|
|
1433
|
-
xmlText: () => xmlText
|
|
1434
|
-
});
|
|
1435
|
-
import {
|
|
1436
|
-
array,
|
|
1437
|
-
map,
|
|
1438
|
-
object,
|
|
1439
|
-
text,
|
|
1440
|
-
xmlElement,
|
|
1441
|
-
xmlFragment,
|
|
1442
|
-
xmlText
|
|
1443
|
-
} from "@pluv/crdt-yjs";
|
|
1444
1402
|
export {
|
|
1445
1403
|
AbstractRoom,
|
|
1446
1404
|
AbstractStorageStore,
|
|
@@ -1448,6 +1406,5 @@ export {
|
|
|
1448
1406
|
MockedRoom,
|
|
1449
1407
|
PluvClient,
|
|
1450
1408
|
PluvRoom,
|
|
1451
|
-
createClient
|
|
1452
|
-
y_exports as y
|
|
1409
|
+
createClient
|
|
1453
1410
|
};
|