Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.

Commit 75d97a6

Browse files
authored
Merge pull request #10 from CodeSignal/9-classes
Fix the bug + add classes back in!
2 parents 8fe0649 + a7ff2f6 commit 75d97a6

File tree

9 files changed

+153
-70
lines changed

9 files changed

+153
-70
lines changed

.babelrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
"@babel/flow"
1010
],
1111
plugins: [
12+
"@babel/proposal-class-properties",
1213
"@babel/proposal-object-rest-spread",
1314
"transform-imports",
1415
"@babel/transform-modules-commonjs"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ need to handle the addition of dynamic reducers!
8989

9090
Here's what your Redux store creation will look like:
9191
```js
92-
import { combineReducers, createStore, applyMiddleware } from 'redux';
92+
import { compose, combineReducers, createStore, applyMiddleware } from 'redux';
9393
import thunk from 'redux-thunk';
9494

9595
import { combineAsyncReducers, configureRags } from 'redux-rags';

package-lock.json

Lines changed: 30 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-rags",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Redux Reducers, Actions, and Getters. Simplified!",
55
"main": "build/index.js",
66
"scripts": {
@@ -32,6 +32,7 @@
3232
"devDependencies": {
3333
"@babel/cli": "^7.2.3",
3434
"@babel/core": "^7.2.2",
35+
"@babel/plugin-proposal-class-properties": "^7.3.0",
3536
"@babel/plugin-proposal-object-rest-spread": "^7.3.1",
3637
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
3738
"@babel/preset-env": "^7.3.1",
@@ -49,6 +50,7 @@
4950
"flow-bin": "^0.86.0",
5051
"jest": "^23.6.0",
5152
"redux": "^4.0.1",
53+
"redux-thunk": "^2.3.0",
5254
"regenerator-runtime": "^0.13.1"
5355
}
5456
}

src/combineAsyncReducers.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ const recursivelyCombineAsyncReducers = function (combineReducers: Function, asy
1010
for (let prop of Object.getOwnPropertyNames(asyncReducers)) {
1111
const subreducer = asyncReducers[prop];
1212
if (typeof subreducer === 'object') {
13-
recursivelyCombineAsyncReducers(combineReducers, subreducer);
13+
reducers[prop] = recursivelyCombineAsyncReducers(combineReducers, subreducer);
1414
} else {
1515
reducers[prop] = subreducer;
1616
}
1717
}
18-
1918
return combineReducers(reducers);
2019
};
2120

src/factory.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -77,97 +77,97 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
7777
const {
7878
name = '',
7979
load,
80-
getInStore,
8180
loadOnlyOnce,
81+
getInStore,
8282
getInitialState,
8383
update,
8484
partialReducer,
8585
} = config;
8686
generatedCount += 1;
8787

8888
const safeDataName = `${name}/${generatedCount}`;
89-
const Getters = new function () {
90-
const wrappedGetInitialState: () => BoilerState<T> = createGetInitialState(getInitialState);
91-
this.getInitialState = wrappedGetInitialState;
89+
const wrappedGetInitialState: () => BoilerState<T> = createGetInitialState(getInitialState);
90+
class Getters {
91+
static getInitialState = wrappedGetInitialState;
9292

93-
this.get = getInStore ||
93+
static get = getInStore ||
9494
((reduxStore: Object): BoilerState<T> =>
95-
reduxStore[prefix][safeDataName] || this.getInitialState());
95+
reduxStore[prefix][safeDataName] || Getters.getInitialState());
9696

97-
this.getData = (reduxStore: Object): $PropertyType<BoilerState<T>, 'data'> => {
98-
const state = this.get(reduxStore);
97+
static getData = (reduxStore: Object): $PropertyType<BoilerState<T>, 'data'> => {
98+
const state = Getters.get(reduxStore);
9999
if (!state.hasOwnProperty('data')) {
100100
warning(`redux-rags: getData failed to find the property \'data\' on the object returned by Getters.get.
101101
This is likely caused by providing an incorrect 'getInStore' configuration option.`);
102102
}
103103
return state.data;
104104
};
105105

106-
this.getMeta = (reduxStore: Object): $PropertyType<BoilerState<T>, 'meta'> => {
107-
const state = this.get(reduxStore);
106+
static getMeta = (reduxStore: Object): $PropertyType<BoilerState<T>, 'meta'> => {
107+
const state = Getters.get(reduxStore);
108108
if (!state.hasOwnProperty('meta')) {
109109
warning(`redux-rags: getData failed to find the property \'meta\' on the object returned by Getters.get.
110110
This is likely caused by providing an incorrect 'getInStore' configuration option.`);
111111
}
112112
return state.meta;
113113
};
114114

115-
this.getIsLoading = (reduxStore: Object) => {
116-
const meta = this.getMeta(reduxStore);
115+
static getIsLoading = (reduxStore: Object) => {
116+
const meta = Getters.getMeta(reduxStore);
117117
return meta && meta.loading;
118118
};
119-
};
119+
}
120120

121121
const BEGIN_LOADING = getLoadingType(name);
122122
const END_LOADING = getEndLoadingType(name);
123123
const ERRORS = getErrorsType(name);
124124
const UPDATE_DATA = getUpdateType(name);
125125
const RESET = getResetType(name);
126126

127-
const Actions = new function(){
128-
this.beginLoading = () => ({
127+
class Actions {
128+
static beginLoading = () => ({
129129
type: BEGIN_LOADING,
130130
payload: null,
131131
});
132132

133-
this.endLoading = () => ({
133+
static endLoading = () => ({
134134
type: END_LOADING,
135135
payload: null,
136136
});
137137

138-
this.reset = () => ({
138+
static reset = () => ({
139139
type: RESET,
140140
payload: null,
141141
});
142142

143-
this.errors = (errors: Object | String) => ({
143+
static errors = (errors: Object | String) => ({
144144
type: ERRORS,
145145
payload: errors,
146146
});
147147

148-
this.clearErrors = () => ({
148+
static clearErrors = () => ({
149149
type: ERRORS,
150150
payload: null,
151151
});
152152

153-
this.updateData = (data: ?T) => ({
153+
static updateData = (data: ?T) => ({
154154
type: UPDATE_DATA,
155155
payload: data,
156156
});
157157

158-
this.update = (...args: *) => async (dispatch, getState: () => Object) => {
158+
static update = (...args: *) => async (dispatch, getState: () => Object) => {
159159
if (!update || typeof update !== 'function') {
160160
return;
161161
}
162162
try {
163163
const manipulated = await Promise.resolve(update(Getters.getData(getState()), ...args));
164-
dispatch(this.updateData(manipulated));
164+
dispatch(Actions.updateData(manipulated));
165165
} catch (err) {
166-
dispatch(this.errors(err));
166+
dispatch(Actions.errors(err));
167167
}
168168
};
169169

170-
this.load = (...args: G) => async (
170+
static load = (...args: G) => async (
171171
dispatch: Dispatch,
172172
getState: () => Object
173173
): Promise<?T> => {
@@ -180,18 +180,18 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
180180
return state.data;
181181
}
182182
}
183-
dispatch(this.beginLoading());
183+
dispatch(Actions.beginLoading());
184184
try {
185185
const data = await Promise.resolve(load(...args));
186-
dispatch(this.updateData(data));
186+
dispatch(Actions.updateData(data));
187187
return data;
188188
} catch (err) {
189-
dispatch(this.errors(err));
190-
dispatch(this.endLoading());
189+
dispatch(Actions.errors(err));
190+
dispatch(Actions.endLoading());
191191
return null;
192192
}
193193
};
194-
};
194+
}
195195

196196
type Interpret = <R>((...Iterable<any>) => R) => R;
197197
type ExtractReturn<Fn> = $Call<Interpret, Fn>;
@@ -202,10 +202,10 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
202202
| ExtractReturn<typeof Actions.clearErrors>
203203
| ExtractReturn<typeof Actions.updateData>
204204
| ExtractReturn<typeof Actions.endLoading>;
205-
const Subreducer = new function() {
206-
this.partialReducer = partialReducer;
205+
class Subreducer {
206+
static partialReducer = partialReducer;
207207

208-
this.subreduce = (
208+
static subreduce = (
209209
state?: BoilerState<T> = Getters.getInitialState(),
210210
action?: GeneratedAction | * // Support other action types
211211
) => {
@@ -235,13 +235,13 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
235235
case RESET:
236236
return (Getters.getInitialState(): BoilerState<T>);
237237
default:
238-
if (typeof this.partialReducer === 'function') {
239-
return {...state, ...(this.partialReducer(state, action) || {})};
238+
if (typeof Subreducer.partialReducer === 'function') {
239+
return {...state, ...(Subreducer.partialReducer(state, action) || {})};
240240
}
241241
return state;
242242
}
243243
};
244-
};
244+
}
245245

246246
if (!getInStore) {
247247
injectReducer([prefix, safeDataName], Subreducer.subreduce);

src/factoryMap.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,40 @@ let generatedCount = 0;
3434
const convertArgsToString = (...args) => JSON.stringify(args);
3535

3636
const createFactoryMap = (injectReducer: Function) => {
37-
const factory = createFactory(injectReducer);
38-
return <T, G: Array<mixed>>(config: ConfigType<T, G>): ReturnType<T, G> => {
39-
const { name = '', load, getInitialState } = config;
40-
generatedCount += 1;
37+
const factory = createFactory(injectReducer);
38+
return <T, G: Array<mixed>>(config: ConfigType<T, G>): ReturnType<T, G> => {
39+
const { name = '', load, getInitialState } = config;
40+
generatedCount += 1;
4141

42-
const safeDataName = `${name}/${generatedCount}`;
43-
const mapArgsToGenerated = {};
44-
const Getters = new function() {
45-
const _getInitialStateForKey: () => BoilerState<T> = createGetInitialState(getInitialState);
42+
const safeDataName = `${name}/${generatedCount}`;
43+
const mapArgsToGenerated = {};
44+
class Getters {
45+
static _getInitialStateForKey: () => BoilerState<T> = createGetInitialState(getInitialState);
4646

47-
this.get = (reduxStore: Object): MapState<T> =>
47+
static get = (reduxStore: Object): MapState<T> =>
4848
reduxStore[prefix] && reduxStore[prefix][safeDataName];
4949

50-
this.getWithArgs = (reduxStore, ...args) => {
50+
static getWithArgs = (reduxStore, ...args) => {
5151
const argsKey = convertArgsToString(...args);
52-
const state = this.get(reduxStore);
52+
const state = Getters.get(reduxStore);
5353
if (!state || !state.hasOwnProperty(argsKey)) {
54-
return _getInitialStateForKey();
54+
return Getters._getInitialStateForKey();
5555
}
5656
return state[argsKey];
5757
};
5858

59-
this.getData = (reduxStore, ...args) => this.getWithArgs(reduxStore, ...args).data;
59+
static getData = (reduxStore, ...args) => Getters.getWithArgs(reduxStore, ...args).data;
6060

61-
this.getMeta = (reduxStore, ...args) => this.getWithArgs(reduxStore, ...args).meta;
61+
static getMeta = (reduxStore, ...args) => Getters.getWithArgs(reduxStore, ...args).meta;
6262

63-
this.getIsLoading = (reduxStore: Object, ...args) => {
64-
const meta = this.getMeta(reduxStore, ...args);
63+
static getIsLoading = (reduxStore: Object, ...args) => {
64+
const meta = Getters.getMeta(reduxStore, ...args);
6565
return meta.loading;
6666
};
67-
};
67+
}
6868

69-
const Actions = new function() {
70-
const _queryOrCreateBoilerplate = (...args) => {
69+
class Actions {
70+
static _queryOrCreateBoilerplate = (...args) => {
7171
const stringHash = convertArgsToString(...args);
7272
if (!mapArgsToGenerated[stringHash]) {
7373
// Need to generate everything for this. Luckily we have a generator
@@ -85,30 +85,30 @@ const createFactoryMap = (injectReducer: Function) => {
8585
};
8686

8787
// Links to argument-less actions generated by the factory.
88-
const _forwardActionForSubreducer = (actionName: string, { forwardArgs = false }: * = {}) => (
88+
static _forwardActionForSubreducer = (actionName: string, { forwardArgs = false }: * = {}) => (
8989
...args: Array<mixed>
9090
) => async dispatch => {
91-
const actions = _queryOrCreateBoilerplate(...args).actions;
91+
const actions = Actions._queryOrCreateBoilerplate(...args).actions;
9292
const action = actions[actionName];
9393
if (forwardArgs) {
9494
return dispatch(action(...args)); // Assumed to be loading arguments.
9595
}
9696
return dispatch(action());
9797
};
9898

99-
this.load = _forwardActionForSubreducer('load', { forwardArgs: true });
99+
static load = Actions._forwardActionForSubreducer('load', { forwardArgs: true });
100100

101-
this.reset = _forwardActionForSubreducer('reset');
101+
static reset = Actions._forwardActionForSubreducer('reset');
102102

103-
this.clearErrors = _forwardActionForSubreducer('clearErrors');
104-
}
103+
static clearErrors = Actions._forwardActionForSubreducer('clearErrors');
104+
}
105105

106-
return {
107-
actions: Actions,
108-
getters: Getters,
109-
};
106+
return {
107+
actions: Actions,
108+
getters: Getters,
110109
};
111110
};
111+
};
112112

113113
export default createFactoryMap;
114114

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function configureRags(store: *, createRootReducer: *) {
1212
injectReducer = makeReducerInjector(store, createRootReducer);
1313
ragFactory = createFactory(injectReducer);
1414
ragFactoryMap = createFactoryMap(injectReducer);
15-
};
15+
}
1616

1717
export {
1818
combineAsyncReducers,

0 commit comments

Comments
 (0)