-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added setMany() functionality #74
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,6 +208,60 @@ exports.set = function(key, json, callback) { | |
], callback); | ||
}; | ||
|
||
/** | ||
* @summary Write multiple sets of user data | ||
* @function | ||
* @public | ||
* | ||
* @param {Object} keys | ||
* @param {Function} callback - callback (error, done) | ||
* | ||
* @example | ||
* const storage = require('electron-json-storage'); | ||
* | ||
* storage.setMany({ foo: 'bar', bar: 'foo' }, function(error, done) { | ||
* if (error) { | ||
* console.log('Error saving ' + error.key); | ||
* throw error.error; | ||
* } | ||
* if (finished) console.log('finished'); | ||
* }); | ||
*/ | ||
exports.setMany = function(keys, callback) { | ||
if (!keys || keys.constructor !== Object) { | ||
callback(new Error('Invalid keys'), true); | ||
return; | ||
} | ||
|
||
const entries = _.map(keys, function(value, key) { | ||
return { key: key, value: value }; | ||
}); | ||
|
||
function process() { | ||
const entry = entries.shift(); | ||
const key = entry.key; | ||
const value = entry.value; | ||
|
||
exports.set(key, value, function(error) { | ||
if (error) { | ||
callback({ error: error, key: key }, entries.length === 0); | ||
} | ||
|
||
if (entries.length > 0) { | ||
process(); | ||
} else if (!error) { | ||
callback(null, true); | ||
} | ||
}); | ||
} | ||
|
||
if (entries.length > 0) { | ||
process(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove a lot of boilerplate by reusing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I replied on #73 about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fair enough. The current approach looks good then :) |
||
} else { | ||
callback(null, true); | ||
} | ||
}; | ||
|
||
/** | ||
* @summary Check if a key exists | ||
* @function | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be good if instead of simply returning if things were successful or not, we would return the list of properties that failed (or the ones that succeeded), so in the case of an error, the user knows which ones to retry?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was originally what I had planned. With the way I ended up doing it, the user will still know which ones to retry (it will set the keys one by one, and every time there is an error it will invoke the callback which is why the callback takes a second parameter
finished
to let the user know when all the keys have been set).It's up to you. If we did it your way, would we simply return a list of failed keys (
['foo', 'bar']
) or the errors that they threw too ({ foo: error1, bar: error2 }
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I think we should return a list of failed keys along with their respective errors, and only call the final callback once.