Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

redux-saga-combine-watchers

1.0.3 • Public • Published

redux-saga-combine-watchers

Redux-Saga combine watchers util

NPM JavaScript Style Guide

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

Colaborators

Aleksandar Ilic

Install

npm i redux-saga-combine-watchers

DownloadsWeekly Downloads

49

Version

1.0.3

License

MIT

Unpacked Size

9.65 kB

Total Files

10

Last publish

Collaborators

  • avatar