add callbacks to 'doGetSync'

This commit is contained in:
Sean Yesmunt 2019-10-14 23:07:41 -04:00
parent 699dc63459
commit e31a50478e
3 changed files with 67 additions and 59 deletions

41
dist/bundle.es.js vendored
View file

@ -2404,7 +2404,17 @@ function doSetSync(oldHash, newHash, data) {
});
};
}
function doGetSync(password = '', shouldSetDefaultAccount) {
function doGetSync(password = '', callback) {
function handleCallback() {
if (callback) {
if (typeof callback !== 'function') {
throw new Error('Second argument passed to "doGetSync" must be a function');
}
callback();
}
}
return dispatch => {
dispatch({
type: GET_SYNC_STARTED
@ -2419,7 +2429,7 @@ function doGetSync(password = '', shouldSetDefaultAccount) {
data.syncData = response.data;
data.hasSyncedWallet = true;
if (response.changed || shouldSetDefaultAccount) {
if (response.changed) {
return lbryRedux.Lbry.sync_apply({
password,
data: response.data
@ -2432,23 +2442,10 @@ function doGetSync(password = '', shouldSetDefaultAccount) {
data
});
if (walletHash !== syncHash || shouldSetDefaultAccount) {
if (walletHash !== syncHash) {
// different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData));
if (shouldSetDefaultAccount) {
dispatch(doSetDefaultAccount(() => {
lbryRedux.Lbry.status().then(status => {
if (status.wallet.is_locked) {
lbryRedux.Lbry.account_unlock({
password
});
}
dispatch(lbryRedux.doFetchChannelListMine());
});
}));
}
handleCallback();
}
});
}
@ -2457,14 +2454,17 @@ function doGetSync(password = '', shouldSetDefaultAccount) {
type: GET_SYNC_COMPLETED,
data
});
handleCallback();
}).catch(() => {
if (data.hasSyncedWallet) {
const error = 'Error getting synced wallet';
dispatch({
type: GET_SYNC_FAILED,
data: {
error: 'Error getting synced wallet'
error
}
});
handleCallback(error);
} else {
// user doesn't have a synced wallet
dispatch({
@ -2481,7 +2481,10 @@ function doGetSync(password = '', shouldSetDefaultAccount) {
}).then(({
hash: walletHash,
data: syncApplyData
}) => dispatch(doSetSync('', walletHash, syncApplyData, password)));
}) => {
dispatch(doSetSync('', walletHash, syncApplyData, password));
handleCallback();
});
}
});
});

40
dist/bundle.js vendored
View file

@ -3969,7 +3969,18 @@ function doSetSync(oldHash, newHash, data) {
}
function doGetSync() {
var password = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var shouldSetDefaultAccount = arguments.length > 1 ? arguments[1] : undefined;
var callback = arguments.length > 1 ? arguments[1] : undefined;
function handleCallback() {
if (callback) {
if (typeof callback !== 'function') {
throw new Error('Second argument passed to "doGetSync" must be a function');
}
callback();
}
}
return function (dispatch) {
dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_STARTED"]
@ -3984,7 +3995,7 @@ function doGetSync() {
data.syncData = response.data;
data.hasSyncedWallet = true;
if (response.changed || shouldSetDefaultAccount) {
if (response.changed) {
return lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].sync_apply({
password: password,
data: response.data
@ -3996,23 +4007,10 @@ function doGetSync() {
data: data
});
if (walletHash !== syncHash || shouldSetDefaultAccount) {
if (walletHash !== syncHash) {
// different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData));
if (shouldSetDefaultAccount) {
dispatch(doSetDefaultAccount(function () {
lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].status().then(function (status) {
if (status.wallet.is_locked) {
lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].account_unlock({
password: password
});
}
dispatch(Object(lbry_redux__WEBPACK_IMPORTED_MODULE_2__["doFetchChannelListMine"])());
});
}));
}
handleCallback();
}
});
}
@ -4021,14 +4019,17 @@ function doGetSync() {
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_COMPLETED"],
data: data
});
handleCallback();
})["catch"](function () {
if (data.hasSyncedWallet) {
var error = 'Error getting synced wallet';
dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_FAILED"],
data: {
error: 'Error getting synced wallet'
error: error
}
});
handleCallback(error);
} else {
// user doesn't have a synced wallet
dispatch({
@ -4045,7 +4046,8 @@ function doGetSync() {
}).then(function (_ref2) {
var walletHash = _ref2.hash,
syncApplyData = _ref2.data;
return dispatch(doSetSync('', walletHash, syncApplyData, password));
dispatch(doSetSync('', walletHash, syncApplyData, password));
handleCallback();
});
}
});

View file

@ -1,6 +1,6 @@
import * as ACTIONS from 'constants/action_types';
import Lbryio from 'lbryio';
import { Lbry, doFetchChannelListMine } from 'lbry-redux';
import { Lbry } from 'lbry-redux';
export function doSetDefaultAccount(success, failure) {
return dispatch => {
@ -77,7 +77,17 @@ export function doSetSync(oldHash, newHash, data) {
};
}
export function doGetSync(password = '', shouldSetDefaultAccount) {
export function doGetSync(password = '', callback) {
function handleCallback() {
if (callback) {
if (typeof callback !== 'function') {
throw new Error('Second argument passed to "doGetSync" must be a function');
}
callback();
}
}
return dispatch => {
dispatch({
type: ACTIONS.GET_SYNC_STARTED,
@ -91,42 +101,34 @@ export function doGetSync(password = '', shouldSetDefaultAccount) {
data.syncHash = syncHash;
data.syncData = response.data;
data.hasSyncedWallet = true;
if (response.changed || shouldSetDefaultAccount) {
if (response.changed) {
return Lbry.sync_apply({ password, data: response.data }).then(
({ hash: walletHash, data: walletData }) => {
dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data });
if (walletHash !== syncHash || shouldSetDefaultAccount) {
if (walletHash !== syncHash) {
// different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData));
if (shouldSetDefaultAccount) {
dispatch(
doSetDefaultAccount(() => {
Lbry.status().then(status => {
if (status.wallet.is_locked) {
Lbry.account_unlock({ password });
}
dispatch(doFetchChannelListMine());
});
})
);
}
handleCallback();
}
}
);
}
dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data });
handleCallback();
})
.catch(() => {
if (data.hasSyncedWallet) {
const error = 'Error getting synced wallet';
dispatch({
type: ACTIONS.GET_SYNC_FAILED,
data: {
error: 'Error getting synced wallet',
error,
},
});
handleCallback(error);
} else {
// user doesn't have a synced wallet
dispatch({
@ -136,9 +138,10 @@ export function doGetSync(password = '', shouldSetDefaultAccount) {
// call sync_apply to get data to sync
// first time sync. use any string for old hash
Lbry.sync_apply({ password }).then(({ hash: walletHash, data: syncApplyData }) =>
dispatch(doSetSync('', walletHash, syncApplyData, password))
);
Lbry.sync_apply({ password }).then(({ hash: walletHash, data: syncApplyData }) => {
dispatch(doSetSync('', walletHash, syncApplyData, password));
handleCallback();
});
}
});
});