Add callbacks to getSync (#67)

Add callbacks to `getSync`
This commit is contained in:
Sean Yesmunt 2019-10-15 10:32:28 -04:00 committed by GitHub
commit aebad10a9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 72 deletions

47
dist/bundle.es.js vendored
View file

@ -2409,7 +2409,19 @@ function doSetSync(oldHash, newHash, data) {
});
};
}
function doGetSync(password = '', shouldSetDefaultAccount) {
function doGetSync(passedPassword, callback) {
const password = passedPassword === null || passedPassword === undefined ? '' : passedPassword;
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
@ -2424,7 +2436,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
@ -2437,39 +2449,29 @@ 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();
}
});
}
} else {
dispatch({
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({
@ -2486,7 +2488,10 @@ function doGetSync(password = '', shouldSetDefaultAccount) {
}).then(({
hash: walletHash,
data: syncApplyData
}) => dispatch(doSetSync('', walletHash, syncApplyData, password)));
}) => {
dispatch(doSetSync('', walletHash, syncApplyData, password));
handleCallback();
});
}
});
});

47
dist/bundle.js vendored
View file

@ -3974,9 +3974,19 @@ 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;
function doGetSync(passedPassword, callback) {
var password = passedPassword === null || passedPassword === undefined ? '' : passedPassword;
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"]
@ -3991,7 +4001,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
@ -4003,39 +4013,29 @@ 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();
}
});
}
} else {
dispatch({
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({
@ -4052,7 +4052,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,19 @@ export function doSetSync(oldHash, newHash, data) {
};
}
export function doGetSync(password = '', shouldSetDefaultAccount) {
export function doGetSync(passedPassword, callback) {
const password = passedPassword === null || passedPassword === undefined ? '' : passedPassword;
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 +103,33 @@ 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 +139,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();
});
}
});
});