Do not write to disk if the json data hasn't actually changed
This commit is contained in:
parent
2365c9489e
commit
6091514630
@ -132,6 +132,8 @@ function Client(manager, name, config = {}) {
|
|||||||
|
|
||||||
delay += 1000 + Math.floor(Math.random() * 1000);
|
delay += 1000 + Math.floor(Math.random() * 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.fileHash = manager.getDataToSave(client).newHash;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -679,6 +681,6 @@ Client.prototype.save = _.debounce(
|
|||||||
const client = this;
|
const client = this;
|
||||||
client.manager.saveUser(client);
|
client.manager.saveUser(client);
|
||||||
},
|
},
|
||||||
1000,
|
5000,
|
||||||
{maxWait: 10000}
|
{maxWait: 20000}
|
||||||
);
|
);
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
const _ = require("lodash");
|
const _ = require("lodash");
|
||||||
const log = require("./log");
|
const log = require("./log");
|
||||||
const colors = require("chalk");
|
const colors = require("chalk");
|
||||||
|
const crypto = require("crypto");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const Client = require("./client");
|
const Client = require("./client");
|
||||||
@ -175,11 +176,26 @@ ClientManager.prototype.addUser = function(name, password, enableLog) {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
ClientManager.prototype.saveUser = function(client, callback) {
|
ClientManager.prototype.getDataToSave = function(client) {
|
||||||
const json = Object.assign({}, client.config, {
|
const json = Object.assign({}, client.config, {
|
||||||
networks: client.networks.map((n) => n.export()),
|
networks: client.networks.map((n) => n.export()),
|
||||||
});
|
});
|
||||||
const newUser = JSON.stringify(json, null, "\t");
|
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 pathReal = Helper.getUserConfigPath(client.name);
|
||||||
const pathTemp = pathReal + ".tmp";
|
const pathTemp = pathReal + ".tmp";
|
||||||
|
@ -13,6 +13,12 @@ describe("Custom highlights", function() {
|
|||||||
const client = new Client(
|
const client = new Client(
|
||||||
{
|
{
|
||||||
clients: [],
|
clients: [],
|
||||||
|
getDataToSave() {
|
||||||
|
return {
|
||||||
|
newUser: "",
|
||||||
|
newHash: "",
|
||||||
|
};
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"test",
|
"test",
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user