I do think you could make it a little simpler by creating a new “Blockstack” object via
const blockstack = new Blockstack({
appConfig: { appDomain: myAppDomain },
sessionConfig: { authenticatorUrl: 'localhost:8700' }
});
and then just use some Object.assign
's to assign defaults, etc. (do note it’s not recursive so you’d have to do some for..in
stuff).
In addition, instead of passing the session within each action (I think that’s pretty complicated), here’s an example for using a callback instead:
// web / localstorage
const onSessionUpdate = (state, key, value) => {
if(!state) window.localStorage.clear();
else if(!key) Object.keys(state).forEach(k => window.localStorage.setItem(k, state[k]);
else if(!value) window.localStorage.removeItem(key);
else window.localstorage.setItem(key, value);
};
// custom rethinkdb wrapper
const onSessionUpdate = (state, key, value) => {
if(!state) db.run(stateTable.delete());
else if(!key) db.run(stateTable.replace(state));
else if(!value) db.run(stateTable.get(key).delete());
else {
const obj = {};
obj[key] = value;
db.run(stateTable.insert(obj, { conflict: 'update' }));
}
};
And then using them in the config…
const blockstack = new Blockstack({
appConfig: { appDomain: 'my-domain' },
sessionConfig: { updateCallback: onSessionUpdate }
});
But outside of those specifics, the biggest difference I see between the Original Proposal and Will’s would be the former uses the storage provider as something to backup and recover the state, whereas the latter uses it as a complete storage solution. I feel like the former is a little easier in regards to speed and ease-of-use (for the library at least), but the latter has better persistence in regards to crashing and unhandled closing of the application. You could also use the latter along with flux/redux/vuex instead of localstorage as well.
Regardless, with either being done there should be a default driver to use localstorage, and it would have to be overidden to use a custom storage backend (like localForage or a db).
Until either solution gets implemented though, here’s a quick fix for those who are running on node currently: