Skip to content

Instantly share code, notes, and snippets.

@cooperka
Last active December 9, 2018 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cooperka/9a17491b874886c6f4de1a4fc4d70b15 to your computer and use it in GitHub Desktop.
Save cooperka/9a17491b874886c6f4de1a4fc4d70b15 to your computer and use it in GitHub Desktop.
This is why I love redux-saga + ES6 generators.
/**
* Root sagas can be exported from every file, and are all bundled together in one master saga.
* This saga listens for requests to sync, as well as failure events.
*/
export default function* rootSaga() {
yield fork(takeLatest, cacheManagerActionTypes.SYNC_ALL, startSync);
yield fork(takeLatest, cacheManagerActionTypes.SYNC_FAILED, showSyncFailed);
}
/**
* Sync everything (download/upload from the server), but halt if a SYNC_FAILED event occurs.
*
* `race` starts multiple effects at once and stops everything else as soon as one completes.
* The object keys here ('sync' and 'cancelSync') are not being used.
*/
function* startSync() {
yield race({
sync: call(syncAll),
cancelSync: take(cacheManagerActionTypes.SYNC_FAILED),
});
}
/**
* Sync everything, showing status messages to the user as it progresses.
*
* `select` gets a value from the global state;
* `call` calls a function;
* `put` dispatches an event;
* `take` waits until an event occurs;
* `fork` calls another saga asynchronously.
*/
function* syncAll() {
const { syncInProgress } = yield select((state) => ({
syncInProgress: state.sync.syncInProgress,
}));
if (syncInProgress) return;
try {
yield call(Snackbar.show, 'Downloading...');
yield put(CacheManagerActions.syncStarted());
yield fork(syncWorkingState);
// Wait until all downloads are done.
yield take(cacheManagerActionTypes.SYNC_FINISHED);
yield fork(syncUrlCache);
yield call(Snackbar.show, 'Uploading...');
yield put(CacheManagerActions.syncStarted());
yield fork(syncCheckins);
yield fork(syncCompletions);
yield fork(syncAttachments);
// Wait until all uploads are done.
yield take(cacheManagerActionTypes.SYNC_FINISHED);
yield call(Snackbar.show, 'Sync successful!');
} catch (error) {
yield put(CacheManagerActions.syncFailed());
}
}
/**
* Show an error message to the user, and clean up.
*/
function* showSyncFailed() {
yield call(Snackbar.show, 'Sync failed!');
yield put(CacheManagerActions.syncFinished());
}
import { all, call } from 'redux-saga/effects';
import syncStatusWorkflow from './SyncStatus/workflow';
/* ... other workflow imports ... */
export default function* rootWorkflow() {
yield all([
call(syncStatusWorkflow),
call(navigationWorkflow),
/* ... other workflows ... */
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment