Do not write to disk if the json data hasn't actually changed

This commit is contained in:
Pavel Djundik 2019-12-18 11:06:20 +02:00
parent 2365c9489e
commit 6091514630
3 changed files with 27 additions and 3 deletions

View File

@ -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}
);

View File

@ -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";

View File

@ -13,6 +13,12 @@ describe("Custom highlights", function() {
const client = new Client(
{
clients: [],
getDataToSave() {
return {
newUser: "",
newHash: "",
};
},
},
"test",
{