Skip to content

Commit

Permalink
make a duck
Browse files Browse the repository at this point in the history
  • Loading branch information
monteslu committed Aug 1, 2018
1 parent 0820644 commit ba97ea4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
31 changes: 25 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ const { exampleAHandler,
} = combinedHandler(['exampleA', 'exampleB']);


export function fetchDataA() {
export function exampleA() {
return function dispatcher(dispatch) {
const promise = somePromiseAPI();
return exampleAHandler(promise, dispatch);
};
}

export function fetchDataB() {
export function exampleB() {
return function dispatcher(dispatch) {
const promise = somePromiseOtherAPI();
return exampleBHandler(promise, dispatch);
Expand Down Expand Up @@ -165,11 +165,30 @@ const { exampleAAction,
reducerCombined
} = combinedHandler(['exampleA', 'exampleB']);

export const fetchDataA = () => exampleAAction(somePromiseAPI());
export const exampleA = () => exampleAAction(somePromiseAPI());
export const exampleB = ipnut => exampleBAction(someOtherAPI(input));

export const fetchDataB = () => exampleBAction(someOtherAPI());

//this will run through all the reducers created
export default reducerCombined;

```

## makeDuck

![makeduck](makeduck.png)

Now that we're using middleware, can we make the above example even more automatic?
Of course!

In cases where you just want to provide async or promise-returning functions and let cooldux manage the actions, dispatching, and the reducer, you can simply provide `cooldux.makeDuck` with a map of those functions.

```javascript

const duck = makeDuck({
exmapleA : somePromiseAPI,
exampleB : input => someOtherAPI(input)
});

export const { exampleA, exampleB } = duck;
export default duck.reducerCombined;

```
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ var promiseMiddleware = function(ref) {
};
};

function makeDuck(actions, options) {
var actionProps = Object.keys(actions).filter(function(key) {
return typeof actions[key] === 'function';
});
var duck = combinedHandler(actionProps, options);
actionProps.forEach(function(key) {
duck[key] = function() {
return duck[key + 'Action'](actions[key].apply(null, arguments));
};
});
duck.reducer = duck.reducerCombined;
return duck;
}

exports.makeActionCreator = makeActionCreator;

exports.reset = reset;
Expand All @@ -157,3 +171,5 @@ exports.promiseHandler = promiseHandler;
exports.combinedHandler = combinedHandler;

exports.promiseMiddleware = promiseMiddleware;

exports.makeDuck = makeDuck;
17 changes: 17 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,20 @@ export const promiseMiddleware = ({ dispatch }) => {
}
}
}

/**
* A function that returns action creators and a combined reducer from a map of promise-returning functions.
*
* @param {Object} actions An object of promise-returning functions
*/
export function makeDuck(actions, options) {
const actionProps = Object.keys(actions).filter(key => typeof actions[key] === 'function');
const duck = combinedHandler(actionProps, options);
actionProps.forEach(key => {
duck[key] = function() {
return duck[key + 'Action'](actions[key].apply(null, arguments));
}
});
duck.reducer = duck.reducerCombined;
return duck;
}
Binary file added makeduck.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 26 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var spies = require('chai-spies');
chai.use(spies);

var expect = chai.expect;
chai.should();
var should = chai.should();

var cooldux = require('../index.js');

Expand Down Expand Up @@ -248,7 +248,32 @@ describe('cooldux', function() {
store.dispatch.should.have.been.called.twice;
next.should.not.have.been.called.with(action);
done();
});
});

it('creates a duck', (done) => {
const duck = cooldux.makeDuck({
a : (input) => Promise.resolve(input),
b : () => Promise.reject('bad'),
c : 1,
d : {},
});
duck.should.be.a('object');
duck.a.should.be.a('function');
duck.b.should.be.a('function');
should.equal(duck.c, undefined);
should.equal(duck.d, undefined);

const { next, invoke, store } = createMiddleware();
const action = duck.a('hello');
invoke(action)
.then(res => {
store.dispatch.should.have.been.called.twice;
next.should.not.have.been.called.with(action);
res.should.equal('hello');
done();
})

});

});

0 comments on commit ba97ea4

Please sign in to comment.