From 609151463044f95ef1c7d9f003ef62b24169183b Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 18 Dec 2019 11:06:20 +0200 Subject: [PATCH] Do not write to disk if the json data hasn't actually changed --- src/client.js | 6 ++++-- src/clientManager.js | 18 +++++++++++++++++- test/tests/customhighlights.js | 6 ++++++ 3 files changed, 27 insertions(+), 3 deletions(-) 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", {