Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lidando com Efeitos Colaterais com Redux Saga

Lidando com Efeitos Colaterais com Redux Saga

Filipe Costa

May 13, 2017
Tweet

More Decks by Filipe Costa

Other Decks in Programming

Transcript

  1. "Evolui ideias do Flux, mas evita suas complexidades pegando dicas

    do Elm" — github.com/reactjs/redux Redux
  2. "É uma função em que seus parâmetros são a única

    coisa que influenciam no seu valor de retorno"
  3. "É uma função em que seus parâmetros são a única

    coisa que influenciam no seu valor de retorno" — eu
  4. Redux UI Middleware Reducer Store Action dispatched (currentState, action) =>

    newState New State Action forwarded Action dispatched
  5. “…a sequence of transactions that can be interleaved with other

    transactions.” http://www.cs.cornell.edu/andru/cs711/2002fa/reading/sagas.pdf — Hector Garcia-Molina e Kenneth Salem
  6. “…a sequence of transactions that can be interleaved with other

    transactions.” http://www.cs.cornell.edu/andru/cs711/2002fa/reading/sagas.pdf — Hector Garcia-Molina e Kenneth Salem
  7. “…a saga is like a separate thread in your application

    that's solely responsible for side effects.” — https://github.com/redux-saga/redux-saga
  8. https://davidwalsh.name/es6-generators function* foo(x) { const y = 2 * (yield

    (x + 1)); const z = yield (y / 3); return (x + y + z); } const it = foo(5); // note: not sending anything into `next()` here console.log(it.next()); // { value:6, done:false } console.log(it.next(12)); // { value:8, done:false } console.log(it.next(13)); // { value:42, done:true }
  9. https://redux-saga.github.io/redux-saga/docs/basics/UsingSagaHelpers.html import { call, put } from 'redux-saga/effects'; export function*

    fetchData(action) { try { const data = yield call(Api.fetchUser, action.payload.url); yield put({type: 'FETCH_SUCCEEDED', data}); } catch (error) { yield put({type: 'FETCH_FAILED', error}); } } function* watchFetchData() { yield takeEvery('FETCH_REQUESTED', fetchData); }
  10. https://redux-saga.github.io/redux-saga/docs/basics/UsingSagaHelpers.html import { takeEvery } from 'redux-saga'; // FETCH_USERS function*

    fetchUsers(action) { ... } // CREATE_USER function* createUser(action) { ... } // use them in parallel export default function* rootSaga() { yield takeEvery('FETCH_USERS', fetchUsers); yield takeEvery('CREATE_USER', createUser); }
  11. import { call, put } from 'redux-saga/effects'; export function* fetchData(action)

    { try { const data = yield call(Api.fetchUser, action.payload.url); yield put({type: 'FETCH_SUCCEEDED', data}); } catch (error) { yield put({type: 'FETCH_FAILED', error}); } } function* watchFetchData() { yield takeEvery('FETCH_REQUESTED', fetchData); } https://redux-saga.github.io/redux-saga/docs/basics/UsingSagaHelpers.html
  12. import { call, put } from 'redux-saga/effects'; export function* fetchData(action)

    { try { const data = yield call(Api.fetchUser, action.payload.url); yield put({type: 'FETCH_SUCCEEDED', data}); } catch (error) { yield put({type: 'FETCH_FAILED', error}); } } function* watchFetchData() { yield takeEvery('FETCH_REQUESTED', fetchData); } https://redux-saga.github.io/redux-saga/docs/basics/UsingSagaHelpers.html
  13. const { posts, timeout } = yield race({ posts: call(fetch,

    '/posts'), timeout: call(delay, 1000) });
  14. function* foo() { ... } function* bar() { ... }

    function* baz() { ... } function* fooBarBaz() { yield* foo(); yield* bar(); yield* baz(); }
  15. function* foo() { ... } function* bar() { ... }

    function* baz() { ... } function* fooBarBaz() { yield call(foo); yield fork(bar); yield call(baz); }
  16. function* mySync() { try { while(true) { yield put({type: 'SYNC_STARTED'});

    const result = yield call(syncMyFiles); yield call(delay, 500); } } finally { if (yield cancelled()) yield put({ type: 'SYNC_STOPED'}); } } function* myDropbox() { while(yield take('START_FILE_SYNC')) { const mySyncTask = yield fork(mySync); yield take('STOP_FILE_SYNC'); // this will cause the forked task to jump into its finally block yield cancel(mySyncTask); } }