Declarative Effects

import { takeEvery } from "redux-saga/effects";
import Api from "./path/to/api";
 
function* watchFetchProducts() {
  yield takeEvery("PRODUCTS_REQUESTED", fetchProducts);
}
 
function* fetchProducts() {
  const products = yield Api.fetch("/products");
  console.log(products);
}
const iterator = fetchProducts()
assert.deepEqual(iterator.next().value, ??)
  1. iterator ์—๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋จ.
  2. next() ๋ฅผ ํ•˜๋ฉด, Api.fetch ๊ฐ€ ์‹คํ–‰๋˜๋ฉด์„œ Promise ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋จ.

์ด๋•Œ ์œ„ featchProducts()๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด์„ , API๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•˜๋Š”๋ฐ, ์‹ค์ œ๋กœ ํ˜ธ์ถœํ•  ์ˆœ ์—†์œผ๋ฏ€๋กœ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด API๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•ด์•ผํ•œ๋‹ค.

effects ์‚ฌ์šฉ

import { call } from "redux-saga/effects";
 
function* fetchProducts() {
  const products = yield call(Api.fetch, "/products");
  // ...
}
import { call } from "redux-saga/effects";
import Api from "...";
 
const iterator = fetchProducts();
 
// expects a call instruction
assert.deepEqual(
  iterator.next().value,
  call(Api.fetch, "/products"),
  "fetchProducts should yield an Effect call(Api.fetch, './products')"
);
  • ์œ„์—์„œ์™€๋Š” ๋‹ฌ๋ฆฌ, iterator.next() ๋Š” call ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋จ.
  • ์‹ค์ œ API๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ, ํ…Œ์ŠคํŠธํ•˜๊ธฐ๊ฐ€ ํ›จ์”ฌ ์šฉ์ดํ•ด์ง„๋‹ค.

api ํ˜ธ์ถœ effects

yield call([obj, obj.method], arg1, arg2, ...);		// ์ฃผ์–ด์ง„ object์˜ ์ฃผ์–ด์ง„ method๋ฅผ ํ˜ธ์ถœ
yield call(fn, arg1, arg2, ...);
yield apply(obj, obj.method, [arg1, arg2, ...])  // call๊ณผ ๊ฐ™๊ณ  arguments ํ˜•์‹๋งŒ ๋ฐ˜๋Œ€
yield cps(fn, arg1, arg2, ...)
  • node ์Šคํƒ€์ผ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ• ๋•Œ ์‚ฌ์šฉ ((error, result) โ‡’ ())

Dispatching Actions

์ œ๋„ˆ๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜ ์•ˆ์—์„œ, dispatch๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด ์ง์ ‘ dispatch๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

function* fetchProducts(dispatch) {
  const products = yield call(Api.fetch, "/products");
  dispatch({ type: "PRODUCTS_RECEIVED", products });
}

ํ•˜์ง€๋งŒ ์ด๋Š” ์œ„์—์„œ์™€ ๊ฐ™์ด, ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด์„  dispatch ํ•จ์ˆ˜๋ฅผ mock ํ•ด์•ผํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ redux-saga์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด โ€œputโ€ effect๋ฅผ ํ†ตํ•ด dispatch๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

import { call, put } from "redux-saga/effects";
// ...
 
function* fetchProducts() {
  const products = yield call(Api.fetch, "/products");
  // create and yield a dispatch Effect
  yield put({ type: "PRODUCTS_RECEIVED", products });
}

Error handling

์‚ฌ๊ฐ€์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ error handling ๊ฐ€๋Šฅ

import Api from "./path/to/api";
import { call, put } from "redux-saga/effects";
 
// ...
 
function* fetchProducts() {
  try {
    const products = yield call(Api.fetch, "/products");
    yield put({ type: "PRODUCTS_RECEIVED", products });
  } catch (error) {
    yield put({ type: "PRODUCTS_REQUEST_FAILED", error });
  }
}

์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค

import Api from "./path/to/api";
import { call, put } from "redux-saga/effects";
 
function fetchProductsApi() {
  return Api.fetch("/products")
    .then((response) => ({ response }))
    .catch((error) => ({ error }));
}
 
function* fetchProducts() {
  const { response, error } = yield call(fetchProductsApi);
  if (response) yield put({ type: "PRODUCTS_RECEIVED", products: response });
  else yield put({ type: "PRODUCTS_REQUEST_FAILED", error });
}

Saga helpers

  • takeEvery : ๋ชจ๋“  dispatch ์•ก์…˜์„ ์ˆ˜์šฉํ•จ
  • takeLatest : ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋“ค์–ด์˜จ ์•ก์…˜๋งŒ ์ˆ˜์šฉํ•จ
    • ์ด์ „์— ๋“ค์–ด์™€ ์ˆ˜ํ–‰์ค‘์ธ task๋Š” ์ค‘๋‹จ๋˜๊ณ , ๋‚˜์ค‘์— ๋“ค์–ด์˜จ task ๊ฐ€ ์ˆ˜ํ–‰๋˜๋Š” ๋ฐฉ์‹
import { takeEverym, takeLatest } from "redux-saga/effects";
 
function* watchFetchData() {
  yield takeEvery("FETCH_REQUESTED", fetchData);
}
function* watchFetchData() {
  yield takeLatest("FETCH_REQUESTED", fetchData);
}

channel

์•„๋ž˜์™€ ๊ฐ™์ด take/fork ๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ์š”์ฒญ์„ ๋™์‹œ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

import { take, fork, ... } from 'redux-saga/effects'
 
function* watchRequests() {
  while (true) {
    const {payload} = yield take('REQUEST')
    yield fork(handleRequest, payload)
  }
}
 
function* handleRequest(payload) { ... }

์ด๋•Œ REQUEST ์š”์ฒญ์„ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒซ๋ฒˆ์งธ๋ถ€ํ„ฐ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์„๋•Œ๋Š”, queue๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•˜๋‚˜๊ฐ€ ๋๋‚˜๋ฉด ๋‹ค์Œ ๊ฒƒ์„ ๊บผ๋‚ด์–ด ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜๋ฉด ๋œ๋‹ค.

์ด๋•Œ redux-sage์—์„œ๋Š” actionChannel(pattern)์ด๋ผ๋Š” effect๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. actionChannel์€ saga๊ฐ€ API call ๋“ฑ์œผ๋กœ ๋ง‰ํ˜€์žˆ์„๋•Œ๋„ ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ›์•„ ํ์— ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

import { take, actionChannel, call, ... } from 'redux-saga/effects'
 
function* watchRequests() {
  // 1- Create a channel for request actions
  const requestChan = yield actionChannel('REQUEST')
  while (true) {
    // 2- take from the channel
    const {payload} = yield take(requestChan)
    // 3- Note that we're using a blocking call
    yield call(handleRequest, payload)
  }
}
 
function* handleRequest(payload) { ... }

composing sagas

watch์—์„œ call์„ yieldํ•˜๋ฉด saga๋Š” call์ด ๋๋‚ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.

function* fetchPosts() {
  yield put(actions.requestPosts());
  const products = yield call(fetchApi, "/products");
  yield put(actions.receivePosts(products));
}
 
function* watchFetch() {
  while (yield take("FETCH_POSTS")) {
    yield call(fetchPosts); // waits for the fetchPosts task to terminate
  }
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด all ์ดํŽ™ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•˜๊ณ , ๋ชจ๋‘ ๋๋‚ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆด ์ˆ˜๋„ ์žˆ๋‹ค.

function* mainSaga(getState) {
  const results = yield all([call(task1), call(task2), ...])
  yield put(showResults(results))
}

concurrency

takeEvery์™€ takeLatest๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์‹œ์„ฑ์„ ์ œ์–ดํ•˜๋Š”๊ฐ€?

takeEvery

import { fork, take } from "redux-saga/effects";
 
const takeEvery = (pattern, saga, ...args) =>
  fork(function* () {
    while (true) {
      const action = yield take(pattern);
      yield fork(saga, ...args.concat(action));
    }
  });

ํ•ด๋‹นํ•˜๋Š” pattern์ด ๋“ค์–ด์˜ค๋ฉด fork๋ฅผ ํ†ตํ•ด ๋ถ„๋ฆฌํ•˜์—ฌ ์ฒ˜๋ฆฌํ•œ๋‹ค.

takeLatest

import { cancel, fork, take } from "redux-saga/effects";
 
const takeLatest = (pattern, saga, ...args) =>
  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));
    }
  });

์ƒˆ๋กœ์šด ๊ฒƒ์ด ๋“ค์–ด์˜ค๋ฉด ์ด์ „์— ๋“ค์–ด์˜จ ๊ฒƒ์„ ์ทจ์†Œํ•˜๊ณ , ์ƒˆ๋กœ์šด ๊ฒƒ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

Fork model

redux-saga์—์„œ๋Š” ๋‹ค์Œ ๋‘ ์ดํŽ™ํŠธ๋ฅผ ํ†ตํ•ด fork๋ฅผ ๋‹ค๋ฃฌ๋‹ค

  • fork : attached fork๋ฅผ ๋งŒ๋“ฆ
  • spawn : datached fork๋ฅผ ๋งŒ๋“ฆ

attached fork

Saga๋Š” ์ž๊ธฐ ์•ˆ์˜ ๋ชจ๋“  ๋ช…๋ น์–ด๊ฐ€ ์ˆ˜ํ–‰๋˜๊ณ , ๋ชจ๋“  attached forks๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ข…๋ฃŒ๋œ๋‹ค.

import { fork, call, put, delay } from "redux-saga/effects";
import api from "./somewhere/api"; // app specific
import { receiveData } from "./somewhere/actions"; // app specific
 
function* fetchAll() {
  const task1 = yield fork(fetchResource, "users");
  const task2 = yield fork(fetchResource, "comments");
  yield delay(1000);
}
 
function* fetchResource(resource) {
  const { data } = yield call(api.fetch, resource);
  yield put(receiveData(data));
}
 
function* main() {
  yield call(fetchAll);
}

๋”ฐ๋ผ์„œ ์œ„ call(fetchAll)์€ delay(1000)์ด ๋๋‚˜๊ณ , fork ๋œ task1, task2 ๊ฐ€ ๋๋‚˜์•ผ ์ข…๋ฃŒ๋œ๋‹ค.

์ด๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด parallel effect์™€ ๊ฐ™์€ ๋™์ž‘์„ ํ•œ๋‹ค.

function* fetchAll() {
  yield all([
    call(fetchResource, "users"), // task1
    call(fetchResource, "comments"), // task2,
    delay(1000),
  ]);
}

์ด๋•Œ ์œ„ ์ฝ”๋“œ๋Š” 3๊ฐœ์˜ child effect ์ค‘ ํ•˜๋‚˜๊ฐ€ ์‹คํŒจํ•˜๋ฉด, Error๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด์„œ 3๊ฐœ์˜ child๊ฐ€ ์ „๋ถ€ ์‹คํŒจํ•˜๊ณ (์™„๋ฃŒ๋˜์ง€ ์•Š์€ ์ž‘์—…๋“ค๋งŒ), Error๋Š” call๋กœ fetchAll์„ ํ˜ธ์ถœํ•œ ํ•จ์ˆ˜๋กœ throw๋œ๋‹ค.

์ด๋Š” fork๋ฅผ ์ด์šฉํ•œ ๋ชจ๋ธ์—์„œ๋„ ๋˜‘๊ฐ™์ด ๋™์ž‘ํ•˜๋ฉฐ ์•„๋ž˜์™€ ๊ฐ™์ด main์—์„œ try/catch๋ฅผ ํ†ตํ•ด ์—๋Ÿฌ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

//... imports
 
function* fetchAll() {
  const task1 = yield fork(fetchResource, "users");
  const task2 = yield fork(fetchResource, "comments");
  yield delay(1000);
}
 
function* fetchResource(resource) {
  const { data } = yield call(api.fetch, resource);
  yield put(receiveData(data));
}
 
function* main() {
  try {
    yield call(fetchAll);
  } catch (e) {
    // handle fetchAll errors
  }
}

Cancellation

Saga๋ฅผ cancelํ•˜๋ฉด, ์•„๋ž˜ ๊ฒƒ๋“ค์ด cancel๋œ๋‹ค

  • saga๊ฐ€ ํ˜„์žฌํ•˜๊ณ  ์žˆ๋Š” effect๋ถ€ํ„ฐ cancel๋จ
  • ๋ชจ๋“  attached fork ๊ฐ€ cancle๋จ

Detached forks

detached fork๋Š” ๊ทธ ์ž์ฒด๋Ÿฌ ํ•˜๋‚˜์˜ ์ˆ˜ํ–‰ํ™˜๊ฒฝ์ด๋‹ค. ๋”ฐ๋ผ์„œ, ๋ถ€๋ชจ๋Š” detached fork์˜ ์ข…๋ฃŒ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ , ์—๋Ÿฌ๋Š” ์ƒ์œ„ ๋ถ€๋ชจ๋กœ ์˜ฌ๋ผ๊ฐ€์ง€ ์•Š๋Š”๋‹ค.

๋˜ cancle์„ ํ•ด๋„ detached fork๋Š” ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.