├── .babelrc ├── .eslintrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── package.json ├── prepublish.js ├── src ├── createMiddleware.js ├── index.js └── on.js ├── test ├── createMiddleware.spec.js └── on.spec.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015" 4 | ], 5 | "sourceMaps": "true" 6 | } -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "babo" 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | 35 | # IDE directory 36 | .idea 37 | 38 | # Buid 39 | lib 40 | dist -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "5" 4 | script: 5 | - npm run lint 6 | - npm test -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Babo-Ltd 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # redux-thaga [![Build Status](https://travis-ci.org/babotech/redux-thaga.svg?branch=master)](https://travis-ci.org/babotech/redux-thaga) 2 | 3 | To not choose between [redux-thunk](https://github.com/gaearon/redux-thunk) and [redux-saga](https://github.com/yelouafi/redux-saga) 4 | 5 | ## Installation 6 | 7 | ``` 8 | npm install --save redux-thaga 9 | ``` 10 | 11 | ## Integration 12 | 13 | ```javascript 14 | ... 15 | import * as sagas from './sagas' 16 | import createThagaMiddleware from 'redux-thaga' 17 | 18 | const thaga = createThagaMiddleware(sagas) 19 | 20 | const createStoreWithMiddlewares = applyMiddleware(thaga)(createStore) 21 | 22 | ... 23 | ``` 24 | 25 | ## Usage 26 | 27 | Like thunk for async actions 28 | 29 | ```javascript 30 | export const asyncActionCreator = () => (dispatch, getState) => { 31 | dispatch({ 32 | type: `REQUEST` 33 | }) 34 | 35 | request() 36 | .then(() => 37 | dispatch({ 38 | type: `SUCCESS` 39 | }) 40 | ) 41 | } 42 | 43 | ``` 44 | 45 | Like saga 46 | 47 | ```javascript 48 | import {on} from 'redux-thaga' 49 | 50 | export const onSomeAction = on(`SOME_ACTION_TYPE`, (dispatch, getState, action) => { 51 | request() 52 | .then(() => { 53 | dispatch({ 54 | type: `ANOTHER_ACTION_TYPE` 55 | }) 56 | }) 57 | }) 58 | 59 | ``` 60 | 61 | ## License 62 | 63 | **MIT** 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-thaga", 3 | "version": "0.1.0", 4 | "description": "Like redux-saga like redux-thunk", 5 | "files": [ 6 | "dist", 7 | "lib", 8 | "src" 9 | ], 10 | "main": "./lib/index.js", 11 | "scripts": { 12 | "build:lib": "babel src --out-dir lib", 13 | "build:umd": "cross-env NODE_ENV=development webpack src/index.js dist/redux-thaga.js", 14 | "build:umd:min": "cross-env NODE_ENV=production webpack src/index.js dist/redux-thaga.min.js", 15 | "build": "npm run build:lib && npm run build:umd && npm run build:umd:min && node ./prepublish", 16 | "lint": "eslint src test", 17 | "lint:fix": "eslint --fix src test", 18 | "test": "mocha --compilers js:babel-register --recursive", 19 | "test:watch": "npm test -- --watch" 20 | }, 21 | "repository": { 22 | "type": "git", 23 | "url": "git+https://github.com/babotech/redux-thaga.git" 24 | }, 25 | "keywords": [ 26 | "redux", 27 | "react", 28 | "reactjs", 29 | "redux-saga", 30 | "redux-thunk", 31 | "middleware" 32 | ], 33 | "author": "Galkin Rostislav (http://github.com/galkinrost)", 34 | "license": "MIT", 35 | "bugs": { 36 | "url": "https://github.com/babotech/redux-thaga/issues" 37 | }, 38 | "homepage": "https://github.com/babotech/redux-thaga#readme", 39 | "devDependencies": { 40 | "babel-core": "^6.5.1", 41 | "babel-loader": "^6.2.2", 42 | "babel-preset-es2015": "^6.5.0", 43 | "cross-env": "^1.0.7", 44 | "es3ify": "^0.2.1", 45 | "eslint": "2.2.0", 46 | "eslint-config-babo": "^0.1.1", 47 | "expect": "^1.14.0", 48 | "glob": "^7.0.0", 49 | "jsdom": "^8.0.2", 50 | "mocha": "^2.4.5", 51 | "redux": "^3.3.1", 52 | "webpack": "^1.12.13" 53 | }, 54 | "dependencies": {} 55 | } 56 | -------------------------------------------------------------------------------- /prepublish.js: -------------------------------------------------------------------------------- 1 | var glob = require('glob'); 2 | var fs = require('fs'); 3 | var es3ify = require('es3ify'); 4 | 5 | glob('./@(lib|dist)/**/*.js', function (err, files) { 6 | if (err) { 7 | throw err 8 | } 9 | 10 | files.forEach(function (file) { 11 | fs.readFile(file, 'utf8', function (err, data) { 12 | if (err) { 13 | throw err 14 | } 15 | 16 | fs.writeFile(file, es3ify.transform(data), function (err) { 17 | if (err) { 18 | throw err 19 | } 20 | 21 | console.log('es3ified ' + file); 22 | }) 23 | }) 24 | }) 25 | }); -------------------------------------------------------------------------------- /src/createMiddleware.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable callback-return */ 2 | 3 | const createThunkSagaMiddleware = (sagas = []) => { 4 | const combinedSagas = Array.isArray(sagas) ? sagas : Object 5 | .keys(sagas) 6 | .map(key => sagas[ key ]) 7 | 8 | return ({dispatch, getState} = {}) => { 9 | return next => action => { 10 | if (typeof action === `function`) { 11 | return action(dispatch, getState) 12 | } 13 | next(action) 14 | combinedSagas 15 | .forEach(saga => saga.type === action.type && saga.actionCreator(dispatch, getState, action)) 16 | } 17 | } 18 | } 19 | 20 | export default createThunkSagaMiddleware -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export {default as on} from './on' 2 | export {default} from './createMiddleware' 3 | -------------------------------------------------------------------------------- /src/on.js: -------------------------------------------------------------------------------- 1 | const on = (type, actionCreator) => ({ 2 | type, 3 | actionCreator 4 | }) 5 | 6 | export default on -------------------------------------------------------------------------------- /test/createMiddleware.spec.js: -------------------------------------------------------------------------------- 1 | import createMiddleware from '../src/createMiddleware' 2 | import expect from 'expect' 3 | 4 | describe(`redux-thunk-saga`, () => { 5 | 6 | describe(`createMiddleware`, () => { 7 | 8 | it(`should work like redux-thunk if action is a function`, () => { 9 | const middleware = createMiddleware() 10 | 11 | const store = { 12 | dispatch: () => null, 13 | getState: () => null 14 | } 15 | 16 | const action = expect.createSpy() 17 | const next = expect.createSpy() 18 | 19 | middleware(store)(next)(action) 20 | 21 | expect(next.calls.length).toEqual(0) 22 | expect(action.calls.length).toEqual(1) 23 | 24 | const {arguments: args} = action.calls[ 0 ] 25 | 26 | expect(args).toEqual([ 27 | store.dispatch, 28 | store.getState 29 | ]) 30 | }) 31 | 32 | it(`should call next if action is an object`, () => { 33 | const action = { 34 | type: `ACTION_TYPE` 35 | } 36 | 37 | const middleware = createMiddleware() 38 | const next = expect.createSpy() 39 | 40 | middleware()(next)(action) 41 | 42 | expect(next.calls.length).toEqual(1) 43 | 44 | const {arguments: args} = next.calls[ 0 ] 45 | 46 | expect(args).toEqual([ action ]) 47 | }) 48 | 49 | it(`should call action creators`, () => { 50 | const sagas = { 51 | onBar: { 52 | type: `BAR`, 53 | actionCreator: expect.createSpy() 54 | }, 55 | onBaz: { 56 | type: `BAZ`, 57 | actionCreator: expect.createSpy() 58 | } 59 | } 60 | 61 | const middleware = createMiddleware(sagas) 62 | const action = { 63 | type: `BAR` 64 | } 65 | const next = () => null 66 | const store = { 67 | dispatch: () => null, 68 | getState: () => null 69 | } 70 | 71 | middleware(store)(next)(action) 72 | 73 | expect(sagas.onBar.actionCreator.calls.length).toEqual(1) 74 | 75 | const {arguments: args} = sagas.onBar.actionCreator.calls[ 0 ] 76 | 77 | expect(args).toEqual([ 78 | store.dispatch, 79 | store.getState, 80 | action 81 | ]) 82 | 83 | expect(sagas.onBaz.actionCreator.calls.length).toEqual(0) 84 | }) 85 | }) 86 | }) -------------------------------------------------------------------------------- /test/on.spec.js: -------------------------------------------------------------------------------- 1 | import expect from 'expect' 2 | import on from '../src/on' 3 | 4 | describe(`redux-thunk-saga`, () => { 5 | describe(`on`, () => { 6 | 7 | it(`should create object with type and actionCreator`, () => { 8 | const type = `ACTION_TYPE` 9 | const actionCreator = () => null 10 | 11 | expect(on(type, actionCreator)) 12 | .toEqual({ 13 | type, 14 | actionCreator 15 | }) 16 | }) 17 | }) 18 | }) 19 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var webpack = require('webpack'); 4 | var env = process.env.NODE_ENV; 5 | 6 | var config = { 7 | module: { 8 | loaders: [ 9 | { test: /\.js$/, loaders: ['babel-loader'], exclude: /node_modules/ } 10 | ] 11 | }, 12 | output: { 13 | library: 'ReduxThaga', 14 | libraryTarget: 'umd' 15 | }, 16 | plugins: [ 17 | { 18 | apply: function apply(compiler) { 19 | compiler.parser.plugin('expression global', function expressionGlobalPlugin() { 20 | this.state.module.addVariable('global', "(function() { return this; }()) || Function('return this')()") 21 | return false 22 | }) 23 | } 24 | }, 25 | new webpack.optimize.OccurenceOrderPlugin(), 26 | new webpack.DefinePlugin({ 27 | 'process.env.NODE_ENV': JSON.stringify(env) 28 | }) 29 | ] 30 | }; 31 | 32 | if (env === 'production') { 33 | config.plugins.push( 34 | new webpack.optimize.UglifyJsPlugin({ 35 | compressor: { 36 | pure_getters: true, 37 | unsafe: true, 38 | unsafe_comps: true, 39 | screw_ie8: true, 40 | warnings: false 41 | } 42 | }) 43 | ) 44 | }; 45 | 46 | module.exports = config; --------------------------------------------------------------------------------