Skip to content

Commit

Permalink
fix: clearer array update handling
Browse files Browse the repository at this point in the history
  • Loading branch information
therealPaulPlay committed Jan 31, 2025
1 parent 994be1f commit 17f428d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 7 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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);
}
Expand All @@ -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;

Expand All @@ -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
}

/**
Expand Down

0 comments on commit 17f428d

Please sign in to comment.