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

53
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 => { return dispatch => {
dispatch({ dispatch({
type: GET_SYNC_STARTED type: GET_SYNC_STARTED
@ -2424,7 +2436,7 @@ function doGetSync(password = '', shouldSetDefaultAccount) {
data.syncData = response.data; data.syncData = response.data;
data.hasSyncedWallet = true; data.hasSyncedWallet = true;
if (response.changed || shouldSetDefaultAccount) { if (response.changed) {
return lbryRedux.Lbry.sync_apply({ return lbryRedux.Lbry.sync_apply({
password, password,
data: response.data data: response.data
@ -2437,39 +2449,29 @@ function doGetSync(password = '', shouldSetDefaultAccount) {
data data
}); });
if (walletHash !== syncHash || shouldSetDefaultAccount) { if (walletHash !== syncHash) {
// different local hash, need to synchronise // different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData)); dispatch(doSetSync(syncHash, walletHash, walletData));
handleCallback();
if (shouldSetDefaultAccount) {
dispatch(doSetDefaultAccount(() => {
lbryRedux.Lbry.status().then(status => {
if (status.wallet.is_locked) {
lbryRedux.Lbry.account_unlock({
password
});
}
dispatch(lbryRedux.doFetchChannelListMine());
});
}));
}
} }
}); });
} else {
dispatch({
type: GET_SYNC_COMPLETED,
data
});
handleCallback();
} }
dispatch({
type: GET_SYNC_COMPLETED,
data
});
}).catch(() => { }).catch(() => {
if (data.hasSyncedWallet) { if (data.hasSyncedWallet) {
const error = 'Error getting synced wallet';
dispatch({ dispatch({
type: GET_SYNC_FAILED, type: GET_SYNC_FAILED,
data: { data: {
error: 'Error getting synced wallet' error
} }
}); });
handleCallback(error);
} else { } else {
// user doesn't have a synced wallet // user doesn't have a synced wallet
dispatch({ dispatch({
@ -2486,7 +2488,10 @@ function doGetSync(password = '', shouldSetDefaultAccount) {
}).then(({ }).then(({
hash: walletHash, hash: walletHash,
data: syncApplyData data: syncApplyData
}) => dispatch(doSetSync('', walletHash, syncApplyData, password))); }) => {
dispatch(doSetSync('', walletHash, syncApplyData, password));
handleCallback();
});
} }
}); });
}); });

53
dist/bundle.js vendored
View file

@ -3974,9 +3974,19 @@ function doSetSync(oldHash, newHash, data) {
}); });
}; };
} }
function doGetSync() { function doGetSync(passedPassword, callback) {
var password = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var password = passedPassword === null || passedPassword === undefined ? '' : passedPassword;
var shouldSetDefaultAccount = 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) { return function (dispatch) {
dispatch({ dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_STARTED"] type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_STARTED"]
@ -3991,7 +4001,7 @@ function doGetSync() {
data.syncData = response.data; data.syncData = response.data;
data.hasSyncedWallet = true; data.hasSyncedWallet = true;
if (response.changed || shouldSetDefaultAccount) { if (response.changed) {
return lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].sync_apply({ return lbry_redux__WEBPACK_IMPORTED_MODULE_2__["Lbry"].sync_apply({
password: password, password: password,
data: response.data data: response.data
@ -4003,39 +4013,29 @@ function doGetSync() {
data: data data: data
}); });
if (walletHash !== syncHash || shouldSetDefaultAccount) { if (walletHash !== syncHash) {
// different local hash, need to synchronise // different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData)); dispatch(doSetSync(syncHash, walletHash, walletData));
handleCallback();
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"])());
});
}));
}
} }
}); });
} else {
dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_COMPLETED"],
data: data
});
handleCallback();
} }
dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_COMPLETED"],
data: data
});
})["catch"](function () { })["catch"](function () {
if (data.hasSyncedWallet) { if (data.hasSyncedWallet) {
var error = 'Error getting synced wallet';
dispatch({ dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_FAILED"], type: constants_action_types__WEBPACK_IMPORTED_MODULE_0__["GET_SYNC_FAILED"],
data: { data: {
error: 'Error getting synced wallet' error: error
} }
}); });
handleCallback(error);
} else { } else {
// user doesn't have a synced wallet // user doesn't have a synced wallet
dispatch({ dispatch({
@ -4052,7 +4052,8 @@ function doGetSync() {
}).then(function (_ref2) { }).then(function (_ref2) {
var walletHash = _ref2.hash, var walletHash = _ref2.hash,
syncApplyData = _ref2.data; 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 * as ACTIONS from 'constants/action_types';
import Lbryio from 'lbryio'; import Lbryio from 'lbryio';
import { Lbry, doFetchChannelListMine } from 'lbry-redux'; import { Lbry } from 'lbry-redux';
export function doSetDefaultAccount(success, failure) { export function doSetDefaultAccount(success, failure) {
return dispatch => { 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 => { return dispatch => {
dispatch({ dispatch({
type: ACTIONS.GET_SYNC_STARTED, type: ACTIONS.GET_SYNC_STARTED,
@ -91,42 +103,33 @@ export function doGetSync(password = '', shouldSetDefaultAccount) {
data.syncHash = syncHash; data.syncHash = syncHash;
data.syncData = response.data; data.syncData = response.data;
data.hasSyncedWallet = true; data.hasSyncedWallet = true;
if (response.changed || shouldSetDefaultAccount) { if (response.changed) {
return Lbry.sync_apply({ password, data: response.data }).then( return Lbry.sync_apply({ password, data: response.data }).then(
({ hash: walletHash, data: walletData }) => { ({ hash: walletHash, data: walletData }) => {
dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data }); dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data });
if (walletHash !== syncHash || shouldSetDefaultAccount) { if (walletHash !== syncHash) {
// different local hash, need to synchronise // different local hash, need to synchronise
dispatch(doSetSync(syncHash, walletHash, walletData)); dispatch(doSetSync(syncHash, walletHash, walletData));
handleCallback();
if (shouldSetDefaultAccount) {
dispatch(
doSetDefaultAccount(() => {
Lbry.status().then(status => {
if (status.wallet.is_locked) {
Lbry.account_unlock({ password });
}
dispatch(doFetchChannelListMine());
});
})
);
}
} }
} }
); );
} }
dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data }); dispatch({ type: ACTIONS.GET_SYNC_COMPLETED, data });
handleCallback();
}) })
.catch(() => { .catch(() => {
if (data.hasSyncedWallet) { if (data.hasSyncedWallet) {
const error = 'Error getting synced wallet';
dispatch({ dispatch({
type: ACTIONS.GET_SYNC_FAILED, type: ACTIONS.GET_SYNC_FAILED,
data: { data: {
error: 'Error getting synced wallet', error,
}, },
}); });
handleCallback(error);
} else { } else {
// user doesn't have a synced wallet // user doesn't have a synced wallet
dispatch({ dispatch({
@ -136,9 +139,10 @@ export function doGetSync(password = '', shouldSetDefaultAccount) {
// call sync_apply to get data to sync // call sync_apply to get data to sync
// first time sync. use any string for old hash // first time sync. use any string for old hash
Lbry.sync_apply({ password }).then(({ hash: walletHash, data: syncApplyData }) => Lbry.sync_apply({ password }).then(({ hash: walletHash, data: syncApplyData }) => {
dispatch(doSetSync('', walletHash, syncApplyData, password)) dispatch(doSetSync('', walletHash, syncApplyData, password));
); handleCallback();
});
} }
}); });
}); });