@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 +1 -0
- package/dist/index.js +36 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/decorators.ts +2 -0
- package/src/server.ts +43 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@signe/room",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "PartyKit room primitives with synchronized state, sessions, guards, and HTTP handlers.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"dset": "^3.1.3",
|
|
32
32
|
"partysocket": "^1.0.1",
|
|
33
33
|
"zod": "^3.23.8",
|
|
34
|
-
"@signe/sync": "3.0
|
|
34
|
+
"@signe/sync": "3.1.0"
|
|
35
35
|
},
|
|
36
36
|
"publishConfig": {
|
|
37
37
|
"access": "public"
|
package/src/decorators.ts
CHANGED
|
@@ -62,6 +62,7 @@ export function Request(options: RequestOptions, bodyValidation?: z.ZodSchema) {
|
|
|
62
62
|
export interface RoomOptions {
|
|
63
63
|
path: string;
|
|
64
64
|
maxUsers?: number;
|
|
65
|
+
persistState?: boolean;
|
|
65
66
|
throttleStorage?: number;
|
|
66
67
|
throttleSync?: number;
|
|
67
68
|
hibernate?: boolean;
|
|
@@ -73,6 +74,7 @@ export function Room(options: RoomOptions) {
|
|
|
73
74
|
return function (target: any) {
|
|
74
75
|
target.path = options.path;
|
|
75
76
|
target.prototype.maxUsers = options.maxUsers;
|
|
77
|
+
target.prototype.persistState = options.persistState ?? true;
|
|
76
78
|
target.prototype.throttleStorage = options.throttleStorage;
|
|
77
79
|
target.prototype.throttleSync = options.throttleSync;
|
|
78
80
|
target.prototype.sessionExpiryTime = options.sessionExpiryTime ?? 5 * 60 * 1000;
|
package/src/server.ts
CHANGED
|
@@ -63,6 +63,7 @@ type TransferData = {
|
|
|
63
63
|
publicId: string;
|
|
64
64
|
restored: boolean;
|
|
65
65
|
created: number;
|
|
66
|
+
userSnapshot?: any;
|
|
66
67
|
};
|
|
67
68
|
|
|
68
69
|
type StorageMetrics = {
|
|
@@ -771,6 +772,7 @@ export class Server implements Party.Server {
|
|
|
771
772
|
|
|
772
773
|
instance.$memoryAll = {}
|
|
773
774
|
instance.$storageMetrics = this.createStorageMetrics();
|
|
775
|
+
const shouldPersistState = instance["persistState"] !== false;
|
|
774
776
|
instance.$autoSync = instance["autoSync"] !== false; // Default to true
|
|
775
777
|
instance.$pendingSync = new Map<string, any>();
|
|
776
778
|
instance.$pendingInitialSync = new Map<Party.Connection, string>(); // Store connections waiting for initial sync with their publicId
|
|
@@ -1043,13 +1045,21 @@ export class Server implements Party.Server {
|
|
|
1043
1045
|
};
|
|
1044
1046
|
};
|
|
1045
1047
|
|
|
1048
|
+
const disabledPersist = (values: Map<string, any>) => {
|
|
1049
|
+
values.clear();
|
|
1050
|
+
};
|
|
1051
|
+
|
|
1046
1052
|
// Set up syncing and persistence with throttling to optimize performance
|
|
1047
1053
|
syncClass(instance, {
|
|
1048
1054
|
onSync: instance["throttleSync"] ? throttle(syncCb, instance["throttleSync"]) : syncCb,
|
|
1049
|
-
onPersist:
|
|
1055
|
+
onPersist: shouldPersistState
|
|
1056
|
+
? instance["throttleStorage"] ? debouncePersist(instance["throttleStorage"]) : persistCb
|
|
1057
|
+
: disabledPersist,
|
|
1050
1058
|
});
|
|
1051
1059
|
|
|
1052
|
-
|
|
1060
|
+
if (shouldPersistState) {
|
|
1061
|
+
await loadMemory();
|
|
1062
|
+
}
|
|
1053
1063
|
|
|
1054
1064
|
initPersist = false
|
|
1055
1065
|
init = false; // Allow syncs after initialization is complete
|
|
@@ -1350,24 +1360,42 @@ export class Server implements Party.Server {
|
|
|
1350
1360
|
let user = null;
|
|
1351
1361
|
const signal = this.getUsersProperty(subRoom);
|
|
1352
1362
|
const usersPropName = this.getUsersPropName(subRoom);
|
|
1363
|
+
const shouldPersistState = subRoom["persistState"] !== false;
|
|
1353
1364
|
|
|
1354
1365
|
if (signal) {
|
|
1355
1366
|
const { classType } = signal.options;
|
|
1367
|
+
const createUser = async (id: string, snapshot?: any) => {
|
|
1368
|
+
const nextUser = this.createUserFromClassType(classType, conn, ctx);
|
|
1369
|
+
signal()[id] = nextUser;
|
|
1370
|
+
if (snapshot !== undefined) {
|
|
1371
|
+
load(nextUser, snapshot, true);
|
|
1372
|
+
}
|
|
1373
|
+
if (shouldPersistState) {
|
|
1374
|
+
const persistedSnapshot = createStatesSnapshotDeep(nextUser);
|
|
1375
|
+
await this.saveStatePath(`${usersPropName}.${id}`, persistedSnapshot);
|
|
1376
|
+
}
|
|
1377
|
+
return nextUser;
|
|
1378
|
+
};
|
|
1356
1379
|
|
|
1357
1380
|
// Restore state if exists
|
|
1358
1381
|
if (!existingSession?.publicId) {
|
|
1359
1382
|
// Check if we have a transferred user already restored
|
|
1360
1383
|
if (transferData?.restored && signal()[publicId]) {
|
|
1361
1384
|
user = signal()[publicId];
|
|
1385
|
+
} else if (transferData?.restored && transferData.userSnapshot !== undefined) {
|
|
1386
|
+
user = await createUser(publicId, transferData.userSnapshot);
|
|
1362
1387
|
} else {
|
|
1363
|
-
user =
|
|
1364
|
-
signal()[publicId] = user;
|
|
1365
|
-
const snapshot = createStatesSnapshotDeep(user);
|
|
1366
|
-
await this.saveStatePath(`${usersPropName}.${publicId}`, snapshot);
|
|
1388
|
+
user = await createUser(publicId);
|
|
1367
1389
|
}
|
|
1368
1390
|
}
|
|
1369
1391
|
else {
|
|
1370
1392
|
user = signal()[existingSession.publicId];
|
|
1393
|
+
if (!user) {
|
|
1394
|
+
const snapshot = transferData?.publicId === existingSession.publicId
|
|
1395
|
+
? transferData.userSnapshot
|
|
1396
|
+
: undefined;
|
|
1397
|
+
user = await createUser(existingSession.publicId, snapshot);
|
|
1398
|
+
}
|
|
1371
1399
|
}
|
|
1372
1400
|
|
|
1373
1401
|
// Only store new session if it doesn't exist
|
|
@@ -1897,6 +1925,8 @@ export class Server implements Party.Server {
|
|
|
1897
1925
|
if (!subRoom) {
|
|
1898
1926
|
return res.serverError('Room not available');
|
|
1899
1927
|
}
|
|
1928
|
+
const shouldPersistState = subRoom["persistState"] !== false;
|
|
1929
|
+
let restoredUserSnapshot: any;
|
|
1900
1930
|
|
|
1901
1931
|
// Create session from privateId
|
|
1902
1932
|
await this.saveSession(privateId, {
|
|
@@ -1934,9 +1964,12 @@ export class Server implements Party.Server {
|
|
|
1934
1964
|
|
|
1935
1965
|
// Load user data from snapshot
|
|
1936
1966
|
load(user, hydratedSnapshot, true);
|
|
1967
|
+
restoredUserSnapshot = createStatesSnapshotDeep(user);
|
|
1937
1968
|
|
|
1938
1969
|
// Save user snapshot to storage
|
|
1939
|
-
|
|
1970
|
+
if (shouldPersistState) {
|
|
1971
|
+
await this.saveStatePath(`${usersPropName}.${publicId}`, restoredUserSnapshot);
|
|
1972
|
+
}
|
|
1940
1973
|
}
|
|
1941
1974
|
}
|
|
1942
1975
|
|
|
@@ -1946,6 +1979,9 @@ export class Server implements Party.Server {
|
|
|
1946
1979
|
privateId,
|
|
1947
1980
|
publicId,
|
|
1948
1981
|
restored: true,
|
|
1982
|
+
...(shouldPersistState || restoredUserSnapshot === undefined
|
|
1983
|
+
? {}
|
|
1984
|
+
: { userSnapshot: restoredUserSnapshot }),
|
|
1949
1985
|
created: Date.now(),
|
|
1950
1986
|
});
|
|
1951
1987
|
|