Skip to content

Commit

Permalink
fix: Deep compare objects in dataUpdate event to fix false positives …
Browse files Browse the repository at this point in the history
…and improve debug logging
  • Loading branch information
3urobeat committed Jan 11, 2025
1 parent 0272d30 commit e91c731
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/controller/events/dataUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2025-01-07 17:18:36
* Author: 3urobeat
*
* Last Modified: 2025-01-11 16:48:01
* Last Modified: 2025-01-11 18:27:38
* Modified By: 3urobeat
*
* Copyright (c) 2025 3urobeat <https://github.com/3urobeat>
Expand All @@ -15,6 +15,8 @@
*/


const util = require("util");

const Controller = require("../controller");


Expand All @@ -27,12 +29,24 @@ const Controller = require("../controller");
Controller.prototype._dataUpdateEvent = function(key, oldData, newData) {

// Calculate changes. If !oldData (e.g. on dataExport) count it as a change
const changes = Object.keys(newData).filter((e) => !oldData || oldData[e] != newData[e]); // TODO: Could provide more info tbh
const changes = Object.keys(newData).filter((e) => {
// Use JSON.stringify to deep compare objects, as obj == obj will nearly always be false because thats how programming languages work
const isDifferent = !oldData || JSON.stringify(oldData[e]) != JSON.stringify(newData[e]);

// Log debug message
if (isDifferent && oldData) {
logger("debug", `Event dataUpdate: DataManager key '${key}' got updated. Difference - old '${util.inspect(oldData[e], false, 2, true)}' | new '${util.inspect(newData[e], false, 2, true)}'`);
}

return isDifferent;
});

if (changes.length == 0) return logger("debug", `Event dataUpdate: DataManager key '${key}' did not change`);

// Log debug message
logger("debug", `Event dataUpdate: DataManager key '${key}' got updated. Changes: '${changes.join(", ")}'`);
if (!oldData) {
logger("debug", `Event dataUpdate: DataManager key '${key}' got exported. All values are included in newData.`);
}

// Emit event
this.events.emit("dataUpdate", key, oldData, newData);
Expand Down
2 changes: 1 addition & 1 deletion src/data/fileStructure.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
{
"path": "src/controller/events/dataUpdate.js",
"url": "https://raw.githubusercontent.com/3urobeat/steam-comment-service-bot/beta-testing/src/controller/events/dataUpdate.js",
"checksum": "de3cb93006f690fc2060420a91056f17"
"checksum": "af70671e6f4c5caa5156bb05f048bafa"
},
{
"path": "src/controller/events/ready.js",
Expand Down

0 comments on commit e91c731

Please sign in to comment.