From 17f428d370246c6d11c9529bedab850dc3e728a8 Mon Sep 17 00:00:00 2001 From: PaulPlay <80539587+therealPaulPlay@users.noreply.github.com> Date: Fri, 31 Jan 2025 22:49:50 +0100 Subject: [PATCH] fix: clearer array update handling --- package.json | 2 +- src/index.js | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index c8d47cb..8515e72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "playpeerjs", - "version": "1.2.1", + "version": "1.2.2", "description": "WebRTC-based wrapper for creating robust peer-2-peer multiplayer systems with ease.", "type": "module", "main": "dist/playpeer.js", diff --git a/src/index.js b/src/index.js index 6f81fd7..4176db8 100644 --- a/src/index.js +++ b/src/index.js @@ -438,14 +438,12 @@ export default class PlayPeer { * @param {*} updateValue */ #handleArrayUpdate(key, operation, value, updateValue) { - const updatedArray = this.#storage?.[key] || []; - if (!Array.isArray(this.#storage?.[key])) { - this.#storage[key] = []; // Ensure it's an array if it wasn't already - } + let updatedArray = this.#storage?.[key] || []; + if (!Array.isArray(this.#storage?.[key])) this.#storage[key] = []; // Ensure it's an array if it wasn't already switch (operation) { case 'add': - this.#storage[key].push(value); + updatedArray.push(value); break; case 'add-unique': @@ -458,13 +456,13 @@ export default class PlayPeer { }); if (uniqueIndex == -1) { - this.#storage[key].push(value); // Add the unique value + updatedArray.push(value); // Add the unique value } break; case 'remove-matching': // Remove matching value (deep comparison for objects) - this.#storage[key] = updatedArray.filter(item => { + updatedArray = updatedArray.filter(item => { if (typeof value === 'object' && value !== null) { return JSON.stringify(item) !== JSON.stringify(value); } @@ -482,7 +480,7 @@ export default class PlayPeer { }); if (updateIndex > -1) { - this.#storage[key][updateIndex] = updateValue; // Perform the update + updatedArray[updateIndex] = updateValue; // Perform the update } break; @@ -491,7 +489,7 @@ export default class PlayPeer { this.#triggerEvent("error", `Unknown array operation: ${operation}`); } - this.#setStorageLocally(key, this.#storage[key]); // Update storage locally + this.#setStorageLocally(key, updatedArray); // Update storage locally } /**