@pluv/client 0.25.3 → 0.26.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/dist/index.js CHANGED
@@ -228,6 +228,31 @@ var StateNotifier = class {
228
228
  }
229
229
  };
230
230
 
231
+ // src/StorageStore.ts
232
+ var StorageStore = class extends AbstractStorageStore {
233
+ addUpdate() {
234
+ return Promise.resolve();
235
+ }
236
+ applyUpdates() {
237
+ return Promise.resolve();
238
+ }
239
+ destroy() {
240
+ return Promise.resolve();
241
+ }
242
+ flatten() {
243
+ return Promise.resolve();
244
+ }
245
+ getShouldFlatten() {
246
+ return Promise.resolve(false);
247
+ }
248
+ getUpdates() {
249
+ return Promise.resolve([]);
250
+ }
251
+ initialize() {
252
+ return Promise.resolve();
253
+ }
254
+ };
255
+
231
256
  // src/UsersManager.ts
232
257
  var UsersManager = class {
233
258
  constructor(config) {
@@ -331,179 +356,6 @@ var ConnectionState = /* @__PURE__ */ ((ConnectionState2) => {
331
356
  return ConnectionState2;
332
357
  })(ConnectionState || {});
333
358
 
334
- // src/MockedRoom.ts
335
- var MockedRoom = class extends AbstractRoom {
336
- constructor(room, options) {
337
- const { events, initialPresence, initialStorage, presence } = options;
338
- super(room);
339
- this._crdtNotifier = new CrdtNotifier();
340
- this._eventNotifier = new EventNotifier();
341
- this._otherNotifier = new OtherNotifier();
342
- this._state = {
343
- authorization: {
344
- token: null,
345
- user: null
346
- },
347
- connection: {
348
- count: 1,
349
- id: null,
350
- state: "Untouched" /* Untouched */
351
- },
352
- webSocket: null
353
- };
354
- this._stateNotifier = new StateNotifier();
355
- this._subscriptions = {
356
- observeCrdt: null
357
- };
358
- this.canRedo = () => {
359
- return !!this._crdtManager.doc.canRedo();
360
- };
361
- this.canUndo = () => {
362
- return !!this._crdtManager.doc.canUndo();
363
- };
364
- this.event = (event, callback) => {
365
- return this._eventNotifier.subscribe(event, callback);
366
- };
367
- this.getConnection = () => {
368
- return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
369
- };
370
- this.getMyPresence = () => {
371
- return this._usersManager.myPresence;
372
- };
373
- this.getMyself = () => {
374
- return this._usersManager.myself;
375
- };
376
- this.getOther = (connectionId) => {
377
- return this._usersManager.getOther(connectionId);
378
- };
379
- this.getOthers = () => {
380
- return this._usersManager.getOthers();
381
- };
382
- this.getStorage = (type) => {
383
- const sharedType = this._crdtManager.get(type);
384
- if (typeof sharedType === "undefined") return null;
385
- return sharedType;
386
- };
387
- this.other = (connectionId, callback) => {
388
- return this._otherNotifier.subscribe(connectionId, callback);
389
- };
390
- this.redo = () => {
391
- this._crdtManager.doc.redo();
392
- };
393
- this.storage = (key, fn) => {
394
- return this._crdtNotifier.subscribe(key, fn);
395
- };
396
- this.storageRoot = (fn) => {
397
- return this._crdtNotifier.subcribeRoot(fn);
398
- };
399
- this.subscribe = (name, callback) => {
400
- return this._stateNotifier.subscribe(name, callback);
401
- };
402
- this.transact = (fn, origin) => {
403
- const _origin = origin != null ? origin : this._state.connection.id;
404
- const crdtManager = this._crdtManager;
405
- if (typeof _origin !== "string") return;
406
- if (!crdtManager) return;
407
- crdtManager.doc.transact(() => {
408
- const storage = crdtManager.doc.get();
409
- fn(storage);
410
- }, _origin);
411
- };
412
- this.undo = () => {
413
- this._crdtManager.doc.undo();
414
- };
415
- this.updateMyPresence = (presence) => {
416
- var _a;
417
- const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
418
- this._usersManager.updateMyPresence(newPresence);
419
- const myPresence = this._usersManager.myPresence;
420
- const myself = (_a = this._usersManager.myself) != null ? _a : null;
421
- this._stateNotifier.subjects["my-presence"].next(myPresence);
422
- !!myself && this._stateNotifier.subjects["myself"].next(myself);
423
- };
424
- this._events = events;
425
- this._usersManager = new UsersManager({
426
- initialPresence,
427
- presence
428
- });
429
- this._crdtManager = new CrdtManager({
430
- initialStorage
431
- });
432
- this._observeCrdt();
433
- }
434
- get storageLoaded() {
435
- return true;
436
- }
437
- broadcast(event, data) {
438
- if (!this._events) return;
439
- const resolver = this._events[event];
440
- if (!resolver) return;
441
- const result = resolver(data);
442
- Object.keys(result).forEach((type) => {
443
- const _type = type.toString();
444
- const data2 = result[_type];
445
- if (!data2) return;
446
- this._eventNotifier.subject(_type).next(data2);
447
- });
448
- }
449
- getDoc() {
450
- return this._crdtManager.doc;
451
- }
452
- getStorageJson(type) {
453
- if (this._state.connection.id === null) return null;
454
- if (typeof type === "undefined") return this._crdtManager.doc.toJson();
455
- return this._crdtManager.doc.toJson(type);
456
- }
457
- _observeCrdt() {
458
- var _a, _b;
459
- (_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
460
- if (!this._crdtManager) return;
461
- const unsubscribe = this._crdtManager.doc.subscribe((event) => {
462
- var _a2;
463
- const origin = (_a2 = event.origin) != null ? _a2 : null;
464
- if (!this._crdtManager) return;
465
- if (origin === "$STORAGE_UPDATED") return;
466
- const sharedTypes = this._crdtManager.doc.get();
467
- const storageRoot = Object.keys(sharedTypes).reduce(
468
- (acc, prop) => {
469
- if (!this._crdtManager) return acc;
470
- const serialized = this._crdtManager.doc.toJson(prop);
471
- this._crdtNotifier.subject(prop).next(serialized);
472
- return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
473
- },
474
- {}
475
- );
476
- this._crdtNotifier.rootSubject.next(storageRoot);
477
- });
478
- this._subscriptions.observeCrdt = unsubscribe;
479
- }
480
- };
481
-
482
- // src/StorageStore.ts
483
- var StorageStore = class extends AbstractStorageStore {
484
- addUpdate() {
485
- return Promise.resolve();
486
- }
487
- applyUpdates() {
488
- return Promise.resolve();
489
- }
490
- destroy() {
491
- return Promise.resolve();
492
- }
493
- flatten() {
494
- return Promise.resolve();
495
- }
496
- getShouldFlatten() {
497
- return Promise.resolve(false);
498
- }
499
- getUpdates() {
500
- return Promise.resolve([]);
501
- }
502
- initialize() {
503
- return Promise.resolve();
504
- }
505
- };
506
-
507
359
  // src/utils/debounce.ts
508
360
  var debounce = (fn, options) => {
509
361
  const { wait, immediate = false } = options != null ? options : {};
@@ -541,6 +393,7 @@ var PluvRoom = class extends AbstractRoom {
541
393
  debug = false,
542
394
  initialPresence,
543
395
  initialStorage,
396
+ metadata,
544
397
  onAuthorizationFail,
545
398
  presence,
546
399
  wsEndpoint
@@ -551,6 +404,7 @@ var PluvRoom = class extends AbstractRoom {
551
404
  this._intervals = {
552
405
  heartbeat: null
553
406
  };
407
+ this._metadata = {};
554
408
  this._otherNotifier = new OtherNotifier();
555
409
  this._state = {
556
410
  authorization: {
@@ -681,9 +535,10 @@ var PluvRoom = class extends AbstractRoom {
681
535
  };
682
536
  const addon = this._getAddon(addons);
683
537
  const { storage } = __spreadValues(__spreadValues({}, DEFAULT_PLUV_CLIENT_ADDON({ room: this })), addon({ room: this }));
684
- this._storageStore = storage;
685
538
  this._debug = debug;
686
539
  this._endpoints = { authEndpoint, wsEndpoint };
540
+ this._storageStore = storage;
541
+ if (!!metadata) this._metadata = metadata;
687
542
  this._listeners = {
688
543
  onAuthorizationFail: (error) => {
689
544
  this._clearTimeout(this._timeouts.reconnect);
@@ -875,7 +730,7 @@ var PluvRoom = class extends AbstractRoom {
875
730
  options: {}
876
731
  };
877
732
  }
878
- const result = this._endpoints.authEndpoint(room);
733
+ const result = this._endpoints.authEndpoint({ metadata: this._metadata, room });
879
734
  return typeof result === "string" ? { url: result, options: {} } : result;
880
735
  }
881
736
  _getAuthorization(room) {
@@ -901,7 +756,7 @@ var PluvRoom = class extends AbstractRoom {
901
756
  case "string":
902
757
  return this._endpoints.wsEndpoint;
903
758
  default:
904
- return this._endpoints.wsEndpoint(room);
759
+ return this._endpoints.wsEndpoint({ metadata: this._metadata, room });
905
760
  }
906
761
  }
907
762
  _handleExit(message) {
@@ -1260,6 +1115,7 @@ var PluvClient = class {
1260
1115
  debug: options.debug,
1261
1116
  initialPresence: options.initialPresence,
1262
1117
  initialStorage: options.initialStorage,
1118
+ metadata: options.metadata,
1263
1119
  onAuthorizationFail: options.onAuthorizationFail,
1264
1120
  wsEndpoint: this._wsEndpoint
1265
1121
  });
@@ -1269,9 +1125,7 @@ var PluvClient = class {
1269
1125
  };
1270
1126
  this.enter = (room) => __async(this, null, function* () {
1271
1127
  const toEnter = typeof room === "string" ? this.getRoom(room) : room;
1272
- if (!toEnter) {
1273
- throw new Error(`Could not find room: ${room}.`);
1274
- }
1128
+ if (!toEnter) throw new Error(`Could not find room: ${room}.`);
1275
1129
  this._rooms.set(toEnter.id, toEnter);
1276
1130
  yield toEnter.connect();
1277
1131
  this._logDebug(`Entered room: ${room}`);
@@ -1307,6 +1161,154 @@ var PluvClient = class {
1307
1161
  var createClient = (options) => {
1308
1162
  return new PluvClient(options);
1309
1163
  };
1164
+
1165
+ // src/MockedRoom.ts
1166
+ var MockedRoom = class extends AbstractRoom {
1167
+ constructor(room, options) {
1168
+ const { events, initialPresence, initialStorage, presence } = options;
1169
+ super(room);
1170
+ this._crdtNotifier = new CrdtNotifier();
1171
+ this._eventNotifier = new EventNotifier();
1172
+ this._otherNotifier = new OtherNotifier();
1173
+ this._state = {
1174
+ authorization: {
1175
+ token: null,
1176
+ user: null
1177
+ },
1178
+ connection: {
1179
+ count: 1,
1180
+ id: null,
1181
+ state: "Untouched" /* Untouched */
1182
+ },
1183
+ webSocket: null
1184
+ };
1185
+ this._stateNotifier = new StateNotifier();
1186
+ this._subscriptions = {
1187
+ observeCrdt: null
1188
+ };
1189
+ this.canRedo = () => {
1190
+ return !!this._crdtManager.doc.canRedo();
1191
+ };
1192
+ this.canUndo = () => {
1193
+ return !!this._crdtManager.doc.canUndo();
1194
+ };
1195
+ this.event = (event, callback) => {
1196
+ return this._eventNotifier.subscribe(event, callback);
1197
+ };
1198
+ this.getConnection = () => {
1199
+ return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
1200
+ };
1201
+ this.getMyPresence = () => {
1202
+ return this._usersManager.myPresence;
1203
+ };
1204
+ this.getMyself = () => {
1205
+ return this._usersManager.myself;
1206
+ };
1207
+ this.getOther = (connectionId) => {
1208
+ return this._usersManager.getOther(connectionId);
1209
+ };
1210
+ this.getOthers = () => {
1211
+ return this._usersManager.getOthers();
1212
+ };
1213
+ this.getStorage = (type) => {
1214
+ const sharedType = this._crdtManager.get(type);
1215
+ if (typeof sharedType === "undefined") return null;
1216
+ return sharedType;
1217
+ };
1218
+ this.other = (connectionId, callback) => {
1219
+ return this._otherNotifier.subscribe(connectionId, callback);
1220
+ };
1221
+ this.redo = () => {
1222
+ this._crdtManager.doc.redo();
1223
+ };
1224
+ this.storage = (key, fn) => {
1225
+ return this._crdtNotifier.subscribe(key, fn);
1226
+ };
1227
+ this.storageRoot = (fn) => {
1228
+ return this._crdtNotifier.subcribeRoot(fn);
1229
+ };
1230
+ this.subscribe = (name, callback) => {
1231
+ return this._stateNotifier.subscribe(name, callback);
1232
+ };
1233
+ this.transact = (fn, origin) => {
1234
+ const _origin = origin != null ? origin : this._state.connection.id;
1235
+ const crdtManager = this._crdtManager;
1236
+ if (typeof _origin !== "string") return;
1237
+ if (!crdtManager) return;
1238
+ crdtManager.doc.transact(() => {
1239
+ const storage = crdtManager.doc.get();
1240
+ fn(storage);
1241
+ }, _origin);
1242
+ };
1243
+ this.undo = () => {
1244
+ this._crdtManager.doc.undo();
1245
+ };
1246
+ this.updateMyPresence = (presence) => {
1247
+ var _a;
1248
+ const newPresence = typeof presence === "function" ? presence(this.getMyPresence()) : presence;
1249
+ this._usersManager.updateMyPresence(newPresence);
1250
+ const myPresence = this._usersManager.myPresence;
1251
+ const myself = (_a = this._usersManager.myself) != null ? _a : null;
1252
+ this._stateNotifier.subjects["my-presence"].next(myPresence);
1253
+ !!myself && this._stateNotifier.subjects["myself"].next(myself);
1254
+ };
1255
+ this._events = events;
1256
+ this._usersManager = new UsersManager({
1257
+ initialPresence,
1258
+ presence
1259
+ });
1260
+ this._crdtManager = new CrdtManager({
1261
+ initialStorage
1262
+ });
1263
+ this._observeCrdt();
1264
+ }
1265
+ get storageLoaded() {
1266
+ return true;
1267
+ }
1268
+ broadcast(event, data) {
1269
+ if (!this._events) return;
1270
+ const resolver = this._events[event];
1271
+ if (!resolver) return;
1272
+ const result = resolver(data);
1273
+ Object.keys(result).forEach((type) => {
1274
+ const _type = type.toString();
1275
+ const data2 = result[_type];
1276
+ if (!data2) return;
1277
+ this._eventNotifier.subject(_type).next(data2);
1278
+ });
1279
+ }
1280
+ getDoc() {
1281
+ return this._crdtManager.doc;
1282
+ }
1283
+ getStorageJson(type) {
1284
+ if (this._state.connection.id === null) return null;
1285
+ if (typeof type === "undefined") return this._crdtManager.doc.toJson();
1286
+ return this._crdtManager.doc.toJson(type);
1287
+ }
1288
+ _observeCrdt() {
1289
+ var _a, _b;
1290
+ (_b = (_a = this._subscriptions).observeCrdt) == null ? void 0 : _b.call(_a);
1291
+ if (!this._crdtManager) return;
1292
+ const unsubscribe = this._crdtManager.doc.subscribe((event) => {
1293
+ var _a2;
1294
+ const origin = (_a2 = event.origin) != null ? _a2 : null;
1295
+ if (!this._crdtManager) return;
1296
+ if (origin === "$STORAGE_UPDATED") return;
1297
+ const sharedTypes = this._crdtManager.doc.get();
1298
+ const storageRoot = Object.keys(sharedTypes).reduce(
1299
+ (acc, prop) => {
1300
+ if (!this._crdtManager) return acc;
1301
+ const serialized = this._crdtManager.doc.toJson(prop);
1302
+ this._crdtNotifier.subject(prop).next(serialized);
1303
+ return __spreadProps(__spreadValues({}, acc), { [prop]: serialized });
1304
+ },
1305
+ {}
1306
+ );
1307
+ this._crdtNotifier.rootSubject.next(storageRoot);
1308
+ });
1309
+ this._subscriptions.observeCrdt = unsubscribe;
1310
+ }
1311
+ };
1310
1312
  // Annotate the CommonJS export names for ESM import in node:
1311
1313
  0 && (module.exports = {
1312
1314
  AbstractRoom,