-
-
Save clarketm/2d74187b1842bb09a7575a9c3158187e to your computer and use it in GitHub Desktop.
redux-saga alternative with async/await?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @providesModule Effects | |
* @flow | |
*/ | |
import { Linking } from 'react-native'; | |
import AppDataApi from 'AppDataApi'; | |
import Actions from 'Actions'; | |
import ActionTypes from 'ActionTypes'; | |
import LocalStorage from 'LocalStorage'; | |
type EffectParams = { | |
action: Object, | |
dispatch: (action: any) => void, | |
getState: () => any, | |
nextDispatchAsync: (actionType: string) => Promise<Object>, | |
}; | |
type EffectErrorHandlerParams = { | |
action: Object, | |
error: Object, | |
}; | |
async function watchOpenApp({action, dispatch, getState, nextDispatchAsync}: EffectParams) { | |
let { app } = action; | |
if (typeof app === 'string') { | |
app = await AppDataApi.fetchAppDataAsync(app); | |
} | |
dispatch(Actions.addAppToHistory(app)); | |
const { history } = getState(); | |
await LocalStorage.saveHistoryAsync(history); | |
const url = `exp://rnplay.org/apps/${app.url_token}`; | |
await Linking.openURL(url); | |
await AppDataApi.incrementViewCountAsync(app.url_token); | |
} | |
function genericErrorHandler({action, error}: EffectErrorHandlerParams) { | |
console.log({error, action}); | |
} | |
export default [ | |
{action: ActionTypes.OPEN_APP, effect: watchOpenApp, errorHandler: genericErrorHandler}, | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment