Skip to content

Commit 285846b

Browse files
committed
Merge pull request #17 from lexich/async_helpers
Add async helpers
2 parents e2bf2c0 + b3a1905 commit 285846b

9 files changed

+117
-19
lines changed

README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ function (state, action) {
182182

183183
- @param **options.{endpoint}.helpers** - object
184184
```js
185-
{
185+
{
186+
logger: "/api/logger",
186187
test: {
187188
url: "/api/test/:name/:id",
188189
helpers: {
@@ -194,16 +195,27 @@ function (state, action) {
194195
const urlparams = {id, name};
195196
const params = {body: {uuid, data}};
196197
return [urlparams, params];
198+
},
199+
// complicated async logic
200+
async() {
201+
const {dispatch} = this;
202+
return (cb)=> {
203+
dispatch(rest.actions.logger((err)=> {
204+
const args = [{id: 1, name: "admin"}];
205+
cb(err, args);
206+
}));
207+
};
197208
}
198209
}
199210
}
200211
}
201212
// using helpers
202213
rest.actions.test.get(1, "admin");
203214
// with callback
204-
rest.actions.post(1, "admin", {msg: "Hello"}, (err)=> {
215+
rest.actions.test.post(1, "admin", {msg: "Hello"}, (err)=> {
205216
// end of action
206217
});
218+
rest.actions.test.async();
207219
```
208220

209221
#### reduxApi object

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.6.4",
3+
"version": "0.6.5",
44
"main": "dist/redux-api.min.js",
55
"dependencies": {}
66
}

dist/redux-api.js

+24-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/redux-api.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api",
3-
"version": "0.6.4",
3+
"version": "0.6.5",
44
"author": {
55
"name": "Efremov Alex",
66
"email": "lexich121@gmail.com",

src/actionFn.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,34 @@ export default function actionFn(url, name, options, ACTIONS={}, meta={}) {
107107
return fn(pathvars, modifyParams, callback)(dispatch, getState);
108108
};
109109
};
110-
return reduce(meta.helpers, (memo, func, name)=> {
110+
111+
return reduce(meta.helpers, (memo, func, helpername)=> {
112+
if (memo[helpername]) {
113+
throw new Error(`Helper name: "${helpername}" for endpoint "${name}" has been already reserved`);
114+
}
111115
const {sync, call} = isFunction(func) ? {call: func} : func;
112-
memo[name] = (...args)=> (dispatch, getState)=> {
116+
memo[helpername] = (...args)=> (dispatch, getState)=> {
113117
const index = args.length - 1;
114118
const callback = isFunction(args[index]) ? args[index] : none;
115-
const newArgs = fastApply(call, {getState}, args);
116-
return fastApply(
117-
sync ? fn.sync : fn,
118-
null,
119-
newArgs.concat(callback)
120-
)(dispatch, getState);
119+
const helpersResult = fastApply(call, {getState, dispatch}, args);
120+
121+
// If helper alias using async functionality
122+
if (isFunction(helpersResult)) {
123+
helpersResult((error, newArgs=[])=> {
124+
if (error) {
125+
callback(error);
126+
} else {
127+
fastApply(
128+
sync ? fn.sync : fn, null, newArgs.concat(callback)
129+
)(dispatch, getState);
130+
}
131+
});
132+
} else {
133+
// if helper alias is synchronous
134+
fastApply(
135+
sync ? fn.sync : fn, null, helpersResult.concat(callback)
136+
)(dispatch, getState);
137+
}
121138
};
122139
return memo;
123140
}, fn);

test/actionFn_spec.js

+49
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,53 @@ describe("actionFn", function() {
342342
]);
343343
});
344344
});
345+
it("check incorrect helpers name", function() {
346+
expect(
347+
()=> actionFn("/test/:id", "test", null, ACTIONS, { helpers: { reset() {} }})
348+
).to.throw(Error, `Helper name: "reset" for endpoint "test" has been already reserved`);
349+
expect(
350+
()=> actionFn("/test/:id", "test", null, ACTIONS, { helpers: { sync() {} }})
351+
).to.throw(Error, `Helper name: "sync" for endpoint "test" has been already reserved`);
352+
});
353+
it("check helpers with async functionality", function() {
354+
const meta = {
355+
holder: {fetch(url, opts) {
356+
return new Promise((resolve)=> resolve({url, opts}));
357+
}},
358+
helpers: {
359+
asyncSuccess: ()=> (cb)=> cb(null, [{id: 1}, {async: true}]),
360+
asyncFail: ()=> (cb)=> cb("Error")
361+
}
362+
};
363+
const api = actionFn("/test/:id", "test", null, ACTIONS, meta);
364+
const expectedEvent1 = [
365+
{
366+
type: ACTIONS.actionFetch,
367+
syncing: false
368+
}, {
369+
type: ACTIONS.actionSuccess,
370+
syncing: false,
371+
data: { url: "/test/1", opts: { async: true }}
372+
}
373+
];
374+
const wait1 = new Promise((resolve)=> {
375+
api.asyncSuccess(resolve)(function(msg) {
376+
expect(expectedEvent1).to.have.length.above(0);
377+
const exp = expectedEvent1.shift();
378+
expect(msg).to.eql(exp);
379+
}, getState);
380+
});
381+
let errorMsg;
382+
const wait2 = new Promise((resolve)=> {
383+
api.asyncFail(function(err) {
384+
errorMsg = err;
385+
resolve();
386+
})(function() {}, getState);
387+
});
388+
return Promise.all([wait1, wait2])
389+
.then(()=> {
390+
expect(expectedEvent1).to.have.length(0);
391+
expect(errorMsg).to.eql("Error");
392+
});
393+
});
345394
});

0 commit comments

Comments
 (0)