playsocketjs 5.1.0 → 5.2.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/README.md +1 -1
- package/package.json +1 -1
- package/src/server/server.js +5 -3
- package/src/universal/constants.js +1 -1
- package/src/universal/crdtManager.js +11 -3
- package/types/server/server.d.ts +6 -2
- package/types/universal/constants.d.ts +1 -1
- package/types/universal/crdtManager.d.ts +5 -0
package/README.md
CHANGED
|
@@ -315,7 +315,7 @@ The callback signature is `callback(verified, code?, message?)` where `code` ref
|
|
|
315
315
|
| `kick()` | `clientId: string, reason?: string` | `void` | Kick a client by their client ID. |
|
|
316
316
|
| `move()` | `clientId: string, roomId: string` | `void` | Move a client that is already in a room to a different room. |
|
|
317
317
|
| `onEvent()` | `event: string, callback: Function` | `() => void` | Register a server-side event callback. Returns unsubscribe function. |
|
|
318
|
-
| `getRoomStorage()` | `roomId: string` | `object` | Get a snapshot of the current room storage. |
|
|
318
|
+
| `getRoomStorage()` | `roomId: string, options?: { dangerouslySkipCloning?: boolean }` | `object` | Get a snapshot of the current room storage. Pass `dangerouslySkipCloning: true` to get the uncloned store for performance reasons. |
|
|
319
319
|
| `getUpdateDetails()` | `update: object` | `object` | Get the details (`key`, `type`, `value` and `secondValue`) of a storage update for building validation logic. |
|
|
320
320
|
| `updateRoomStorage()` | `roomId: string, key: string, type: string, value: any, secondValue?: any` | `void` | Update a key in the shared storage of a room. |
|
|
321
321
|
| `createRoom()` | `initialStorage?: object, size?: number` | `object` | Create a server-owned room (returns object containing room ID and state).|
|
package/package.json
CHANGED
package/src/server/server.js
CHANGED
|
@@ -576,13 +576,15 @@ export default class PlaySocketServer {
|
|
|
576
576
|
}
|
|
577
577
|
|
|
578
578
|
/**
|
|
579
|
-
* Get snapshot of a room's storage
|
|
579
|
+
* Get a snapshot of a room's storage
|
|
580
580
|
* @param {string} roomId - ID of the room to get the storage from
|
|
581
|
+
* @param {object} [options] - Options
|
|
582
|
+
* @param {boolean} [options.dangerouslySkipCloning=false] - Return uncloned store (faster, but allows illegal mutation)
|
|
581
583
|
* @returns {Record<string, any> | undefined} Storage object or undefined if the room doesn't exist
|
|
582
584
|
*/
|
|
583
|
-
getRoomStorage(roomId) {
|
|
585
|
+
getRoomStorage(roomId, { dangerouslySkipCloning = false } = {}) {
|
|
584
586
|
const room = this.#rooms[roomId];
|
|
585
|
-
if (room) return room.crdtManager.propertyStore;
|
|
587
|
+
if (room) return dangerouslySkipCloning ? room.crdtManager.rawPropertyStore : room.crdtManager.propertyStore;
|
|
586
588
|
}
|
|
587
589
|
|
|
588
590
|
/**
|
|
@@ -366,12 +366,12 @@ export default class CRDTManager {
|
|
|
366
366
|
*/
|
|
367
367
|
#handleOperation(curValue, type, value, secondValue) {
|
|
368
368
|
try {
|
|
369
|
+
// Set operation (before the structured clone since for set we don't care about curValue anyway)
|
|
370
|
+
if (type === "set") return value;
|
|
371
|
+
|
|
369
372
|
// Deep copy to avoid reference issues in case value is or contains object(s)
|
|
370
373
|
curValue = structuredClone(curValue);
|
|
371
374
|
|
|
372
|
-
// Set operation
|
|
373
|
-
if (type === "set") return value;
|
|
374
|
-
|
|
375
375
|
// Number increment operation
|
|
376
376
|
if (type === "number-increment") {
|
|
377
377
|
if (!Number.isFinite(curValue)) curValue = 0; // Auto-convert to number if current value isn't a finite number
|
|
@@ -461,6 +461,14 @@ export default class CRDTManager {
|
|
|
461
461
|
}
|
|
462
462
|
}
|
|
463
463
|
|
|
464
|
+
/**
|
|
465
|
+
* Get uncloned property store
|
|
466
|
+
* @returns {Record<string, any>} Object with key value pairs
|
|
467
|
+
*/
|
|
468
|
+
get rawPropertyStore() {
|
|
469
|
+
return this.#propertyStore;
|
|
470
|
+
}
|
|
471
|
+
|
|
464
472
|
/**
|
|
465
473
|
* Check for changes in the local property store
|
|
466
474
|
* @returns {boolean} Check if any values changed going from the last to the current property update
|
package/types/server/server.d.ts
CHANGED
|
@@ -145,11 +145,15 @@ export default class PlaySocketServer {
|
|
|
145
145
|
*/
|
|
146
146
|
move(clientId: string, roomId: string): void;
|
|
147
147
|
/**
|
|
148
|
-
* Get snapshot of a room's storage
|
|
148
|
+
* Get a snapshot of a room's storage
|
|
149
149
|
* @param {string} roomId - ID of the room to get the storage from
|
|
150
|
+
* @param {object} [options] - Options
|
|
151
|
+
* @param {boolean} [options.dangerouslySkipCloning=false] - Return uncloned store (faster, but allows illegal mutation)
|
|
150
152
|
* @returns {Record<string, any> | undefined} Storage object or undefined if the room doesn't exist
|
|
151
153
|
*/
|
|
152
|
-
getRoomStorage(roomId: string
|
|
154
|
+
getRoomStorage(roomId: string, { dangerouslySkipCloning }?: {
|
|
155
|
+
dangerouslySkipCloning?: boolean;
|
|
156
|
+
}): Record<string, any> | undefined;
|
|
153
157
|
/**
|
|
154
158
|
* Get the operation details from a storage update (e.g. in the "storageUpdateRequested" event)
|
|
155
159
|
* @param {PropertyUpdate} update - Property update
|
|
@@ -123,6 +123,11 @@ export default class CRDTManager {
|
|
|
123
123
|
* @returns {Record<string, any>} Object with key value pairs
|
|
124
124
|
*/
|
|
125
125
|
get propertyStore(): Record<string, any>;
|
|
126
|
+
/**
|
|
127
|
+
* Get uncloned property store
|
|
128
|
+
* @returns {Record<string, any>} Object with key value pairs
|
|
129
|
+
*/
|
|
130
|
+
get rawPropertyStore(): Record<string, any>;
|
|
126
131
|
/**
|
|
127
132
|
* Check for changes in the local property store
|
|
128
133
|
* @returns {boolean} Check if any values changed going from the last to the current property update
|