playsocketjs 1.0.4 → 1.0.5

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.
@@ -332,13 +332,12 @@ class PlaySocket {
332
332
  * @param {*} value - New value
333
333
  */
334
334
  updateStorage(key, value) {
335
- if (JSON.stringify(this.#storage[key]) === JSON.stringify(value)) return;
335
+ if (JSON.stringify(this.#storage[key]) === JSON.stringify(value)) return; // Prevent updates without changes
336
336
  this.#sendToServer({
337
337
  type: 'room_storage_update',
338
338
  key,
339
339
  value
340
340
  });
341
- // Optimistic update AFTER sendToServer to preserve order if messages are being sent in the event callback
342
341
  this.#storage[key] = value;
343
342
  this.#triggerEvent("storageUpdated", { ...this.#storage });
344
343
  }
@@ -351,6 +350,8 @@ class PlaySocket {
351
350
  * @param {*} updateValue - New value for update-matching
352
351
  */
353
352
  updateStorageArray(key, operation, value, updateValue) {
353
+ const updatedArray = this.#handleArrayUpdate(key, operation, value, updateValue);
354
+ if (JSON.stringify(this.#storage[key]) === JSON.stringify(updatedArray)) return; // Prevent updates without changes
354
355
  this.#sendToServer({
355
356
  type: 'room_storage_array_update',
356
357
  key,
@@ -358,8 +359,8 @@ class PlaySocket {
358
359
  value,
359
360
  updateValue
360
361
  });
361
- // Optimistic update AFTER sendToServer to preserve order if messages are being sent in the event callback
362
- this.#handleArrayUpdate(key, operation, value, updateValue);
362
+ this.#storage[key] = updatedArray;
363
+ this.#triggerEvent("storageUpdated", { ...this.#storage });
363
364
  }
364
365
 
365
366
  /**
@@ -367,12 +368,9 @@ class PlaySocket {
367
368
  * @private
368
369
  */
369
370
  #handleArrayUpdate(key, operation, value, updateValue) {
370
- if (!this.#storage[key] || !Array.isArray(this.#storage[key])) this.#storage[key] = [];
371
- let array = this.#storage[key];
372
-
371
+ let array = (!this.#storage[key] || !Array.isArray(this.#storage[key])) ? [] : [...this.#storage[key]];
373
372
  const isObject = typeof value === 'object' && value !== null;
374
- const compare = (item) => isObject ?
375
- JSON.stringify(item) === JSON.stringify(value) : item === value;
373
+ const compare = (item) => isObject ? JSON.stringify(item) === JSON.stringify(value) : item === value;
376
374
 
377
375
  switch (operation) {
378
376
  case 'add':
@@ -384,7 +382,7 @@ class PlaySocket {
384
382
  break;
385
383
 
386
384
  case 'remove-matching':
387
- this.#storage[key] = array.filter(item => !compare(item));
385
+ array = array.filter(item => !compare(item));
388
386
  break;
389
387
 
390
388
  case 'update-matching':
@@ -394,10 +392,9 @@ class PlaySocket {
394
392
 
395
393
  default:
396
394
  console.error(ERROR_PREFIX + `Unknown array operation: ${operation}`);
397
- return;
398
395
  }
399
396
 
400
- this.#triggerEvent("storageUpdated", { ...this.#storage });
397
+ return array;
401
398
  }
402
399
 
403
400
  /**
@@ -169,7 +169,7 @@ function requireServer () {
169
169
  case 'room_storage_update':
170
170
  const updateRoomId = this.#clientRooms.get(ws.clientId);
171
171
  const updateRoom = updateRoomId ? this.#rooms[updateRoomId] : null;
172
- if (updateRoom && data.key) {
172
+ if (updateRoom && data.key && JSON.stringify(updateRoom.storage[data.key]) !== JSON.stringify(data.value)) {
173
173
  updateRoom.storage[data.key] = data.value;
174
174
  this.#syncRoomStorageKey(updateRoomId, data.key);
175
175
  }
@@ -179,8 +179,11 @@ function requireServer () {
179
179
  const arrayRoomId = this.#clientRooms.get(ws.clientId);
180
180
  const arrayRoom = arrayRoomId ? this.#rooms[arrayRoomId] : null;
181
181
  if (arrayRoom && data.key) {
182
- this.#arrayUpdate(arrayRoom.storage, data.key, data.operation, data.value, data.updateValue);
183
- this.#syncRoomStorageKey(arrayRoomId, data.key);
182
+ const updatedArray = this.#arrayUpdate(arrayRoom.storage, data.key, data.operation, data.value, data.updateValue);
183
+ if (JSON.stringify(arrayRoom.storage[data.key]) !== JSON.stringify(updatedArray)) {
184
+ arrayRoom.storage[data.key] = updatedArray;
185
+ this.#syncRoomStorageKey(arrayRoomId, data.key);
186
+ }
184
187
  }
185
188
  break;
186
189
  }
@@ -224,7 +227,6 @@ function requireServer () {
224
227
  #syncRoomStorageKey(roomId, key) {
225
228
  const room = this.#rooms[roomId];
226
229
  if (!room) return;
227
-
228
230
  room.participants.forEach(p => {
229
231
  const client = this.#clients.get(p);
230
232
  if (client) {
@@ -242,12 +244,9 @@ function requireServer () {
242
244
  * @private
243
245
  */
244
246
  #arrayUpdate(storage, key, operation, value, updateValue) {
245
- if (!storage[key] || !Array.isArray(storage[key])) storage[key] = [];
246
- let array = storage[key];
247
-
247
+ let array = (!storage[key] || !Array.isArray(storage[key])) ? [] : [...storage[key]];
248
248
  const isObject = typeof value === 'object' && value !== null;
249
- const compare = (item) => isObject ?
250
- JSON.stringify(item) === JSON.stringify(value) : item === value;
249
+ const compare = (item) => isObject ? JSON.stringify(item) === JSON.stringify(value) : item === value;
251
250
 
252
251
  switch (operation) {
253
252
  case 'add':
@@ -267,6 +266,8 @@ function requireServer () {
267
266
  if (index !== -1) array[index] = updateValue;
268
267
  break;
269
268
  }
269
+
270
+ return array;
270
271
  }
271
272
 
272
273
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "module": "dist/playsocket-client.js",
6
6
  "exports": {