@signe/room 3.0.1 → 3.1.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.d.ts CHANGED
@@ -24,6 +24,7 @@ declare function Request(options: RequestOptions, bodyValidation?: z.ZodSchema):
24
24
  interface RoomOptions {
25
25
  path: string;
26
26
  maxUsers?: number;
27
+ persistState?: boolean;
27
28
  throttleStorage?: number;
28
29
  throttleSync?: number;
29
30
  hibernate?: boolean;
package/dist/index.js CHANGED
@@ -41,6 +41,7 @@ function Room(options) {
41
41
  return function(target) {
42
42
  target.path = options.path;
43
43
  target.prototype.maxUsers = options.maxUsers;
44
+ target.prototype.persistState = options.persistState ?? true;
44
45
  target.prototype.throttleStorage = options.throttleStorage;
45
46
  target.prototype.throttleSync = options.throttleSync;
46
47
  target.prototype.sessionExpiryTime = options.sessionExpiryTime ?? 5 * 60 * 1e3;
@@ -991,6 +992,7 @@ var Server = class {
991
992
  };
992
993
  instance.$memoryAll = {};
993
994
  instance.$storageMetrics = this.createStorageMetrics();
995
+ const shouldPersistState = instance["persistState"] !== false;
994
996
  instance.$autoSync = instance["autoSync"] !== false;
995
997
  instance.$pendingSync = /* @__PURE__ */ new Map();
996
998
  instance.$pendingInitialSync = /* @__PURE__ */ new Map();
@@ -1196,11 +1198,16 @@ var Server = class {
1196
1198
  schedule();
1197
1199
  };
1198
1200
  };
1201
+ const disabledPersist = (values) => {
1202
+ values.clear();
1203
+ };
1199
1204
  syncClass(instance, {
1200
1205
  onSync: instance["throttleSync"] ? throttle(syncCb, instance["throttleSync"]) : syncCb,
1201
- onPersist: instance["throttleStorage"] ? debouncePersist(instance["throttleStorage"]) : persistCb
1206
+ onPersist: shouldPersistState ? instance["throttleStorage"] ? debouncePersist(instance["throttleStorage"]) : persistCb : disabledPersist
1202
1207
  });
1203
- await loadMemory();
1208
+ if (shouldPersistState) {
1209
+ await loadMemory();
1210
+ }
1204
1211
  initPersist = false;
1205
1212
  init = false;
1206
1213
  return instance;
@@ -1462,19 +1469,35 @@ var Server = class {
1462
1469
  let user = null;
1463
1470
  const signal2 = this.getUsersProperty(subRoom);
1464
1471
  const usersPropName = this.getUsersPropName(subRoom);
1472
+ const shouldPersistState = subRoom["persistState"] !== false;
1465
1473
  if (signal2) {
1466
1474
  const { classType } = signal2.options;
1475
+ const createUser = async (id2, snapshot) => {
1476
+ const nextUser = this.createUserFromClassType(classType, conn, ctx);
1477
+ signal2()[id2] = nextUser;
1478
+ if (snapshot !== void 0) {
1479
+ load(nextUser, snapshot, true);
1480
+ }
1481
+ if (shouldPersistState) {
1482
+ const persistedSnapshot = createStatesSnapshotDeep(nextUser);
1483
+ await this.saveStatePath(`${usersPropName}.${id2}`, persistedSnapshot);
1484
+ }
1485
+ return nextUser;
1486
+ };
1467
1487
  if (!existingSession?.publicId) {
1468
1488
  if (transferData?.restored && signal2()[publicId]) {
1469
1489
  user = signal2()[publicId];
1490
+ } else if (transferData?.restored && transferData.userSnapshot !== void 0) {
1491
+ user = await createUser(publicId, transferData.userSnapshot);
1470
1492
  } else {
1471
- user = this.createUserFromClassType(classType, conn, ctx);
1472
- signal2()[publicId] = user;
1473
- const snapshot = createStatesSnapshotDeep(user);
1474
- await this.saveStatePath(`${usersPropName}.${publicId}`, snapshot);
1493
+ user = await createUser(publicId);
1475
1494
  }
1476
1495
  } else {
1477
1496
  user = signal2()[existingSession.publicId];
1497
+ if (!user) {
1498
+ const snapshot = transferData?.publicId === existingSession.publicId ? transferData.userSnapshot : void 0;
1499
+ user = await createUser(existingSession.publicId, snapshot);
1500
+ }
1478
1501
  }
1479
1502
  if (!existingSession) {
1480
1503
  await this.saveSession(privateId, {
@@ -1888,6 +1911,8 @@ var Server = class {
1888
1911
  if (!subRoom) {
1889
1912
  return res.serverError("Room not available");
1890
1913
  }
1914
+ const shouldPersistState = subRoom["persistState"] !== false;
1915
+ let restoredUserSnapshot;
1891
1916
  await this.saveSession(privateId, {
1892
1917
  publicId,
1893
1918
  state: sessionState,
@@ -1913,7 +1938,10 @@ var Server = class {
1913
1938
  ) ?? userSnapshot;
1914
1939
  signal2()[publicId] = user;
1915
1940
  load(user, hydratedSnapshot, true);
1916
- await this.saveStatePath(`${usersPropName}.${publicId}`, userSnapshot);
1941
+ restoredUserSnapshot = createStatesSnapshotDeep(user);
1942
+ if (shouldPersistState) {
1943
+ await this.saveStatePath(`${usersPropName}.${publicId}`, restoredUserSnapshot);
1944
+ }
1917
1945
  }
1918
1946
  }
1919
1947
  const transferToken = generateShortUUID2();
@@ -1921,6 +1949,7 @@ var Server = class {
1921
1949
  privateId,
1922
1950
  publicId,
1923
1951
  restored: true,
1952
+ ...shouldPersistState || restoredUserSnapshot === void 0 ? {} : { userSnapshot: restoredUserSnapshot },
1924
1953
  created: Date.now()
1925
1954
  });
1926
1955
  return res.success({ transferToken });