Skip to content

Commit

Permalink
Feat: Prevent storage updates and synchronization if storage is 1:1
Browse files Browse the repository at this point in the history
  • Loading branch information
therealPaulPlay committed Jan 27, 2025
1 parent aa63646 commit fa5358f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 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.1.4",
"version": "1.1.5",
"description": "WebRTC-based wrapper for creating robust peer-2-peer multiplayer systems with ease.",
"type": "module",
"main": "dist/playpeer.js",
Expand Down
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,8 @@ export default class PlayPeer {
switch (data.type) {
case 'storage_update':
// Storage updates, sent out by clients
if (this.#isHost) {
this.#setStorageLocally(data.key, data.value);
this.#broadcastMessage("storage_sync", { storage: this.#storage });
if (this.#isHost && data.value && data.key) {
this.updateStorage(data.key, data.value);
}
break;
case 'heartbeat_request': {
Expand Down Expand Up @@ -346,8 +345,11 @@ export default class PlayPeer {
if (!data || !data?.type) return;
switch (data.type) {
case 'storage_sync':
this.#storage = data.storage;
this.#triggerEvent("storageUpdate", { ...this.#storage });
// Update storage with host sync only if local save isn't identical
if (JSON.stringify(this.#storage) !== JSON.stringify(data.storage)) {
this.#storage = data.storage;
this.#triggerEvent("storageUpdate", { ...this.#storage });
}
break;
case 'peer_list':
this.#hostConnectionsIdArray = data.peers;
Expand Down Expand Up @@ -386,6 +388,7 @@ export default class PlayPeer {
* @param {*} value - New value
*/
updateStorage(key, value) {
if (JSON.stringify(this.#storage[key]) === JSON.stringify(value)) return; // If the key already has this value, exit
if (this.#isHost) {
this.#setStorageLocally(key, value);
this.#broadcastMessage("storage_sync", { storage: this.#storage });
Expand Down

0 comments on commit fa5358f

Please sign in to comment.