Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------COMMUNICATING WITH REDUX STORE----------------------
- // put(action) : Instructs the middleware to dispatch an action to the Store
- // Ex- yield put({ type: 'ADD', payload: result });
- // take(pattern): the middleware to wait for a specified action on the Store.
- //The Generator is suspended until an action that matches pattern is dispatched.
- // Ex-
- /*
- Will be dispatched from the UI
- dispacth({
- type: 'FETCH_REQUEST',
- url: }
- );
- function* watchFetchRequests() {
- while(true) {
- //watch for the dispatched action FETCH_REQUEST
- const action = yield take('FETCH_REQUEST');
- //the parent process will wait until the 'forked' process is finished
- yield fork(fetchUrl, action.url);
- }
- }
- // The worker: perform the requested task
- function* fetchUrl(url) {
- const data = yield call(fetch, url);
- // data would be
- { type: CALL, function: fetchUrl, args: [url] }.
- yield put({
- type: 'FETCH_SUCCESS',
- data
- });
- }
- */
- //------------------------------------------------------------------------------------------
- //------------------COMMUNICATING WITH API(s)----------------------
- // call(fn, ...args): Instructs the middleware to call the function fn with args as arguments.
- // Ex- yield call(fetch, '/increment-add-calls', { method: 'post' });
- //apply(): Alias for call
- //-------------------------------------------------------------------------------------------
- //------------------COMMUNICATING WITH OTHER SAGA(s)----------------------
- // fork(fn, ...args): Creates an Effect description that instructs the middleware to perform a call on fn
- // Ex: ref 22
- //spawn(fn, ...args): Same as fork(fn, ...args) but creates a detached task. A detached task remains independent from its parent and acts like a top-level task.
- /*Ex:
- function* rootSaga() { |
- // Returns immediately with a Task object |
- const task = yield spawn(serverHello, 'world'); | const result0 = yield call(serverHello, 'world');
- // Perform an effect in the meantime //This wont be performed in the meantime.
- yield call(console.log, "waiting on server result..."); | yield call(console.log, "waiting on server result...");
- // Block on the result of serverHello
- const result = yield join(task);
- // Use the result of serverHello
- yield call(console.log, result);
- }
- */
- //join(...tasks): Creates an Effect description that instructs the middleware to wait for the results of previously forked tasks.
- //Ex: ref 67
- //-------------------------------------------------------------------------------------------
- //------------------SAGA(s) Helpers----------------------
- //takeEvery(pattern, saga, ...args): allows concurrent actions to be handled
- //Ex: import { takeEvery } from `redux-saga/effects`
- // function* fetchUser(action) {
- // ...
- // }
- // function* watchFetchUser() {
- // yield takeEvery('USER_REQUESTED', fetchUser)
- // }
- //internal code
- /*
- function* takeEvery(pattern, saga, ...args) {
- const task = yield fork(function* () {
- while (true) {
- const action = yield take(pattern)
- yield fork(saga, ...args.concat(action))
- }
- })
- return task
- }
- */
- //There is no guarantee that the tasks will termiate in the same order they were started
- //takeLatest:
- //takeLatest(pattern, saga, ...args): Spawns a saga on each action dispatched to the Store that matches pattern.
- //And automatically cancels any previous saga task started previous if it's still running.
- //Ex: import { takeEvery } from `redux-saga/effects`
- // function* fetchUser(action) {
- // ...
- // }
- // function* watchFetchUser() {
- // yield takeEvery('USER_REQUESTED', fetchUser)
- // }
- // Since takeLatest cancels any pending task started previously,
- //we ensure that if a user triggers multiple consecutive USER_REQUESTED actions rapidly,
- //we'll only conclude with the latest action
- //internal code
- /*
- function* takeLatest(pattern, saga, ...args) {
- const task = yield fork(function* () {
- let lastTask
- while (true) {
- const action = yield take(pattern)
- if (lastTask)
- yield cancel(lastTask) // cancel is no-op if the task has already terminated
- lastTask = yield fork(saga, ...args.concat(action))
- }
- })
- return task
- }
- */
- // Much safer and reliable than takeEvery
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement