redux-saga-combine-watchers
v1.0.3
Published
Redux-Saga combine watchers util
Downloads
2,100
Maintainers
Readme
redux-saga-combine-watchers
Redux-Saga combine watchers util
Install
npm install --save redux-saga-combine-watchers
About
combineWatchers
is a simple util, ultra small (0.6 kb gzipped) that helps you organize your saga watchers easily without worrying about the way that you export watchers from your saga files.
type Watchers = GeneratorFunction | GeneratorFunction[]
const combineWatchers = (...args: Watchers[]): Generator[]
Example
store/sagas/usersSaga.js
import { put, takeLatest } from 'redux-saga/effects';
import {setUsers, setSelectedUserDetails} from "../actions";
import {GET_USERS, GET_CURRENT_USER} from "../types";
import {apiUrl} from "../../config";
const usersEndpoint = '...'
function* fetchUsers() {
const users = yield fetch(`${apiUrl}/${usersEndpoint}`).then(res => res.json());
yield put(setUsers(users.result))
}
function* fetchUserDetailInfo(action) {
const users = yield fetch(`${apiUrl}/${usersEndpoint}/${action.payload}`).then(res => res.json());
yield put(setSelectedUserDetails(users.result))
}
function* getUsersWatcher() {
yield takeLatest(GET_USERS, fetchUsers)
}
function* getCurrentUserWatcher() {
yield takeLatest(GET_CURRENT_USER, fetchUsers)
}
export const userWatchers = [getUsersWatcher, getCurrentUserWatcher]
store/sagas/imagesSaga.js
import { put, takeLatest } from 'redux-saga/effects';
import {setImages} from "../actions";
import {GET_IMAGES} from "../types";
import {apiUrl} from "../../config";
const imagesEndPoint = '...'
function* fetchImages() {
const images = yield fetch(`${apiUrl}/${imagesEndPoint}`).then(res => res.json());
yield put(setImages(images.result))
}
export function* getImagesWatcher() {
yield takeLatest(GET_IMAGES, fetchImages)
store/sagas/index.js
import { all } from 'redux-saga/effects';
import { combineWatchers } from 'redux-saga-combine-watchers';
import {userWatchers} from "./usersSaga";
import {getImagesWatcher} from "./imagesSaga";
export function* rootSaga() {
yield all(combineWatchers(userWatchers, getImagesWatcher))
}
License
MIT © rasha08