diff --git a/src/client.js b/src/client.js index 909824f5..69a87c70 100644 --- a/src/client.js +++ b/src/client.js @@ -132,6 +132,8 @@ function Client(manager, name, config = {}) { delay += 1000 + Math.floor(Math.random() * 1000); }); + + client.fileHash = manager.getDataToSave(client).newHash; } } @@ -679,6 +681,6 @@ Client.prototype.save = _.debounce( const client = this; client.manager.saveUser(client); }, - 1000, - {maxWait: 10000} + 5000, + {maxWait: 20000} ); diff --git a/src/clientManager.js b/src/clientManager.js index dfe8a673..df774451 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -3,6 +3,7 @@ const _ = require("lodash"); const log = require("./log"); const colors = require("chalk"); +const crypto = require("crypto"); const fs = require("fs"); const path = require("path"); const Client = require("./client"); @@ -175,11 +176,26 @@ ClientManager.prototype.addUser = function(name, password, enableLog) { return true; }; -ClientManager.prototype.saveUser = function(client, callback) { +ClientManager.prototype.getDataToSave = function(client) { const json = Object.assign({}, client.config, { networks: client.networks.map((n) => n.export()), }); const newUser = JSON.stringify(json, null, "\t"); + const newHash = crypto + .createHash("sha256") + .update(newUser) + .digest("hex"); + + return {newUser, newHash}; +}; + +ClientManager.prototype.saveUser = function(client, callback) { + const {newUser, newHash} = this.getDataToSave(client); + + // Do not write to disk if the exported data hasn't actually changed + if (client.fileHash === newHash) { + return; + } const pathReal = Helper.getUserConfigPath(client.name); const pathTemp = pathReal + ".tmp"; diff --git a/test/tests/customhighlights.js b/test/tests/customhighlights.js index 9d4d37d8..a151fef8 100644 --- a/test/tests/customhighlights.js +++ b/test/tests/customhighlights.js @@ -13,6 +13,12 @@ describe("Custom highlights", function() { const client = new Client( { clients: [], + getDataToSave() { + return { + newUser: "", + newHash: "", + }; + }, }, "test", {