├── .babelrc ├── .eslintrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── package.json ├── src ├── controls │ └── index.js ├── index.js └── utils │ ├── helpers.js │ ├── is.js │ └── keys.js └── test └── controls └── index.spec.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"] 3 | } 4 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint:recommended", 3 | "env": { 4 | "browser": true, 5 | "node": true, 6 | "es6": true 7 | }, 8 | "parser": "babel-eslint" 9 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | node_modules 3 | dist -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .babelrc 2 | .eslintrc 3 | test 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Riad Benguella 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Redux Rungen 2 | ============ 3 | 4 | Generator middleware for Redux (Based on [rungen](https://github.com/youknowriad/rungen)) 5 | 6 | Another redux-saga ? 7 | -------------------- 8 | 9 | I like [redux-saga](https://github.com/yelouafi/redux-saga) a lot, It's quite powerfull and if you already use it and feel 10 | quite happy about it, than just continue. This is just a pale copy of it :). 11 | 12 | But if like me, 13 | * You find that the generator approach is a breeze, 14 | * You like how easy testing those generators is. 15 | 16 | And in the same time you find the `redux-saga` approach a little bit harder to reason about in comparison to `thunks`, than I made this library for you (for me at first :P) 17 | 18 | So, how does this work ? 19 | ------------------------ 20 | 21 | The principle here is simple. Instead of writing your action creators using thunks, you just replace them (one to one) by generators. 22 | 23 | **Example :** 24 | 25 | Take the following thunk action creator 26 | 27 | ```javascript 28 | function requestPost(id) { 29 | return dispatch => { 30 | dispatch({ type: 'post-fetch', payload: id }) 31 | fetch('/api/post/' + id) 32 | .then(post => { 33 | dispatch({ type: 'post-fetch-success', payload: post }) 34 | }) 35 | .catch(error => { 36 | dispatch({ type: 'post-fetch-error', payload: error }) 37 | }) 38 | } 39 | } 40 | ``` 41 | 42 | The equivalent using `redux-rungen` is 43 | 44 | ```javascript 45 | import {put} from 'redux-rungen' 46 | import {call} from 'rungen' 47 | 48 | function* requestPost(id) { 49 | yield put({ type: 'post-fetch', payload: id }) 50 | try { 51 | const post = yield call(fetch, '/api/post/' + id) 52 | yield put({ type: 'post-fetch-success', payload: post }) 53 | } catch (error) { 54 | yield put({ type: 'post-fetch-error', payload: error }) 55 | } 56 | } 57 | ``` 58 | 59 | Why It's better ? 60 | ----------------- 61 | 62 | - Because testing effects based generators is refreshing : The generator itself never triggers the side effect (API call) it self. This means no mocking is required to test your action creators. 63 | - Because I can compose my action creators easily and have a really nice way to express complex flows using `fork`/`join` effects. 64 | 65 | Seems too complex ? 66 | ------------------- 67 | 68 | If you are not yet familiar with ES6 generators and runtimes like [co](https://github.com/tj/co) or [rungen](https://github.com/youknowriad/rungen), I understand. 69 | So if you are not interested in the ease of testing and are happy with what you're doing 70 | using `thunks`, you certainly don't want to use rungen :) 71 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-rungen", 3 | "version": "0.2.0", 4 | "description": "Write your redux action creators as generators", 5 | "main": "dist/index.js", 6 | "devDependencies": { 7 | "babel": "^6.3.26", 8 | "babel-cli": "^6.4.5", 9 | "babel-eslint": "^5.0.0-beta8", 10 | "babel-polyfill": "^6.3.14", 11 | "babel-preset-es2015": "^6.3.13", 12 | "babel-register": "^6.4.3", 13 | "conventional-changelog": "^0.5.3", 14 | "eslint": "^1.10.3", 15 | "expect": "^1.13.4", 16 | "mocha": "^2.4.5", 17 | "redux": "^3.5.2", 18 | "rimraf": "^2.5.1", 19 | "rungen": "^0.3.0" 20 | }, 21 | "peerDependencies": { 22 | "rungen": "^0.3.2" 23 | }, 24 | "scripts": { 25 | "lint": "eslint src", 26 | "compile": "rimraf lib && babel -d dist/ src/", 27 | "prepublish": "npm run test && npm run compile", 28 | "unit": "mocha --compilers js:babel-register --recursive test", 29 | "test": "npm run lint && npm run unit", 30 | "changelog": "conventional-changelog -i CHANGELOG.md -w -r 0 -p angular" 31 | }, 32 | "repository": { 33 | "type": "git", 34 | "url": "git+https://github.com/youknowriad/redux-rungen.git" 35 | }, 36 | "author": "Riad Benguella ", 37 | "license": "MIT", 38 | "bugs": { 39 | "url": "https://github.com/youknowriad/redux-rungen/issues" 40 | }, 41 | "homepage": "https://github.com/youknowriad/redux-rungen#readme" 42 | } 43 | -------------------------------------------------------------------------------- /src/controls/index.js: -------------------------------------------------------------------------------- 1 | import is from '../utils/is' 2 | 3 | const createControls = (dispatch, getState) => { 4 | const select = (value, next) => { 5 | if (!is.select(value)) return false 6 | const result = value.selector 7 | ? value.selector(getState(), ...value.args) 8 | : getState() 9 | next(result) 10 | return true 11 | } 12 | 13 | const put = (value, next) => { 14 | if (!is.put(value)) return false 15 | next(dispatch(value.action)) 16 | return true 17 | } 18 | 19 | return [select, put] 20 | } 21 | 22 | export default createControls 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import is from './utils/is' 2 | import createControls from './controls' 3 | import { create, asyncControls, wrapControls } from 'rungen' 4 | export * from './utils/helpers' 5 | 6 | const middleware = store => { 7 | const runtime = create([ 8 | ...asyncControls, 9 | ...wrapControls, 10 | ...createControls(store.dispatch, store.getState) 11 | ]) 12 | 13 | const runtimeReturningPromise = input => 14 | new Promise((resolve, reject) => 15 | runtime(input, resolve, reject) 16 | ) 17 | 18 | return next => action => 19 | is.iterator(action) ? 20 | runtimeReturningPromise(action) : 21 | next(action) 22 | } 23 | 24 | export default middleware 25 | -------------------------------------------------------------------------------- /src/utils/helpers.js: -------------------------------------------------------------------------------- 1 | import keys from './keys' 2 | 3 | export const select = (selector, ...args) => ({ 4 | type: keys.select, 5 | selector, 6 | args 7 | }) 8 | 9 | export const put = action => ({ 10 | type: keys.put, 11 | action 12 | }) 13 | -------------------------------------------------------------------------------- /src/utils/is.js: -------------------------------------------------------------------------------- 1 | import keys from './keys' 2 | 3 | const is = { 4 | obj : value => typeof value === 'object' && !! value, 5 | func : value => typeof value === 'function', 6 | select : value => is.obj(value) && value.type === keys.select, 7 | put : value => is.obj(value) && value.type === keys.put, 8 | iterator : value => value && is.func(value.next) && is.func(value.throw) 9 | } 10 | 11 | export default is 12 | -------------------------------------------------------------------------------- /src/utils/keys.js: -------------------------------------------------------------------------------- 1 | const keys = { 2 | select : Symbol('select'), 3 | put : Symbol('put') 4 | } 5 | 6 | export default keys 7 | -------------------------------------------------------------------------------- /test/controls/index.spec.js: -------------------------------------------------------------------------------- 1 | import 'babel-polyfill' 2 | import expect from 'expect' 3 | import { create } from 'rungen' 4 | import createControls from '../../src/controls' 5 | import {put, select} from '../../src/utils/helpers' 6 | 7 | describe('Redux Controls', () => { 8 | it('should select all the redux state', () => { 9 | let output 10 | const fakeState = { 11 | 10: { id: 10, title: 'title' } 12 | } 13 | const dispatch = () => {} 14 | const getState = () => fakeState 15 | const generator = function* () { 16 | output = yield select() 17 | } 18 | const runtime = create(createControls(dispatch, getState)) 19 | 20 | runtime(generator()) 21 | expect(output).toEqual(fakeState) 22 | }) 23 | 24 | it('should select using a selector', () => { 25 | let output 26 | const fakeState = { 27 | 10: { id: 10, title: 'title' } 28 | } 29 | const selector = (state, id) => state[id] 30 | const dispatch = () => {} 31 | const getState = () => fakeState 32 | const generator = function* () { 33 | output = yield select(selector, 10) 34 | } 35 | const runtime = create(createControls(dispatch, getState)) 36 | 37 | runtime(generator()) 38 | expect(output).toEqual({ id: 10, title: 'title' }) 39 | }) 40 | 41 | it('should dispatch the action', () => { 42 | let output 43 | const action = { type: 'my action' } 44 | const dispatch = action => ({ dispatched: true, action }) 45 | const getState = () => {} 46 | const generator = function* () { 47 | output = yield put(action) 48 | } 49 | const runtime = create(createControls(dispatch, getState)) 50 | 51 | runtime(generator()) 52 | expect(output).toEqual({ dispatched: true, action }) 53 | }) 54 | }) 55 | --------------------------------------------------------------------------------