├── .babelrc ├── .editorconfig ├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── package.json ├── src └── index.js └── test └── test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015", "stage-0"] 3 | } 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | coverage 17 | index.js 18 | !src/* 19 | !test/* 20 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | before_install: 2 | git config user.name "Travis CI" 3 | 4 | language: 5 | node_js 6 | node_js: 7 | - stable 8 | 9 | after_script: 10 | npm run test 11 | 12 | sudo: false 13 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright © 2015 Christoph Hermann 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. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | redux-future 2 | ============ 3 | 4 | [![build status](https://img.shields.io/travis/stoeffel/redux-future/master.svg?style=flat-square)](https://travis-ci.org/stoeffel/redux-future) 5 | [![npm version](https://img.shields.io/npm/v/redux-future.svg?style=flat-square)](https://www.npmjs.com/package/redux-future) 6 | 7 | [FSA](https://github.com/acdlite/flux-standard-action)-compliant future monad [middleware](https://github.com/gaearon/redux/blob/master/docs/middleware.md) for Redux. 8 | 9 | This is based on [redux-promise](https://github.com/acdlite/redux-promise). 10 | 11 | ```js 12 | npm install --save redux-future 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import futureMiddleware from 'redux-future'; 19 | ``` 20 | 21 | The default export is a middleware function. If it receives a future, it will dispatch the resolved value of the future (after forking the future). It will dispatch the error if one occures. 22 | 23 | If it receives an Flux Standard Action whose `payload` is a future, it will `fork` and then either 24 | 25 | - dispatch a copy of the action with the resolved value of the future. 26 | - dispatch a copy of the action with the rejected value of the future, and set `error` to `true`. 27 | 28 | 29 | ### Example 30 | 31 | ```js 32 | const result = new Future((reject, resolve) => 33 | resolve([1, 2, 3, 4, 5, 6])); 34 | 35 | const resultFiltered = result.map( 36 | R.compose( 37 | R.assoc('numbers', R.__, { type: 'FILTER_NUMBERS' }) 38 | , R.filter(R.gt(3)) 39 | )); // will hold [1, 2] 40 | 41 | store.dispatch(resultFiltered); 42 | ``` 43 | 44 | ## Using in combination with redux-actions 45 | 46 | Because it supports FSA actions, you can use redux-future in combination with [redux-actions](https://github.com/acdlite/redux-actions). 47 | 48 | ### Example: Action creators 49 | 50 | ```js 51 | const result = new Future((reject, resolve) => 52 | resolve([1, 2, 3, 4, 5, 6]) 53 | ); 54 | 55 | const resultFiltered = result.map(R.filter(R.gt(3))); // will hold [1, 2] 56 | 57 | createAction('FILTER_ASYNC', () => resultFiltered); 58 | // or 59 | const filterAction = createAction('FILTER_ASYNC'); 60 | filterAction(resultFiltered); 61 | ``` 62 | 63 | ### Example: Future(IO) 64 | 65 | You can use `redux-future` together with [`redux-io`](https://github.com/stoeffel/redux-io). 66 | 67 | ```js 68 | // futureIo :: Future(IO(String)) 69 | const futureIo = new Future((rej, res) => { 70 | const io = IO(() => location.href); 71 | 72 | setTimeout(() => res(io), 2000); 73 | }); 74 | 75 | const action = createAction('FSA_ACTION'); 76 | store.dispatch(action(futureIo)); 77 | ``` 78 | 79 | 80 | ## Related 81 | 82 | ### Resources 83 | 84 | Don't know what a future is? Read the following blog post or watch the video. 85 | 86 | * [A Monad in Practicality: Controlling Time](http://robotlolita.me/2014/03/20/a-monad-in-practicality-controlling-time.html) 87 | * [Monad a day 2: Future](https://vimeo.com/106008027) 88 | 89 | ### Libraries 90 | 91 | * [folktale data.task](https://github.com/folktale/data.task) 92 | * [ramda-fantasy](https://github.com/ramda/ramda-fantasy) 93 | * [fantasy-future](https://github.com/jsanchesleao/fantasy-future) 94 | * [futurize](https://github.com/stoeffel/futurize) - Turn callback-style functions or promises into futures 95 | * [redux-io](https://github.com/stoeffel/redux-io) - FSA-compliant IO monad middleware for redux 96 | * [redux-either](https://github.com/stoeffel/redux-either) - FSA-compliant Either monad middleware for redux 97 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-future", 3 | "version": "0.0.11", 4 | "description": "FSA-compliant future monad middleware for redux", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha --require babel-core/register", 8 | "prepublish": "npm run transpile", 9 | "patch-release": "npm version patch && npm publish && git push --follow-tags", 10 | "minor-release": "npm version minor && npm publish && git push --follow-tags", 11 | "major-release": "npm version major && npm publish && git push --follow-tags", 12 | "transpile": "babel src --out-dir ." 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "git+https://github.com/stoeffel/redux-future.git" 17 | }, 18 | "keywords": [ 19 | "redux", 20 | "future", 21 | "task", 22 | "promise", 23 | "middleware", 24 | "redux-middleware", 25 | "fsa", 26 | "flux", 27 | "monad" 28 | ], 29 | "author": "schtoeffel", 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/stoeffel/redux-future/issues" 33 | }, 34 | "homepage": "https://github.com/stoeffel/redux-future#readme", 35 | "devDependencies": { 36 | "babel": "^6.3.13", 37 | "babel-cli": "^6.3.17", 38 | "babel-core": "^6.3.17", 39 | "babel-preset-es2015": "^6.3.13", 40 | "babel-preset-stage-0": "^6.3.13", 41 | "data.task": "^3.0.0", 42 | "expect": "^1.13.2", 43 | "mocha": "^2.3.4", 44 | "ramda": "^0.18.0", 45 | "ramda-fantasy": "^0.4.1", 46 | "redux": "^3.0.5", 47 | "redux-actions": "^0.9.0", 48 | "redux-io": "0.0.3" 49 | }, 50 | "dependencies": { 51 | "flux-standard-action": "^0.6.0" 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import { isFSA } from 'flux-standard-action'; 2 | 3 | 4 | 5 | function isFuture(val) { 6 | return val && typeof val.fork === 'function'; 7 | } 8 | 9 | export default function futureMiddleware({ dispatch }) { 10 | return next => action => { 11 | if (!isFSA(action)) { 12 | return isFuture(action) 13 | ? action.fork( 14 | error => error.type? dispatch(error): next(action) 15 | , dispatch 16 | ) 17 | : next(action); 18 | } 19 | 20 | return isFuture(action.payload) 21 | ? action.payload.fork( 22 | error => dispatch({ ...action, payload: error, error: true }) 23 | , result => dispatch({ ...action, payload: result }) 24 | ) 25 | : next(action); 26 | }; 27 | } 28 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import expect from 'expect'; 2 | import { createStore, applyMiddleware } from 'redux'; 3 | import { createAction } from 'redux-actions'; 4 | import Future from 'data.task'; 5 | import R from 'ramda'; 6 | import { IO } from 'ramda-fantasy'; 7 | import ioMiddleware from 'redux-io'; 8 | 9 | import futureMiddleware from '../src'; 10 | 11 | 12 | describe('redux-future', () => { 13 | let store, unsubscribe; 14 | 15 | before(() => { 16 | const initialState = 17 | { counter: 0 18 | , filtered: [] 19 | , filteredFSA: [] 20 | , futureIo: '' 21 | }; 22 | 23 | function counter(state = initialState, action) { 24 | switch (action.type) { 25 | case 'INCREMENT': 26 | return { ... state 27 | , counter: state.counter + 1 28 | }; 29 | case 'FILTER': 30 | return { ... state 31 | , filtered: action.numbers 32 | }; 33 | case 'FILTER_NUMBERS': 34 | return { ... state 35 | , filtered: action.numbers 36 | }; 37 | case 'FILTER_NUMBERS_FSA': 38 | return { ... state 39 | , filteredFSA: action.payload 40 | }; 41 | case 'FUTURE_IO': 42 | return { ... state 43 | , futureIo: action.payload 44 | }; 45 | default: 46 | return state 47 | } 48 | } 49 | 50 | const createStoreWithMiddleware = applyMiddleware( 51 | ioMiddleware('runIO') 52 | , futureMiddleware 53 | )(createStore) 54 | 55 | store = createStoreWithMiddleware(counter); 56 | }); 57 | 58 | 59 | afterEach(() => { 60 | unsubscribe(); 61 | }); 62 | 63 | it('should work without a future', done => { 64 | unsubscribe = store.subscribe(() => { 65 | expect(store.getState().counter).toEqual(1); 66 | done(); 67 | }); 68 | store.dispatch({ type: 'INCREMENT' }); 69 | }); 70 | 71 | it('should work with a future', done => { 72 | unsubscribe = store.subscribe(() => { 73 | expect(store.getState().filtered).toEqual([1, 2]); 74 | done(); 75 | }); 76 | const result = new Future((reject, resolve) => 77 | resolve({ type: 'FILTER', numbers: [1, 2] })); 78 | 79 | store.dispatch(result); 80 | }); 81 | 82 | it('should work with a future', done => { 83 | unsubscribe = store.subscribe(() => { 84 | expect(store.getState().filtered).toEqual([1, 2]); 85 | done(); 86 | }); 87 | const result = new Future((reject, resolve) => 88 | resolve([1, 2, 3, 4, 5, 6])); 89 | 90 | const resultFiltered = result.map( 91 | R.compose( 92 | R.assoc('numbers', R.__, { type: 'FILTER_NUMBERS' }) 93 | , R.filter(R.gt(3)) 94 | )); // will hold [1, 2] 95 | 96 | store.dispatch(resultFiltered); 97 | }); 98 | 99 | it('should work with a FSA', done => { 100 | unsubscribe = store.subscribe(() => { 101 | expect(store.getState().filteredFSA).toEqual([1, 2]); 102 | done(); 103 | }); 104 | const result = new Future((reject, resolve) => 105 | resolve([1, 2, 3, 4, 5, 6])); 106 | 107 | const resultFiltered = result.map(R.filter(R.gt(3))); // will hold [1, 2] 108 | 109 | const filterNumbers = createAction('FILTER_NUMBERS_FSA', () => resultFiltered); 110 | 111 | store.dispatch(filterNumbers()); 112 | }); 113 | 114 | it('should work together with IOs', done => { 115 | const spy = expect.createSpy() 116 | 117 | unsubscribe = store.subscribe(() => { 118 | expect(store.getState().futureIo).toEqual('back to the future'); 119 | expect(spy).toHaveBeenCalled() 120 | done(); 121 | }); 122 | const future = new Future((rej, res) => { 123 | const io = IO(() => { 124 | spy(); 125 | return 'back to the future'; 126 | }); 127 | 128 | setTimeout(() => res(io), 100); 129 | }); 130 | 131 | 132 | const action = createAction('FUTURE_IO'); 133 | store.dispatch(action(future)); 134 | }); 135 | }); 136 | --------------------------------------------------------------------------------