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, ??)- iterator ์๋ ์ ๋๋ ์ดํฐ ๊ฐ์ฒด๊ฐ ๋ฐํ๋จ.
- 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๋ ์ํฅ์ ๋ฐ์ง ์๋๋ค.