playsocketjs 1.0.3 → 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,14 +332,14 @@ 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;
336
- this.#storage[key] = value;
337
- this.#triggerEvent("storageUpdated", { ...this.#storage });
335
+ if (JSON.stringify(this.#storage[key]) === JSON.stringify(value)) return; // Prevent updates without changes
338
336
  this.#sendToServer({
339
337
  type: 'room_storage_update',
340
338
  key,
341
339
  value
342
340
  });
341
+ this.#storage[key] = value;
342
+ this.#triggerEvent("storageUpdated", { ...this.#storage });
343
343
  }
344
344
 
345
345
  /**
@@ -350,7 +350,8 @@ class PlaySocket {
350
350
  * @param {*} updateValue - New value for update-matching
351
351
  */
352
352
  updateStorageArray(key, operation, value, updateValue) {
353
- this.#handleArrayUpdate(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,6 +359,8 @@ class PlaySocket {
358
359
  value,
359
360
  updateValue
360
361
  });
362
+ this.#storage[key] = updatedArray;
363
+ this.#triggerEvent("storageUpdated", { ...this.#storage });
361
364
  }
362
365
 
363
366
  /**
@@ -365,12 +368,9 @@ class PlaySocket {
365
368
  * @private
366
369
  */
367
370
  #handleArrayUpdate(key, operation, value, updateValue) {
368
- if (!this.#storage[key] || !Array.isArray(this.#storage[key])) this.#storage[key] = [];
369
- let array = this.#storage[key];
370
-
371
+ let array = (!this.#storage[key] || !Array.isArray(this.#storage[key])) ? [] : [...this.#storage[key]];
371
372
  const isObject = typeof value === 'object' && value !== null;
372
- const compare = (item) => isObject ?
373
- JSON.stringify(item) === JSON.stringify(value) : item === value;
373
+ const compare = (item) => isObject ? JSON.stringify(item) === JSON.stringify(value) : item === value;
374
374
 
375
375
  switch (operation) {
376
376
  case 'add':
@@ -382,7 +382,7 @@ class PlaySocket {
382
382
  break;
383
383
 
384
384
  case 'remove-matching':
385
- this.#storage[key] = array.filter(item => !compare(item));
385
+ array = array.filter(item => !compare(item));
386
386
  break;
387
387
 
388
388
  case 'update-matching':
@@ -392,10 +392,9 @@ class PlaySocket {
392
392
 
393
393
  default:
394
394
  console.error(ERROR_PREFIX + `Unknown array operation: ${operation}`);
395
- return;
396
395
  }
397
396
 
398
- this.#triggerEvent("storageUpdated", { ...this.#storage });
397
+ return array;
399
398
  }
400
399
 
401
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.3",
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": {