Skip to content

Commit 90f7d66

Browse files
authored
Merge pull request #34 from kouhin/release/v1.2.0
Release/v1.2.0
2 parents 67d76c5 + f31e56d commit 90f7d66

File tree

9 files changed

+95
-117
lines changed

9 files changed

+95
-117
lines changed

.babelrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"presets": [
3-
"es2015"
3+
"env"
4+
],
5+
"plugins": [
6+
"transform-runtime"
47
]
58
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 - 2017 HOU Bin
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.

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-dataloader",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Loads async data for Redux apps focusing on preventing duplicated requests and dealing with async dependencies.",
55
"main": "lib/index.js",
66
"jsnext:main": "src/index.js",
@@ -42,10 +42,8 @@
4242
"babel-cli": "^6.18.0",
4343
"babel-core": "^6.20.0",
4444
"babel-eslint": "^7.1.1",
45-
"babel-loader": "^6.2.10",
4645
"babel-plugin-transform-runtime": "^6.15.0",
47-
"babel-preset-es2015": "^6.18.0",
48-
"babel-preset-stage-0": "^6.16.0",
46+
"babel-preset-env": "^1.2.1",
4947
"chai": "^3.5.0",
5048
"eslint-config-airbnb-deps": "^14.0.0",
5149
"isparta": "^4.0.0",
@@ -56,8 +54,9 @@
5654
"sinon": "^1.17.6"
5755
},
5856
"dependencies": {
59-
"async": "^2.1.4",
60-
"lodash": "^4.17.2"
57+
"babel-runtime": "^6.23.0",
58+
"lodash": "^4.17.2",
59+
"retryit": "^1.0.0"
6160
},
6261
"eslintConfig": {
6362
"extends": "eslint-config-airbnb",

src/Task.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import asyncify from 'async/asyncify';
2-
import retry from 'async/retry';
3-
import assign from 'lodash/assign';
1+
import retryit from 'retryit';
42

53
import { loadFailure, loadSuccess } from './actions';
64
import { isAction } from './utils';
@@ -12,11 +10,11 @@ export default class Task {
1210
throw new Error('action must be a plain object');
1311
}
1412

15-
this.context = assign({}, context, {
13+
this.context = Object.assign({}, context, {
1614
action: monitoredAction,
1715
});
1816

19-
this.params = assign({}, {
17+
this.params = Object.assign({}, {
2018
success({ action }) {
2119
throw new Error('success() is not implemented', action.type);
2220
},
@@ -35,8 +33,8 @@ export default class Task {
3533
}, params);
3634
}
3735

38-
execute(options = {}, callback) {
39-
const opts = assign({}, DEFAULT_OPTIONS, options);
36+
execute(options = {}) {
37+
const opts = Object.assign({}, DEFAULT_OPTIONS, options);
4038

4139
const context = this.context;
4240
const dispatch = context.dispatch;
@@ -51,37 +49,33 @@ export default class Task {
5149
const disableInternalAction = options.disableInternalAction;
5250

5351
if (!shouldFetch(context)) {
54-
callback(null, null); // load nothing
5552
if (!disableInternalAction) {
5653
const successAction = loadSuccess(context.action);
5754
dispatch(successAction);
5855
}
59-
return;
56+
return Promise.resolve();
6057
}
6158

6259
dispatch(loading(context));
6360

6461
// Retry
65-
const asyncFetch = asyncify(fetch);
66-
retry({
62+
return retryit({
6763
times: opts.retryTimes,
6864
interval: opts.retryWait,
69-
}, (retryCb) => {
70-
asyncFetch(context, retryCb);
71-
}, (err, result) => {
72-
if (err) {
65+
}, () => Promise.resolve(fetch(context)))
66+
.then((result) => {
67+
const successAction = success(context, result);
68+
if (!disableInternalAction) {
69+
dispatch(loadSuccess(context.action, result));
70+
}
71+
return dispatch(successAction);
72+
})
73+
.catch((err) => {
7374
const errorAction = error(context, err);
7475
if (!disableInternalAction) {
7576
dispatch(loadFailure(context.action, err));
7677
}
77-
callback(null, dispatch(errorAction));
78-
return;
79-
}
80-
const successAction = success(context, result);
81-
callback(null, dispatch(successAction));
82-
if (!disableInternalAction) {
83-
dispatch(loadSuccess(context.action, result));
84-
}
85-
});
78+
return dispatch(errorAction);
79+
});
8680
}
8781
}

src/TaskDescriptor.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import isEqual from 'lodash/isEqual';
2-
import assign from 'lodash/assign';
32

43
import Task from './Task';
54
import { DEFAULT_OPTIONS } from './constants';
@@ -8,7 +7,7 @@ export default class TaskDescriptor {
87
constructor(pattern, params, options = {}) {
98
this.pattern = pattern;
109
this.params = params;
11-
this.options = assign({}, DEFAULT_OPTIONS, options);
10+
this.options = Object.assign({}, DEFAULT_OPTIONS, options);
1211
if (this.options.retryTimes < 1) {
1312
this.options.retryTimes = 1;
1413
}

src/createDataLoaderMiddleware.js

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import findKey from 'lodash/findKey';
22
import find from 'lodash/find';
33
import isEqual from 'lodash/isEqual';
4-
import assign from 'lodash/assign';
54
import flattenDeep from 'lodash/flattenDeep';
65
import get from 'lodash/get';
7-
import isInteger from 'lodash/isInteger';
86

97
import { REDUX_DATALOADER_ACTION_ID } from './constants';
108

@@ -27,10 +25,14 @@ export default function createDataLoaderMiddleware(
2725

2826
const middleware = ({ dispatch, getState }) => {
2927
middleware.runningTasks = {};
30-
const ctx = assign({}, withArgs, {
31-
dispatch,
32-
getState,
33-
});
28+
const ctx = Object.assign(
29+
{},
30+
withArgs,
31+
{
32+
dispatch,
33+
getState,
34+
},
35+
);
3436

3537
return next => (receivedAction) => {
3638
// eslint-disable-next-line no-underscore-dangle
@@ -53,25 +55,17 @@ export default function createDataLoaderMiddleware(
5355
}
5456

5557
// Priority: Action Meta Options > TaskDescriptor Options > Middleware Options
56-
const options = assign(
58+
const options = Object.assign(
5759
{},
5860
middlewareOpts,
5961
taskDescriptor.options,
6062
get(asyncAction, 'meta.options', {}),
6163
);
6264

6365
const task = taskDescriptor.newTask(ctx, action);
64-
const runningTask = new Promise((resolve, reject) => {
65-
task.execute(options, (err, result) => {
66-
if (err) {
67-
reject(err);
68-
} else {
69-
resolve(result);
70-
}
71-
});
72-
});
66+
const runningTask = task.execute(options);
7367

74-
if (isInteger(options.ttl) && options.ttl > 0) {
68+
if (Number.isInteger(options.ttl) && options.ttl > 0) {
7569
const key = uniqueId(`${action.type}__`);
7670
middleware.runningTasks[key] = { action, promise: runningTask };
7771
if (typeof window !== 'undefined' && typeof document !== 'undefined') {

test/TaskDescriptor.test.js

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,17 @@ describe('DataLoderTaskDescriptor', () => {
4848
payload: {
4949
userId: 25,
5050
},
51-
}).execute({}, (err) => {
52-
if (err) {
53-
done(err);
54-
return;
55-
}
56-
expect(loadingSpy).to.have.been.calledOnce;
57-
expect(shouldFetchSpy).to.have.been.calledOnce;
58-
expect(fetchSpy).to.have.been.calledOnce;
59-
expect(successSpy).to.have.been.calledOnce;
60-
expect(errorSpy).to.have.not.been.called;
61-
sinon.assert.callOrder(shouldFetchSpy, fetchSpy, successSpy);
62-
done();
63-
}).catch(done);
51+
})
52+
.execute({})
53+
.then(() => {
54+
expect(loadingSpy).to.have.been.calledOnce;
55+
expect(shouldFetchSpy).to.have.been.calledOnce;
56+
expect(fetchSpy).to.have.been.calledOnce;
57+
expect(successSpy).to.have.been.calledOnce;
58+
expect(errorSpy).to.have.not.been.called;
59+
sinon.assert.callOrder(shouldFetchSpy, fetchSpy, successSpy);
60+
done();
61+
}).catch(done);
6462
});
6563

6664
it('loading -> shouldFetch(return false) -> noop', (done) => {
@@ -88,18 +86,17 @@ describe('DataLoderTaskDescriptor', () => {
8886
payload: {
8987
userId: 25,
9088
},
91-
}).execute({}, (err, result) => {
92-
if (err) {
93-
done(err);
94-
return;
95-
}
96-
expect(shouldFetchSpy).to.have.been.calledOnce;
97-
expect(loadingSpy).to.have.not.been.called;
98-
expect(fetchSpy).to.have.not.been.calledOnce;
99-
expect(successSpy).to.have.not.been.called;
100-
expect(errorSpy).to.have.not.been.called;
101-
done(null, result);
102-
});
89+
})
90+
.execute({})
91+
.then((result) => {
92+
expect(shouldFetchSpy).to.have.been.calledOnce;
93+
expect(loadingSpy).to.have.not.been.called;
94+
expect(fetchSpy).to.have.not.been.calledOnce;
95+
expect(successSpy).to.have.not.been.called;
96+
expect(errorSpy).to.have.not.been.called;
97+
done(null, result);
98+
})
99+
.catch(done);
103100
});
104101

105102
it('loading -> shouldFetch -> fetch -> error', (done) => {
@@ -127,18 +124,17 @@ describe('DataLoderTaskDescriptor', () => {
127124
payload: {
128125
userId: 25,
129126
},
130-
}).execute({}, (err, result) => {
131-
if (err) {
132-
done(err);
133-
return;
134-
}
135-
expect(loadingSpy).to.have.been.calledOnce;
136-
expect(shouldFetchSpy).to.have.been.calledOnce;
137-
expect(fetchSpy).to.have.been.calledOnce;
138-
expect(successSpy).to.have.not.been.called;
139-
expect(errorSpy).to.have.been.calledOnce;
140-
sinon.assert.callOrder(shouldFetchSpy, fetchSpy, errorSpy);
141-
done(null, result);
142-
});
127+
})
128+
.execute({})
129+
.then((result) => {
130+
expect(loadingSpy).to.have.been.calledOnce;
131+
expect(shouldFetchSpy).to.have.been.calledOnce;
132+
expect(fetchSpy).to.have.been.calledOnce;
133+
expect(successSpy).to.have.not.been.called;
134+
expect(errorSpy).to.have.been.calledOnce;
135+
sinon.assert.callOrder(shouldFetchSpy, fetchSpy, errorSpy);
136+
done(null, result);
137+
})
138+
.catch(done);
143139
});
144140
});

test/createDataLoaderMiddleware.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ describe('createDataLoaderMiddleware', () => {
215215
return store.dispatch(userActions.fetchUserRequest('tom'));
216216
})
217217
.then((result) => {
218-
expect(result).to.be.equal(null);
218+
expect(result).to.be.equal(undefined);
219219
return store.dispatch(userActions.fetchUserRequest('tom'));
220220
})
221221
.then(() => {

webpack.config.js

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)