Update user file without reading, debounce all saves

This commit is contained in:
Pavel Djundik 2019-12-15 17:26:18 +02:00
parent def56dc694
commit f269ac3bee
3 changed files with 23 additions and 51 deletions

View file

@ -315,29 +315,23 @@ Client.prototype.updateSession = function(token, ip, request) {
agent: friendlyAgent,
});
client.manager.updateUser(client.name, {
browser: client.config.browser,
sessions: client.config.sessions,
});
client.save();
};
Client.prototype.setPassword = function(hash, callback) {
const client = this;
client.manager.updateUser(
client.name,
{
password: hash,
},
function(err) {
if (err) {
return callback(false);
}
client.config.password = hash;
return callback(true);
const oldHash = client.config.password;
client.config.password = hash;
client.manager.saveUser(client, function(err) {
if (err) {
// If user file fails to write, reset it back
client.config.password = oldHash;
return callback(false);
}
);
return callback(true);
});
};
Client.prototype.input = function(data) {
@ -662,9 +656,7 @@ Client.prototype.registerPushSubscription = function(session, subscription, noSa
session.pushSubscription = data;
if (!noSave) {
this.manager.updateUser(this.name, {
sessions: this.config.sessions,
});
this.save();
}
return data;
@ -672,9 +664,7 @@ Client.prototype.registerPushSubscription = function(session, subscription, noSa
Client.prototype.unregisterPushSubscription = function(token) {
this.config.sessions[token].pushSubscription = null;
this.manager.updateUser(this.name, {
sessions: this.config.sessions,
});
this.save();
};
Client.prototype.save = _.debounce(
@ -684,9 +674,7 @@ Client.prototype.save = _.debounce(
}
const client = this;
const json = {};
json.networks = this.networks.map((n) => n.export());
client.manager.updateUser(client.name, json);
client.manager.saveUser(client);
},
1000,
{maxWait: 10000}

View file

@ -179,25 +179,13 @@ ClientManager.prototype.addUser = function(name, password, enableLog) {
return true;
};
ClientManager.prototype.updateUser = function(name, opts, callback) {
const user = readUserConfig(name);
ClientManager.prototype.saveUser = function(client, callback) {
const json = Object.assign({}, client.config, {
networks: client.networks.map((n) => n.export()),
});
const newUser = JSON.stringify(json, null, "\t");
if (!user) {
return callback ? callback(true) : false;
}
const currentUser = JSON.stringify(user, null, "\t");
_.assign(user, opts);
const newUser = JSON.stringify(user, null, "\t");
// Do not touch the disk if object has not changed
if (currentUser === newUser) {
console.log("same");
return callback ? callback() : true;
}
const pathReal = Helper.getUserConfigPath(name);
const pathReal = Helper.getUserConfigPath(client.name);
const pathTemp = pathReal + ".tmp";
try {
@ -208,7 +196,7 @@ ClientManager.prototype.updateUser = function(name, opts, callback) {
return callback ? callback() : true;
} catch (e) {
log.error(`Failed to update user ${colors.green(name)} (${e})`);
log.error(`Failed to update user ${colors.green(client.name)} (${e})`);
if (callback) {
callback(e);

View file

@ -591,9 +591,7 @@ function initializeClient(socket, client, token, lastMessage, openChannel) {
value: newSetting.value,
});
client.manager.updateUser(client.name, {
clientSettings: client.config.clientSettings,
});
client.save();
if (newSetting.name === "highlights") {
client.compileCustomHighlights();
@ -630,9 +628,7 @@ function initializeClient(socket, client, token, lastMessage, openChannel) {
delete client.config.sessions[tokenToSignOut];
client.manager.updateUser(client.name, {
sessions: client.config.sessions,
});
client.save();
_.map(client.attachedClients, (attachedClient, socketId) => {
if (attachedClient.token !== tokenToSignOut) {