Last active
August 26, 2018 07:42
-
-
Save datvtwkm/dd8c9c46bd47c179c932bc8eabfaba1f to your computer and use it in GitHub Desktop.
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
import { | |
call, | |
put, | |
take, | |
takeLatest, | |
race, | |
select | |
} from 'redux-saga/effects'; | |
import { | |
getContainer, | |
removeContainer | |
} from "../utils/global-container"; | |
import api from './api'; | |
import actions from '../actions' | |
const { | |
start, cancel, succeed, fail | |
} = actions; | |
function* fetchSomeThing() { | |
const container = getContainer('callingContainer'); | |
if (container && container.name === 'callingContainer') { | |
container.setState(()=>({ | |
loading: true, | |
result: '' | |
})) | |
} | |
try { | |
yield call(api); | |
yield put(succeed()); | |
if (container) { | |
container.setState({ | |
loading: false, | |
result: 'success' | |
}) | |
} | |
} catch (error) { | |
yield put(fail()); | |
if (container) { | |
container.setState({ | |
loading: false, | |
result: 'fail', | |
error | |
}) | |
} | |
} finally { | |
removeContainer('callingContainer') | |
} | |
} | |
const fetchSomeThingSaga = function* () { | |
yield takeLatest([start], function* (...args) { | |
yield race({ | |
task: call(fetchSomeThing, ...args), | |
cancel: take(cancel) | |
}); | |
}); | |
}; | |
export default fetchSomeThingSaga; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment