playsocketjs 1.0.12 → 1.0.13

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.
@@ -404,7 +404,7 @@ class PlaySocket {
404
404
  key,
405
405
  value
406
406
  });
407
- // Optimistic update for regular storage updates
407
+ // Optimistic update
408
408
  this.#storage[key] = value;
409
409
  this.#triggerEvent("storageUpdated", { ...this.#storage });
410
410
  }
@@ -417,8 +417,8 @@ class PlaySocket {
417
417
  * @param {*} updateValue - New value for update-matching
418
418
  */
419
419
  updateStorageArray(key, operation, value, updateValue) {
420
- // Send operation to server (no optimistic update – we can't guarantee that it is correct)
421
- // If another client has sent an update to the server that is processed first, the end result will differ.
420
+ const updatedArray = this.#handleArrayUpdate(key, operation, value, updateValue);
421
+ if (JSON.stringify(this.#storage[key]) === JSON.stringify(updatedArray)) return; // Prevent updates without changes
422
422
  this.#sendToServer({
423
423
  type: 'room_storage_array_update',
424
424
  key,
@@ -426,6 +426,43 @@ class PlaySocket {
426
426
  value,
427
427
  updateValue
428
428
  });
429
+ // Optimistic update
430
+ this.#storage[key] = updatedArray;
431
+ this.#triggerEvent("storageUpdated", { ...this.#storage });
432
+ }
433
+
434
+ /**
435
+ * Handle array operations client-side
436
+ * @private
437
+ */
438
+ #handleArrayUpdate(key, operation, value, updateValue) {
439
+ let array = (!this.#storage[key] || !Array.isArray(this.#storage[key])) ? [] : [...this.#storage[key]];
440
+ const isObject = typeof value === 'object' && value !== null;
441
+ const compare = (item) => isObject ? JSON.stringify(item) === JSON.stringify(value) : item === value;
442
+
443
+ switch (operation) {
444
+ case 'add':
445
+ array.push(value);
446
+ break;
447
+
448
+ case 'add-unique':
449
+ if (!array.some(compare)) array.push(value);
450
+ break;
451
+
452
+ case 'remove-matching':
453
+ array = array.filter(item => !compare(item));
454
+ break;
455
+
456
+ case 'update-matching':
457
+ const index = array.findIndex(compare);
458
+ if (index !== -1) array[index] = updateValue;
459
+ break;
460
+
461
+ default:
462
+ console.error(ERROR_PREFIX + `Unknown array operation: ${operation}`);
463
+ }
464
+
465
+ return array;
429
466
  }
430
467
 
431
468
  /**
@@ -457,7 +494,7 @@ class PlaySocket {
457
494
 
458
495
  // Public getters
459
496
  get connectionCount() { return this.#connectionCount; }
460
- get getStorage() { return { ...this.#storage }; }
497
+ get getStorage() { return JSON.parse(JSON.stringify(this.#storage)); }
461
498
  get isHost() { return this.#id == this.#roomHost; }
462
499
  get id() { return this.#id; }
463
500
  }
@@ -221,7 +221,7 @@ function requireServer () {
221
221
  const updatedArray = this.#arrayUpdate(arrayRoom.storage, data.key, data.operation, data.value, data.updateValue);
222
222
  if (JSON.stringify(arrayRoom.storage[data.key]) !== JSON.stringify(updatedArray)) {
223
223
  arrayRoom.storage[data.key] = updatedArray;
224
- this.#syncRoomStorageKey(arrayRoom, data.key);
224
+ this.#syncRoomStorageKey(arrayRoom, data.key, ws.clientId);
225
225
  }
226
226
  }
227
227
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "module": "dist/playsocket-client.js",
6
6
  "exports": {