@pluv/client 0.15.0 → 0.16.1
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.js
CHANGED
|
@@ -63,8 +63,7 @@ __export(src_exports, {
|
|
|
63
63
|
MockedRoom: () => MockedRoom,
|
|
64
64
|
PluvClient: () => PluvClient,
|
|
65
65
|
PluvRoom: () => PluvRoom,
|
|
66
|
-
createClient: () => createClient
|
|
67
|
-
y: () => y_exports
|
|
66
|
+
createClient: () => createClient
|
|
68
67
|
});
|
|
69
68
|
module.exports = __toCommonJS(src_exports);
|
|
70
69
|
|
|
@@ -83,25 +82,16 @@ var AbstractStorageStore = class {
|
|
|
83
82
|
};
|
|
84
83
|
|
|
85
84
|
// src/CrdtManager.ts
|
|
86
|
-
var
|
|
85
|
+
var import_crdt = require("@pluv/crdt");
|
|
87
86
|
var CrdtManager = class {
|
|
88
|
-
constructor(options
|
|
87
|
+
constructor(options) {
|
|
89
88
|
this.initialized = false;
|
|
90
89
|
const {
|
|
91
|
-
captureTimeout,
|
|
92
90
|
encodedState,
|
|
93
|
-
initialStorage
|
|
94
|
-
trackedOrigins = []
|
|
91
|
+
initialStorage = import_crdt.noop.doc({})
|
|
95
92
|
} = options;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.doc = _doc;
|
|
99
|
-
this.trackOrigins({ captureTimeout, trackedOrigins });
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
_doc == null ? void 0 : _doc.destroy();
|
|
103
|
-
this.doc = (0, import_crdt_yjs.doc)(initialStorage == null ? void 0 : initialStorage());
|
|
104
|
-
this.trackOrigins({ captureTimeout, trackedOrigins });
|
|
93
|
+
this._docFactory = initialStorage;
|
|
94
|
+
this.doc = encodedState ? initialStorage.getFresh().applyEncodedState({ update: encodedState }) : initialStorage.getFresh();
|
|
105
95
|
}
|
|
106
96
|
/**
|
|
107
97
|
* !HACK
|
|
@@ -123,41 +113,43 @@ var CrdtManager = class {
|
|
|
123
113
|
return this.doc.get(key);
|
|
124
114
|
}
|
|
125
115
|
initialize(params) {
|
|
126
|
-
const {
|
|
127
|
-
captureTimeout,
|
|
128
|
-
onInitialized,
|
|
129
|
-
origin,
|
|
130
|
-
trackedOrigins,
|
|
131
|
-
update
|
|
132
|
-
} = params;
|
|
116
|
+
const { onInitialized, origin, update } = params;
|
|
133
117
|
const updates = typeof update === "string" ? [update] : update;
|
|
134
|
-
this.trackOrigins({ captureTimeout, trackedOrigins });
|
|
135
118
|
if (!updates.length)
|
|
136
119
|
return this;
|
|
137
120
|
if (this.initialized) {
|
|
138
|
-
this._applyDocUpdates(this.doc, updates, origin);
|
|
121
|
+
this.doc = this._applyDocUpdates(this.doc, updates, origin).track();
|
|
139
122
|
return this;
|
|
140
123
|
}
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
124
|
+
const onEmpty = this._applyDocUpdates(
|
|
125
|
+
this._docFactory.getEmpty(),
|
|
126
|
+
updates,
|
|
127
|
+
origin
|
|
128
|
+
);
|
|
129
|
+
if (!onEmpty.isEmpty()) {
|
|
130
|
+
onEmpty.destroy();
|
|
131
|
+
this.doc = this._applyDocUpdates(
|
|
132
|
+
this._docFactory.getFresh(),
|
|
133
|
+
updates,
|
|
134
|
+
origin
|
|
135
|
+
).track();
|
|
136
|
+
this.initialized = true;
|
|
137
|
+
onInitialized == null ? void 0 : onInitialized(this.doc.getEncodedState());
|
|
138
|
+
return this;
|
|
147
139
|
}
|
|
148
|
-
|
|
149
|
-
this.
|
|
140
|
+
onEmpty.destroy();
|
|
141
|
+
this.doc = this._docFactory.getInitialized().track();
|
|
142
|
+
this.initialized = true;
|
|
143
|
+
onInitialized == null ? void 0 : onInitialized(this.doc.getEncodedState());
|
|
150
144
|
return this;
|
|
151
145
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
yjsDoc.transact(() => {
|
|
158
|
-
updates.reduce((acc, _update) => acc.applyUpdate(_update), yjsDoc);
|
|
146
|
+
_applyDocUpdates(doc, updates, origin) {
|
|
147
|
+
doc.transact(() => {
|
|
148
|
+
doc.batchApplyEncodedState(
|
|
149
|
+
updates.map((update) => ({ origin, update }))
|
|
150
|
+
);
|
|
159
151
|
}, origin);
|
|
160
|
-
return
|
|
152
|
+
return doc;
|
|
161
153
|
}
|
|
162
154
|
};
|
|
163
155
|
|
|
@@ -234,7 +226,8 @@ var StateNotifier = class {
|
|
|
234
226
|
connection: (0, import_wonka4.makeSubject)(),
|
|
235
227
|
"my-presence": (0, import_wonka4.makeSubject)(),
|
|
236
228
|
myself: (0, import_wonka4.makeSubject)(),
|
|
237
|
-
others: (0, import_wonka4.makeSubject)()
|
|
229
|
+
others: (0, import_wonka4.makeSubject)(),
|
|
230
|
+
"storage-loaded": (0, import_wonka4.makeSubject)()
|
|
238
231
|
};
|
|
239
232
|
}
|
|
240
233
|
subscribe(name, callback) {
|
|
@@ -246,41 +239,6 @@ var StateNotifier = class {
|
|
|
246
239
|
}
|
|
247
240
|
};
|
|
248
241
|
|
|
249
|
-
// src/StorageStore.ts
|
|
250
|
-
var StorageStore = class extends AbstractStorageStore {
|
|
251
|
-
addUpdate() {
|
|
252
|
-
return Promise.resolve();
|
|
253
|
-
}
|
|
254
|
-
applyUpdates() {
|
|
255
|
-
return Promise.resolve();
|
|
256
|
-
}
|
|
257
|
-
destroy() {
|
|
258
|
-
return Promise.resolve();
|
|
259
|
-
}
|
|
260
|
-
flatten() {
|
|
261
|
-
return Promise.resolve();
|
|
262
|
-
}
|
|
263
|
-
getShouldFlatten() {
|
|
264
|
-
return Promise.resolve(false);
|
|
265
|
-
}
|
|
266
|
-
getUpdates() {
|
|
267
|
-
return Promise.resolve([]);
|
|
268
|
-
}
|
|
269
|
-
initialize() {
|
|
270
|
-
return Promise.resolve();
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
|
|
274
|
-
// src/types.ts
|
|
275
|
-
var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
|
|
276
|
-
ConnectionState2["Closed"] = "Closed";
|
|
277
|
-
ConnectionState2["Connecting"] = "Connecting";
|
|
278
|
-
ConnectionState2["Open"] = "Open";
|
|
279
|
-
ConnectionState2["Unavailable"] = "Unavailable";
|
|
280
|
-
ConnectionState2["Untouched"] = "Untouched";
|
|
281
|
-
return ConnectionState2;
|
|
282
|
-
})(ConnectionState || {});
|
|
283
|
-
|
|
284
242
|
// src/UsersManager.ts
|
|
285
243
|
var UsersManager = class {
|
|
286
244
|
constructor(config) {
|
|
@@ -376,6 +334,195 @@ var UsersManager = class {
|
|
|
376
334
|
}
|
|
377
335
|
};
|
|
378
336
|
|
|
337
|
+
// src/types.ts
|
|
338
|
+
var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
|
|
339
|
+
ConnectionState2["Closed"] = "Closed";
|
|
340
|
+
ConnectionState2["Connecting"] = "Connecting";
|
|
341
|
+
ConnectionState2["Open"] = "Open";
|
|
342
|
+
ConnectionState2["Unavailable"] = "Unavailable";
|
|
343
|
+
ConnectionState2["Untouched"] = "Untouched";
|
|
344
|
+
return ConnectionState2;
|
|
345
|
+
})(ConnectionState || {});
|
|
346
|
+
|
|
347
|
+
// src/MockedRoom.ts
|
|
348
|
+
var MockedRoom = class extends AbstractRoom {
|
|
349
|
+
constructor(room, options) {
|
|
350
|
+
const { events, initialPresence, initialStorage, presence } = options;
|
|
351
|
+
super(room);
|
|
352
|
+
this._crdtNotifier = new CrdtNotifier();
|
|
353
|
+
this._eventNotifier = new EventNotifier();
|
|
354
|
+
this._otherNotifier = new OtherNotifier();
|
|
355
|
+
this._state = {
|
|
356
|
+
authorization: {
|
|
357
|
+
token: null,
|
|
358
|
+
user: null
|
|
359
|
+
},
|
|
360
|
+
connection: {
|
|
361
|
+
count: 1,
|
|
362
|
+
id: null,
|
|
363
|
+
state: "Untouched" /* Untouched */
|
|
364
|
+
},
|
|
365
|
+
webSocket: null
|
|
366
|
+
};
|
|
367
|
+
this._stateNotifier = new StateNotifier();
|
|
368
|
+
this._subscriptions = {
|
|
369
|
+
observeCrdt: null
|
|
370
|
+
};
|
|
371
|
+
this.canRedo = () => {
|
|
372
|
+
return !!this._crdtManager.doc.canRedo();
|
|
373
|
+
};
|
|
374
|
+
this.canUndo = () => {
|
|
375
|
+
return !!this._crdtManager.doc.canUndo();
|
|
376
|
+
};
|
|
377
|
+
this.event = (event, callback) => {
|
|
378
|
+
return this._eventNotifier.subscribe(event, callback);
|
|
379
|
+
};
|
|
380
|
+
this.getMyPresence = () => {
|
|
381
|
+
return this._usersManager.myPresence;
|
|
382
|
+
};
|
|
383
|
+
this.getMyself = () => {
|
|
384
|
+
return this._usersManager.myself;
|
|
385
|
+
};
|
|
386
|
+
this.getOther = (connectionId) => {
|
|
387
|
+
return this._usersManager.getOther(connectionId);
|
|
388
|
+
};
|
|
389
|
+
this.getOthers = () => {
|
|
390
|
+
return this._usersManager.getOthers();
|
|
391
|
+
};
|
|
392
|
+
this.getStorage = (key) => {
|
|
393
|
+
const sharedType = this._crdtManager.get(key);
|
|
394
|
+
if (typeof sharedType === "undefined")
|
|
395
|
+
return null;
|
|
396
|
+
return sharedType.value;
|
|
397
|
+
};
|
|
398
|
+
this.other = (connectionId, callback) => {
|
|
399
|
+
return this._otherNotifier.subscribe(connectionId, callback);
|
|
400
|
+
};
|
|
401
|
+
this.redo = () => {
|
|
402
|
+
this._crdtManager.doc.redo();
|
|
403
|
+
};
|
|
404
|
+
this.storage = (key, fn) => {
|
|
405
|
+
return this._crdtNotifier.subscribe(key, fn);
|
|
406
|
+
};
|
|
407
|
+
this.storageRoot = (fn) => {
|
|
408
|
+
return this._crdtNotifier.subcribeRoot(fn);
|
|
409
|
+
};
|
|
410
|
+
this.subscribe = (name, callback) => {
|
|
411
|
+
return this._stateNotifier.subscribe(name, callback);
|
|
412
|
+
};
|
|
413
|
+
this.transact = (fn, origin) => {
|
|
414
|
+
const _origin = origin != null ? origin : this._state.connection.id;
|
|
415
|
+
const crdtManager = this._crdtManager;
|
|
416
|
+
if (typeof _origin !== "string")
|
|
417
|
+
return;
|
|
418
|
+
if (!crdtManager)
|
|
419
|
+
return;
|
|
420
|
+
crdtManager.doc.transact(() => {
|
|
421
|
+
const storage = crdtManager.doc.get();
|
|
422
|
+
fn(storage);
|
|
423
|
+
}, _origin);
|
|
424
|
+
};
|
|
425
|
+
this.undo = () => {
|
|
426
|
+
this._crdtManager.doc.undo();
|
|
427
|
+
};
|
|
428
|
+
this.updateMyPresence = (presence) => {
|
|
429
|
+
var _a;
|
|
430
|
+
this._usersManager.updateMyPresence(presence);
|
|
431
|
+
const myPresence = this._usersManager.myPresence;
|
|
432
|
+
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
433
|
+
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
434
|
+
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
435
|
+
};
|
|
436
|
+
this._events = events;
|
|
437
|
+
this._usersManager = new UsersManager({
|
|
438
|
+
initialPresence,
|
|
439
|
+
presence
|
|
440
|
+
});
|
|
441
|
+
this._crdtManager = new CrdtManager({
|
|
442
|
+
initialStorage
|
|
443
|
+
});
|
|
444
|
+
this._observeCrdt();
|
|
445
|
+
}
|
|
446
|
+
get storageLoaded() {
|
|
447
|
+
return true;
|
|
448
|
+
}
|
|
449
|
+
broadcast(event, data) {
|
|
450
|
+
if (!this._events)
|
|
451
|
+
return;
|
|
452
|
+
const resolver = this._events[event];
|
|
453
|
+
if (!resolver)
|
|
454
|
+
return;
|
|
455
|
+
const result = resolver(data);
|
|
456
|
+
Object.keys(result).forEach((type) => {
|
|
457
|
+
const _type = type.toString();
|
|
458
|
+
const data2 = result[_type];
|
|
459
|
+
if (!data2)
|
|
460
|
+
return;
|
|
461
|
+
this._eventNotifier.subject(_type).next(data2);
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
getConnection() {
|
|
465
|
+
return Object.freeze(
|
|
466
|
+
JSON.parse(JSON.stringify(this._state.connection))
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
getDoc() {
|
|
470
|
+
return this._crdtManager.doc;
|
|
471
|
+
}
|
|
472
|
+
_observeCrdt() {
|
|
473
|
+
var _a, _b;
|
|
474
|
+
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
475
|
+
if (!this._crdtManager)
|
|
476
|
+
return;
|
|
477
|
+
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
478
|
+
var _a2;
|
|
479
|
+
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
480
|
+
if (!this._crdtManager)
|
|
481
|
+
return;
|
|
482
|
+
if (origin === "$STORAGE_UPDATED")
|
|
483
|
+
return;
|
|
484
|
+
const sharedTypes = this._crdtManager.doc.get();
|
|
485
|
+
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
486
|
+
(acc, [prop, sharedType]) => {
|
|
487
|
+
if (!this._crdtManager)
|
|
488
|
+
return acc;
|
|
489
|
+
const serialized = sharedType.toJson();
|
|
490
|
+
this._crdtNotifier.subject(prop).next(serialized);
|
|
491
|
+
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
492
|
+
},
|
|
493
|
+
{}
|
|
494
|
+
);
|
|
495
|
+
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
496
|
+
});
|
|
497
|
+
this._subscriptions.observeCrdt = unsubscribe;
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// src/StorageStore.ts
|
|
502
|
+
var StorageStore = class extends AbstractStorageStore {
|
|
503
|
+
addUpdate() {
|
|
504
|
+
return Promise.resolve();
|
|
505
|
+
}
|
|
506
|
+
applyUpdates() {
|
|
507
|
+
return Promise.resolve();
|
|
508
|
+
}
|
|
509
|
+
destroy() {
|
|
510
|
+
return Promise.resolve();
|
|
511
|
+
}
|
|
512
|
+
flatten() {
|
|
513
|
+
return Promise.resolve();
|
|
514
|
+
}
|
|
515
|
+
getShouldFlatten() {
|
|
516
|
+
return Promise.resolve(false);
|
|
517
|
+
}
|
|
518
|
+
getUpdates() {
|
|
519
|
+
return Promise.resolve([]);
|
|
520
|
+
}
|
|
521
|
+
initialize() {
|
|
522
|
+
return Promise.resolve();
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
|
|
379
526
|
// src/utils/debounce.ts
|
|
380
527
|
var debounce = (fn, options) => {
|
|
381
528
|
const { wait, immediate = false } = options != null ? options : {};
|
|
@@ -412,17 +559,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
412
559
|
const {
|
|
413
560
|
addons = [],
|
|
414
561
|
authEndpoint,
|
|
415
|
-
captureTimeout,
|
|
416
562
|
debug = false,
|
|
417
563
|
initialPresence,
|
|
418
564
|
initialStorage,
|
|
419
565
|
onAuthorizationFail,
|
|
420
566
|
presence,
|
|
421
|
-
trackedOrigins,
|
|
422
567
|
wsEndpoint
|
|
423
568
|
} = options;
|
|
424
569
|
super(room);
|
|
425
|
-
this._captureTimeout = null;
|
|
426
570
|
this._crdtNotifier = new CrdtNotifier();
|
|
427
571
|
this._eventNotifier = new EventNotifier();
|
|
428
572
|
this._intervals = {
|
|
@@ -449,7 +593,6 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
449
593
|
pong: null,
|
|
450
594
|
reconnect: null
|
|
451
595
|
};
|
|
452
|
-
this._trackedOrigins = null;
|
|
453
596
|
this._windowListeners = null;
|
|
454
597
|
this._wsListeners = null;
|
|
455
598
|
this.canRedo = () => {
|
|
@@ -475,9 +618,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
475
618
|
};
|
|
476
619
|
this.getStorage = (key) => {
|
|
477
620
|
const sharedType = this._crdtManager.get(key);
|
|
478
|
-
if (typeof sharedType === "undefined")
|
|
479
|
-
|
|
480
|
-
}
|
|
621
|
+
if (typeof sharedType === "undefined")
|
|
622
|
+
return null;
|
|
481
623
|
return sharedType;
|
|
482
624
|
};
|
|
483
625
|
this.other = (connectionId, callback) => {
|
|
@@ -500,7 +642,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
500
642
|
if (typeof _origin !== "string")
|
|
501
643
|
return;
|
|
502
644
|
this._crdtManager.doc.transact(() => {
|
|
503
|
-
|
|
645
|
+
const storage = this._crdtManager.doc.get();
|
|
646
|
+
fn(storage);
|
|
504
647
|
}, _origin);
|
|
505
648
|
};
|
|
506
649
|
this.undo = () => {
|
|
@@ -525,7 +668,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
525
668
|
return;
|
|
526
669
|
const updates = yield this._storageStore.getUpdates();
|
|
527
670
|
this._crdtManager.applyUpdate(updates, this);
|
|
528
|
-
const encodedState = this._crdtManager.doc.
|
|
671
|
+
const encodedState = this._crdtManager.doc.getEncodedState();
|
|
529
672
|
yield this._storageStore.flatten(encodedState);
|
|
530
673
|
}),
|
|
531
674
|
{ wait: ADD_TO_STORAGE_STATE_DEBOUNCE_MS }
|
|
@@ -556,12 +699,11 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
556
699
|
presence
|
|
557
700
|
});
|
|
558
701
|
this._crdtManager = new CrdtManager({
|
|
559
|
-
|
|
560
|
-
initialStorage,
|
|
561
|
-
trackedOrigins
|
|
702
|
+
initialStorage
|
|
562
703
|
});
|
|
563
|
-
|
|
564
|
-
|
|
704
|
+
}
|
|
705
|
+
get storageLoaded() {
|
|
706
|
+
return this._crdtManager.initialized;
|
|
565
707
|
}
|
|
566
708
|
get webSocket() {
|
|
567
709
|
return this._state.webSocket;
|
|
@@ -639,7 +781,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
639
781
|
);
|
|
640
782
|
}
|
|
641
783
|
getDoc() {
|
|
642
|
-
return this._crdtManager.doc
|
|
784
|
+
return this._crdtManager.doc;
|
|
643
785
|
}
|
|
644
786
|
_addToStorageStore(update) {
|
|
645
787
|
return __async(this, null, function* () {
|
|
@@ -649,16 +791,13 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
649
791
|
}
|
|
650
792
|
_applyStorageStore() {
|
|
651
793
|
return __async(this, null, function* () {
|
|
652
|
-
var _a;
|
|
653
794
|
const updates = yield this._storageStore.getUpdates();
|
|
654
|
-
const trackedOrigins = this._getTrackedOrigins();
|
|
655
795
|
this._crdtManager.initialize({
|
|
656
|
-
captureTimeout: (_a = this._captureTimeout) != null ? _a : void 0,
|
|
657
796
|
onInitialized: () => {
|
|
797
|
+
this._stateNotifier.subjects["storage-loaded"].next(true);
|
|
658
798
|
this._observeCrdt();
|
|
659
799
|
},
|
|
660
800
|
origin: ORIGIN_INITIALIZED,
|
|
661
|
-
trackedOrigins: trackedOrigins != null ? trackedOrigins : void 0,
|
|
662
801
|
update: updates
|
|
663
802
|
});
|
|
664
803
|
this._emitSharedTypes();
|
|
@@ -776,10 +915,10 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
776
915
|
this._wsListeners = null;
|
|
777
916
|
}
|
|
778
917
|
_emitSharedTypes() {
|
|
779
|
-
const sharedTypes = this._crdtManager.doc.
|
|
918
|
+
const sharedTypes = this._crdtManager.doc.get();
|
|
780
919
|
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
781
920
|
(acc, [prop, sharedType]) => {
|
|
782
|
-
const serialized = sharedType.
|
|
921
|
+
const serialized = sharedType.toJson();
|
|
783
922
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
784
923
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
785
924
|
},
|
|
@@ -816,7 +955,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
816
955
|
throw new Error("Room is unauthorized");
|
|
817
956
|
}
|
|
818
957
|
try {
|
|
819
|
-
return yield res.text().then((
|
|
958
|
+
return yield res.text().then((text) => text.trim());
|
|
820
959
|
} catch (err) {
|
|
821
960
|
throw new Error(
|
|
822
961
|
err instanceof Error ? err.message : "Room is unauthorized"
|
|
@@ -824,11 +963,6 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
824
963
|
}
|
|
825
964
|
});
|
|
826
965
|
}
|
|
827
|
-
_getTrackedOrigins() {
|
|
828
|
-
var _a;
|
|
829
|
-
const connectionId = this._state.connection.id;
|
|
830
|
-
return !connectionId ? this._trackedOrigins : [connectionId, ...(_a = this._trackedOrigins) != null ? _a : []];
|
|
831
|
-
}
|
|
832
966
|
_getWsEndpoint(room) {
|
|
833
967
|
switch (typeof this._endpoints.wsEndpoint) {
|
|
834
968
|
case "undefined":
|
|
@@ -917,7 +1051,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
917
1051
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
918
1052
|
this._stateNotifier.subjects["my-presence"].next(presence);
|
|
919
1053
|
this._stateNotifier.subjects["myself"].next(myself);
|
|
920
|
-
const update = this._state.connection.count > 1 ? (_b = this._crdtManager.doc.
|
|
1054
|
+
const update = this._state.connection.count > 1 ? (_b = this._crdtManager.doc.getEncodedState()) != null ? _b : null : null;
|
|
921
1055
|
this._sendMessage({
|
|
922
1056
|
type: "$INITIALIZE_SESSION",
|
|
923
1057
|
data: { presence, update }
|
|
@@ -925,27 +1059,26 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
925
1059
|
}
|
|
926
1060
|
_handleStorageReceivedMessage(message) {
|
|
927
1061
|
return __async(this, null, function* () {
|
|
928
|
-
var _a;
|
|
929
1062
|
const { connectionId } = message;
|
|
930
1063
|
if (!connectionId)
|
|
931
1064
|
return;
|
|
932
1065
|
if (!this._state.webSocket)
|
|
933
1066
|
throw new Error("Could not find WebSocket");
|
|
934
1067
|
const data = message.data;
|
|
935
|
-
const trackedOrigins = this._getTrackedOrigins();
|
|
936
1068
|
this._crdtManager.initialize({
|
|
937
|
-
|
|
938
|
-
|
|
1069
|
+
onInitialized: (update2) => __async(this, null, function* () {
|
|
1070
|
+
this._addToStorageStore(update2);
|
|
1071
|
+
this._emitSharedTypes();
|
|
939
1072
|
this._observeCrdt();
|
|
1073
|
+
this._stateNotifier.subjects["storage-loaded"].next(true);
|
|
940
1074
|
}),
|
|
941
1075
|
origin: ORIGIN_INITIALIZED,
|
|
942
|
-
trackedOrigins: trackedOrigins != null ? trackedOrigins : void 0,
|
|
943
1076
|
update: data.state
|
|
944
1077
|
});
|
|
945
|
-
const encodedState = this._crdtManager.doc.
|
|
1078
|
+
const encodedState = this._crdtManager.doc.getEncodedState();
|
|
946
1079
|
yield this._storageStore.addUpdate(encodedState);
|
|
947
1080
|
this._emitSharedTypes();
|
|
948
|
-
const update = this._crdtManager.doc.
|
|
1081
|
+
const update = this._crdtManager.doc.getEncodedState();
|
|
949
1082
|
this._sendMessage({
|
|
950
1083
|
type: "$UPDATE_STORAGE",
|
|
951
1084
|
data: { origin: ORIGIN_INITIALIZED, update }
|
|
@@ -961,13 +1094,16 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
961
1094
|
const data = message.data;
|
|
962
1095
|
if (!this._crdtManager)
|
|
963
1096
|
return;
|
|
964
|
-
this._crdtManager.doc.
|
|
965
|
-
|
|
1097
|
+
this._crdtManager.doc.applyEncodedState({
|
|
1098
|
+
origin: ORIGIN_STORAGE_UPDATED,
|
|
1099
|
+
update: data.state
|
|
1100
|
+
});
|
|
1101
|
+
const sharedTypes = this._crdtManager.doc.get();
|
|
966
1102
|
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
967
1103
|
(acc, [prop, sharedType]) => {
|
|
968
1104
|
if (!this._crdtManager)
|
|
969
1105
|
return acc;
|
|
970
|
-
const serialized = sharedType.
|
|
1106
|
+
const serialized = sharedType.toJson();
|
|
971
1107
|
this._crdtNotifier.subject(prop).next(serialized);
|
|
972
1108
|
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
973
1109
|
},
|
|
@@ -1017,27 +1153,25 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1017
1153
|
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
1018
1154
|
if (!this._crdtManager)
|
|
1019
1155
|
return;
|
|
1020
|
-
const unsubscribe = this._crdtManager.doc.subscribe(
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
if (!this._state.webSocket)
|
|
1028
|
-
return;
|
|
1029
|
-
if (!this._state.connection.id)
|
|
1030
|
-
return;
|
|
1031
|
-
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
1032
|
-
return;
|
|
1033
|
-
if (origin === ORIGIN_STORAGE_UPDATED)
|
|
1034
|
-
return;
|
|
1035
|
-
this._sendMessage({
|
|
1036
|
-
type: "$UPDATE_STORAGE",
|
|
1037
|
-
data: { origin, update }
|
|
1038
|
-
});
|
|
1156
|
+
const unsubscribe = this._crdtManager.doc.subscribe((event) => {
|
|
1157
|
+
var _a2;
|
|
1158
|
+
const origin = (_a2 = event.origin) != null ? _a2 : null;
|
|
1159
|
+
this._addToStorageStore(event.update);
|
|
1160
|
+
this._emitSharedTypes();
|
|
1161
|
+
if (origin === ORIGIN_INITIALIZED || origin === ORIGIN_STORAGE_UPDATED) {
|
|
1162
|
+
return;
|
|
1039
1163
|
}
|
|
1040
|
-
|
|
1164
|
+
if (!this._state.webSocket)
|
|
1165
|
+
return;
|
|
1166
|
+
if (!this._state.connection.id)
|
|
1167
|
+
return;
|
|
1168
|
+
if (this._state.webSocket.readyState !== WebSocket.OPEN)
|
|
1169
|
+
return;
|
|
1170
|
+
this._sendMessage({
|
|
1171
|
+
type: "$UPDATE_STORAGE",
|
|
1172
|
+
data: { origin, update: event.update }
|
|
1173
|
+
});
|
|
1174
|
+
});
|
|
1041
1175
|
this._subscriptions.observeCrdt = unsubscribe;
|
|
1042
1176
|
}
|
|
1043
1177
|
_onAuthorizationFail(error) {
|
|
@@ -1235,7 +1369,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1235
1369
|
var PluvClient = class {
|
|
1236
1370
|
constructor(options) {
|
|
1237
1371
|
this._rooms = /* @__PURE__ */ new Map();
|
|
1238
|
-
this.createRoom = (room, options
|
|
1372
|
+
this.createRoom = (room, options) => {
|
|
1239
1373
|
const oldRoom = this.getRoom(room);
|
|
1240
1374
|
if (oldRoom)
|
|
1241
1375
|
return oldRoom;
|
|
@@ -1294,171 +1428,6 @@ var PluvClient = class {
|
|
|
1294
1428
|
var createClient = (options) => {
|
|
1295
1429
|
return new PluvClient(options);
|
|
1296
1430
|
};
|
|
1297
|
-
|
|
1298
|
-
// src/MockedRoom.ts
|
|
1299
|
-
var MockedRoom = class extends AbstractRoom {
|
|
1300
|
-
constructor(room, options) {
|
|
1301
|
-
const { events, initialPresence, initialStorage, presence } = options;
|
|
1302
|
-
super(room);
|
|
1303
|
-
this._crdtNotifier = new CrdtNotifier();
|
|
1304
|
-
this._eventNotifier = new EventNotifier();
|
|
1305
|
-
this._otherNotifier = new OtherNotifier();
|
|
1306
|
-
this._state = {
|
|
1307
|
-
authorization: {
|
|
1308
|
-
token: null,
|
|
1309
|
-
user: null
|
|
1310
|
-
},
|
|
1311
|
-
connection: {
|
|
1312
|
-
count: 1,
|
|
1313
|
-
id: null,
|
|
1314
|
-
state: "Untouched" /* Untouched */
|
|
1315
|
-
},
|
|
1316
|
-
webSocket: null
|
|
1317
|
-
};
|
|
1318
|
-
this._stateNotifier = new StateNotifier();
|
|
1319
|
-
this._subscriptions = {
|
|
1320
|
-
observeCrdt: null
|
|
1321
|
-
};
|
|
1322
|
-
this.canRedo = () => {
|
|
1323
|
-
return !!this._crdtManager.doc.canRedo();
|
|
1324
|
-
};
|
|
1325
|
-
this.canUndo = () => {
|
|
1326
|
-
return !!this._crdtManager.doc.canUndo();
|
|
1327
|
-
};
|
|
1328
|
-
this.event = (event, callback) => {
|
|
1329
|
-
return this._eventNotifier.subscribe(event, callback);
|
|
1330
|
-
};
|
|
1331
|
-
this.getMyPresence = () => {
|
|
1332
|
-
return this._usersManager.myPresence;
|
|
1333
|
-
};
|
|
1334
|
-
this.getMyself = () => {
|
|
1335
|
-
return this._usersManager.myself;
|
|
1336
|
-
};
|
|
1337
|
-
this.getOther = (connectionId) => {
|
|
1338
|
-
return this._usersManager.getOther(connectionId);
|
|
1339
|
-
};
|
|
1340
|
-
this.getOthers = () => {
|
|
1341
|
-
return this._usersManager.getOthers();
|
|
1342
|
-
};
|
|
1343
|
-
this.getStorage = (key) => {
|
|
1344
|
-
const sharedType = this._crdtManager.get(key);
|
|
1345
|
-
if (typeof sharedType === "undefined") {
|
|
1346
|
-
throw new Error(`Could not find storege: ${key.toString()}`);
|
|
1347
|
-
}
|
|
1348
|
-
return sharedType;
|
|
1349
|
-
};
|
|
1350
|
-
this.other = (connectionId, callback) => {
|
|
1351
|
-
return this._otherNotifier.subscribe(connectionId, callback);
|
|
1352
|
-
};
|
|
1353
|
-
this.redo = () => {
|
|
1354
|
-
this._crdtManager.doc.redo();
|
|
1355
|
-
};
|
|
1356
|
-
this.storage = (key, fn) => {
|
|
1357
|
-
return this._crdtNotifier.subscribe(key, fn);
|
|
1358
|
-
};
|
|
1359
|
-
this.storageRoot = (fn) => {
|
|
1360
|
-
return this._crdtNotifier.subcribeRoot(fn);
|
|
1361
|
-
};
|
|
1362
|
-
this.subscribe = (name, callback) => {
|
|
1363
|
-
return this._stateNotifier.subscribe(name, callback);
|
|
1364
|
-
};
|
|
1365
|
-
this.transact = (fn, origin) => {
|
|
1366
|
-
const _origin = origin != null ? origin : this._state.connection.id;
|
|
1367
|
-
const crdtManager = this._crdtManager;
|
|
1368
|
-
if (typeof _origin !== "string")
|
|
1369
|
-
return;
|
|
1370
|
-
if (!crdtManager)
|
|
1371
|
-
return;
|
|
1372
|
-
crdtManager.doc.transact(() => {
|
|
1373
|
-
fn(crdtManager.doc.storage);
|
|
1374
|
-
}, _origin);
|
|
1375
|
-
};
|
|
1376
|
-
this.undo = () => {
|
|
1377
|
-
this._crdtManager.doc.undo();
|
|
1378
|
-
};
|
|
1379
|
-
this.updateMyPresence = (presence) => {
|
|
1380
|
-
var _a;
|
|
1381
|
-
this._usersManager.updateMyPresence(presence);
|
|
1382
|
-
const myPresence = this._usersManager.myPresence;
|
|
1383
|
-
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
1384
|
-
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
1385
|
-
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
1386
|
-
};
|
|
1387
|
-
this._events = events;
|
|
1388
|
-
this._usersManager = new UsersManager({
|
|
1389
|
-
initialPresence,
|
|
1390
|
-
presence
|
|
1391
|
-
});
|
|
1392
|
-
this._crdtManager = new CrdtManager({
|
|
1393
|
-
initialStorage
|
|
1394
|
-
});
|
|
1395
|
-
this._observeCrdt();
|
|
1396
|
-
}
|
|
1397
|
-
broadcast(event, data) {
|
|
1398
|
-
if (!this._events)
|
|
1399
|
-
return;
|
|
1400
|
-
const resolver = this._events[event];
|
|
1401
|
-
if (!resolver)
|
|
1402
|
-
return;
|
|
1403
|
-
const result = resolver(data);
|
|
1404
|
-
Object.keys(result).forEach((type) => {
|
|
1405
|
-
const _type = type.toString();
|
|
1406
|
-
const data2 = result[_type];
|
|
1407
|
-
if (!data2)
|
|
1408
|
-
return;
|
|
1409
|
-
this._eventNotifier.subject(_type).next(data2);
|
|
1410
|
-
});
|
|
1411
|
-
}
|
|
1412
|
-
getConnection() {
|
|
1413
|
-
return Object.freeze(
|
|
1414
|
-
JSON.parse(JSON.stringify(this._state.connection))
|
|
1415
|
-
);
|
|
1416
|
-
}
|
|
1417
|
-
getDoc() {
|
|
1418
|
-
return this._crdtManager.doc.value;
|
|
1419
|
-
}
|
|
1420
|
-
_observeCrdt() {
|
|
1421
|
-
var _a, _b;
|
|
1422
|
-
(_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
|
|
1423
|
-
if (!this._crdtManager)
|
|
1424
|
-
return;
|
|
1425
|
-
const unsubscribe = this._crdtManager.doc.subscribe(
|
|
1426
|
-
(update, _origin) => {
|
|
1427
|
-
const origin = _origin != null ? _origin : null;
|
|
1428
|
-
if (!this._crdtManager)
|
|
1429
|
-
return;
|
|
1430
|
-
if (origin === "$STORAGE_UPDATED")
|
|
1431
|
-
return;
|
|
1432
|
-
const sharedTypes = this._crdtManager.doc.getSharedTypes();
|
|
1433
|
-
const storageRoot = Object.entries(sharedTypes).reduce(
|
|
1434
|
-
(acc, [prop, sharedType]) => {
|
|
1435
|
-
if (!this._crdtManager)
|
|
1436
|
-
return acc;
|
|
1437
|
-
const serialized = sharedType.toJSON();
|
|
1438
|
-
this._crdtNotifier.subject(prop).next(serialized);
|
|
1439
|
-
return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
|
|
1440
|
-
},
|
|
1441
|
-
{}
|
|
1442
|
-
);
|
|
1443
|
-
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
1444
|
-
}
|
|
1445
|
-
);
|
|
1446
|
-
this._subscriptions.observeCrdt = unsubscribe;
|
|
1447
|
-
}
|
|
1448
|
-
};
|
|
1449
|
-
|
|
1450
|
-
// src/y.ts
|
|
1451
|
-
var y_exports = {};
|
|
1452
|
-
__export(y_exports, {
|
|
1453
|
-
array: () => import_crdt_yjs2.array,
|
|
1454
|
-
map: () => import_crdt_yjs2.map,
|
|
1455
|
-
object: () => import_crdt_yjs2.object,
|
|
1456
|
-
text: () => import_crdt_yjs2.text,
|
|
1457
|
-
xmlElement: () => import_crdt_yjs2.xmlElement,
|
|
1458
|
-
xmlFragment: () => import_crdt_yjs2.xmlFragment,
|
|
1459
|
-
xmlText: () => import_crdt_yjs2.xmlText
|
|
1460
|
-
});
|
|
1461
|
-
var import_crdt_yjs2 = require("@pluv/crdt-yjs");
|
|
1462
1431
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1463
1432
|
0 && (module.exports = {
|
|
1464
1433
|
AbstractRoom,
|
|
@@ -1467,6 +1436,5 @@ var import_crdt_yjs2 = require("@pluv/crdt-yjs");
|
|
|
1467
1436
|
MockedRoom,
|
|
1468
1437
|
PluvClient,
|
|
1469
1438
|
PluvRoom,
|
|
1470
|
-
createClient
|
|
1471
|
-
y
|
|
1439
|
+
createClient
|
|
1472
1440
|
});
|