From 8344379bfd4ea17a5185c6ef7827a9599b1b6567 Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Thu, 12 Nov 2020 13:21:13 -0500 Subject: [PATCH] add fatal error sync action --- dist/bundle.es.js | 80 ++++++++++++++++----------- src/constants/action_types.js | 1 + src/redux/actions/sync.js | 82 ++++++++++++++++------------ src/redux/middleware/shared-state.js | 2 +- 4 files changed, 96 insertions(+), 69 deletions(-) diff --git a/dist/bundle.es.js b/dist/bundle.es.js index 071869d..33e7afd 100644 --- a/dist/bundle.es.js +++ b/dist/bundle.es.js @@ -289,6 +289,7 @@ const FETCH_COST_INFO_FAILED = 'FETCH_COST_INFO_FAILED'; // Sync const USER_STATE_POPULATE = 'USER_STATE_POPULATE'; +const SYNC_FATAL_ERROR = 'SYNC_FATAL_ERROR'; var action_types = /*#__PURE__*/Object.freeze({ WINDOW_FOCUSED: WINDOW_FOCUSED, @@ -529,7 +530,8 @@ var action_types = /*#__PURE__*/Object.freeze({ FETCH_COST_INFO_STARTED: FETCH_COST_INFO_STARTED, FETCH_COST_INFO_COMPLETED: FETCH_COST_INFO_COMPLETED, FETCH_COST_INFO_FAILED: FETCH_COST_INFO_FAILED, - USER_STATE_POPULATE: USER_STATE_POPULATE + USER_STATE_POPULATE: USER_STATE_POPULATE, + SYNC_FATAL_ERROR: SYNC_FATAL_ERROR }); const CC_LICENSES = [{ @@ -1754,43 +1756,55 @@ function doPopulateSharedUserState(sharedSettings) { } function doPreferenceSet(key, value, version, success, fail) { - const preference = { - type: typeof value, - version, - value - }; + return dispatch => { + const preference = { + type: typeof value, + version, + value + }; - const options = { - key, - value: JSON.stringify(preference) - }; + const options = { + key, + value: JSON.stringify(preference) + }; - lbryProxy.preference_set(options).then(() => { - success(preference); - }).catch(() => { - if (fail) { - fail(); - } - }); + lbryProxy.preference_set(options).then(() => { + success(preference); + }).catch(() => { + dispatch({ + type: SYNC_FATAL_ERROR + }); + + if (fail) { + fail(); + } + }); + }; } function doPreferenceGet(key, success, fail) { - const options = { - key + return dispatch => { + const options = { + key + }; + + return lbryProxy.preference_get(options).then(result => { + if (result) { + const preference = result[key]; + return success(preference); + } + + return success(null); + }).catch(err => { + dispatch({ + type: SYNC_FATAL_ERROR + }); + + if (fail) { + fail(err); + } + }); }; - - return lbryProxy.preference_get(options).then(result => { - if (result) { - const preference = result[key]; - return success(preference); - } - - return success(null); - }).catch(err => { - if (fail) { - fail(err); - } - }); } // @@ -1831,7 +1845,7 @@ const buildSharedStateMiddleware = (actions, sharedStateFilters, sharedStateCb) if (!isEqual(oldShared, shared)) { // only update if the preference changed from last call in the same session oldShared = shared; - doPreferenceSet(preferenceKey, shared, SHARED_PREFERENCE_VERSION); + dispatch(doPreferenceSet(preferenceKey, shared, SHARED_PREFERENCE_VERSION)); } if (sharedStateCb) { diff --git a/src/constants/action_types.js b/src/constants/action_types.js index a8f88d9..afc2245 100644 --- a/src/constants/action_types.js +++ b/src/constants/action_types.js @@ -268,3 +268,4 @@ export const FETCH_COST_INFO_FAILED = 'FETCH_COST_INFO_FAILED'; // Sync export const USER_STATE_POPULATE = 'USER_STATE_POPULATE'; +export const SYNC_FATAL_ERROR = 'SYNC_FATAL_ERROR'; diff --git a/src/redux/actions/sync.js b/src/redux/actions/sync.js index 43b8a82..c5c0889 100644 --- a/src/redux/actions/sync.js +++ b/src/redux/actions/sync.js @@ -74,45 +74,57 @@ export function doPreferenceSet( success: Function, fail: Function ) { - const preference = { - type: typeof value, - version, - value, - }; + return (dispatch: Dispatch) => { + const preference = { + type: typeof value, + version, + value, + }; - const options = { - key, - value: JSON.stringify(preference), - }; + const options = { + key, + value: JSON.stringify(preference), + }; - Lbry.preference_set(options) - .then(() => { - success(preference); - }) - .catch(() => { - if (fail) { - fail(); - } - }); + Lbry.preference_set(options) + .then(() => { + success(preference); + }) + .catch(() => { + dispatch({ + type: ACTIONS.SYNC_FATAL_ERROR, + }); + + if (fail) { + fail(); + } + }); + }; } export function doPreferenceGet(key: string, success: Function, fail?: Function) { - const options = { - key, + return (dispatch: Dispatch) => { + const options = { + key, + }; + + return Lbry.preference_get(options) + .then(result => { + if (result) { + const preference = result[key]; + return success(preference); + } + + return success(null); + }) + .catch(err => { + dispatch({ + type: ACTIONS.SYNC_FATAL_ERROR, + }); + + if (fail) { + fail(err); + } + }); }; - - return Lbry.preference_get(options) - .then(result => { - if (result) { - const preference = result[key]; - return success(preference); - } - - return success(null); - }) - .catch(err => { - if (fail) { - fail(err); - } - }); } diff --git a/src/redux/middleware/shared-state.js b/src/redux/middleware/shared-state.js index cb3a28d..e4a8c4f 100644 --- a/src/redux/middleware/shared-state.js +++ b/src/redux/middleware/shared-state.js @@ -49,7 +49,7 @@ export const buildSharedStateMiddleware = ( if (!isEqual(oldShared, shared)) { // only update if the preference changed from last call in the same session oldShared = shared; - doPreferenceSet(preferenceKey, shared, SHARED_PREFERENCE_VERSION); + dispatch(doPreferenceSet(preferenceKey, shared, SHARED_PREFERENCE_VERSION)); } if (sharedStateCb) {