use nthen to sequence async events

pull/1/head
ansuz 7 years ago
parent 9a7abe0b1e
commit 508521664f

@ -12,11 +12,12 @@ define([
'/common/common-feedback.js', '/common/common-feedback.js',
'/common/outer/local-store.js', '/common/outer/local-store.js',
'/customize/messages.js', '/customize/messages.js',
'/bower_components/nthen/index.js',
'/bower_components/tweetnacl/nacl-fast.min.js', '/bower_components/tweetnacl/nacl-fast.min.js',
'/bower_components/scrypt-async/scrypt-async.min.js', // better load speed '/bower_components/scrypt-async/scrypt-async.min.js', // better load speed
], function ($, Listmap, Crypto, Util, NetConfig, Cred, ChainPad, Realtime, Constants, UI, ], function ($, Listmap, Crypto, Util, NetConfig, Cred, ChainPad, Realtime, Constants, UI,
Feedback, LocalStore, Messages) { Feedback, LocalStore, Messages, nThen) {
var Exports = { var Exports = {
Cred: Cred, Cred: Cred,
}; };
@ -105,18 +106,24 @@ define([
return void cb('PASS_TOO_SHORT'); return void cb('PASS_TOO_SHORT');
} }
Cred.deriveFromPassphrase(uname, passwd, 192, function (bytes) {
// results... // results...
var res = { var res = {
register: isRegister, register: isRegister,
}; };
// run scrypt to derive the user's keys var RT;
var opt = res.opt = allocateBytes(bytes);
nThen(function (waitFor) {
Cred.deriveFromPassphrase(uname, passwd, 192, waitFor(function (bytes) {
// run scrypt to derive the user's keys
res.opt = allocateBytes(bytes);
}));
}).nThen(function (waitFor) {
var opt = res.opt;
// use the derived key to generate an object // use the derived key to generate an object
loadUserObject(opt, function (err, rt) { loadUserObject(opt, waitFor(function (err, rt) {
if (err) { return void cb(err); } if (err) { return void cb(err); }
RT = rt;
res.proxy = rt.proxy; res.proxy = rt.proxy;
res.realtime = rt.realtime; res.realtime = rt.realtime;
@ -136,12 +143,14 @@ define([
// they tried to just log in but there's no such user // they tried to just log in but there's no such user
if (!isRegister && isProxyEmpty(rt.proxy)) { if (!isRegister && isProxyEmpty(rt.proxy)) {
rt.network.disconnect(); // clean up after yourself rt.network.disconnect(); // clean up after yourself
waitFor.abort();
return void cb('NO_SUCH_USER', res); return void cb('NO_SUCH_USER', res);
} }
// they tried to register, but those exact credentials exist // they tried to register, but those exact credentials exist
if (isRegister && !isProxyEmpty(rt.proxy)) { if (isRegister && !isProxyEmpty(rt.proxy)) {
rt.network.disconnect(); rt.network.disconnect();
waitFor.abort();
return void cb('ALREADY_REGISTERED', res); return void cb('ALREADY_REGISTERED', res);
} }
@ -163,21 +172,21 @@ define([
if (shouldImport) { if (shouldImport) {
sessionStorage.migrateAnonDrive = 1; sessionStorage.migrateAnonDrive = 1;
} }
}));
}).nThen(function () {
// We have to call whenRealtimeSyncs asynchronously here because in the current // We have to call whenRealtimeSyncs asynchronously here because in the current
// version of listmap, onLocal calls `chainpad.contentUpdate(newValue)` // version of listmap, onLocal calls `chainpad.contentUpdate(newValue)`
// asynchronously. // asynchronously.
// The following setTimeout is here to make sure whenRealtimeSyncs is called after // The following setTimeout is here to make sure whenRealtimeSyncs is called after
// `contentUpdate` so that we have an update userDoc in chainpad. // `contentUpdate` so that we have an update userDoc in chainpad.
setTimeout(function () { setTimeout(function () {
Realtime.whenRealtimeSyncs(rt.realtime, function () { Realtime.whenRealtimeSyncs(RT.realtime, function () {
LocalStore.login(res.userHash, res.userName, function () { LocalStore.login(res.userHash, res.userName, function () {
setTimeout(function () { cb(void 0, res); }); setTimeout(function () { cb(void 0, res); });
}); });
}); });
}); });
}); });
});
}; };
Exports.redirect = function () { Exports.redirect = function () {
if (sessionStorage.redirectTo) { if (sessionStorage.redirectTo) {

Loading…
Cancel
Save