Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"presets": ["env", "es2015", "flow", "stage-3"],
"plugins": ["babel-plugin-transform-runtime", "babel-plugin-transform-class-properties"]
"presets": ["env", "es2015", "flow", "stage-3"]
}
27 changes: 6 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-rags",
"version": "1.0.1",
"version": "1.1.0",
"description": "Redux Reducers, Actions, and Getters. Simplified!",
"main": "build/index.js",
"scripts": {
Expand Down Expand Up @@ -50,7 +50,6 @@
"babel-jest": "^23.6.0",
"babel-plugin-dynamic-import-node": "^2.2.0",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
Expand All @@ -63,8 +62,5 @@
"flow-bin": "^0.86.0",
"jest": "^23.6.0",
"redux": "^4.0.1"
},
"dependencies": {
"babel-runtime": "^6.26.0"
}
}
46 changes: 23 additions & 23 deletions src/combineAsyncReducers.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
// @flow

const recursivelyCombineAsyncReducers = (combineReducers: Function, asyncReducers: Function | Object) => {
// Don't combine a reducer leaf
if (typeof asyncReducers !== 'object') {
return asyncReducers;
}
// Call combineReducers on every function, recursive walk.
const reducers = {};
for (let prop of Object.getOwnPropertyNames(asyncReducers)) {
const subreducer = asyncReducers[prop];
if (typeof subreducer === 'object') {
recursivelyCombineAsyncReducers(combineReducers, subreducer);
} else {
reducers[prop] = subreducer;
}
const recursivelyCombineAsyncReducers = function (combineReducers: Function, asyncReducers: Function | Object) {
// Don't combine a reducer leaf
if (typeof asyncReducers !== 'object') {
return asyncReducers;
}
// Call combineReducers on every function, recursive walk.
const reducers = {};
for (let prop of Object.getOwnPropertyNames(asyncReducers)) {
const subreducer = asyncReducers[prop];
if (typeof subreducer === 'object') {
recursivelyCombineAsyncReducers(combineReducers, subreducer);
} else {
reducers[prop] = subreducer;
}
}

return combineReducers(reducers);
return combineReducers(reducers);
};

const combineAsyncReducers = (combineReducers: Function, asyncReducers: Function | Object) => {
const newAsyncReducers = Object.getOwnPropertyNames(asyncReducers).reduce(
(reducers, key) => {
reducers[key] = recursivelyCombineAsyncReducers(combineReducers, asyncReducers[key]);
return reducers;
}
, {});
return newAsyncReducers;
const combineAsyncReducers = function (combineReducers: Function, asyncReducers: Function | Object) {
const newAsyncReducers = Object.getOwnPropertyNames(asyncReducers).reduce(
(reducers, key) => {
reducers[key] = recursivelyCombineAsyncReducers(combineReducers, asyncReducers[key]);
return reducers;
}
, {});
return newAsyncReducers;
};

export default combineAsyncReducers;
70 changes: 34 additions & 36 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,89 +86,88 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
generatedCount += 1;

const safeDataName = `${name}/${generatedCount}`;
const Getters = new function () {
const wrappedGetInitialState: () => BoilerState<T> = createGetInitialState(getInitialState);
this.getInitialState = wrappedGetInitialState;

class Getters {
static getInitialState: () => BoilerState<T> = createGetInitialState(getInitialState);

static get =
getInStore ||
this.get = getInStore ||
((reduxStore: Object): BoilerState<T> =>
reduxStore[prefix][safeDataName] || Getters.getInitialState());
reduxStore[prefix][safeDataName] || this.getInitialState());

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

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

static getIsLoading = (reduxStore: Object) => {
const meta = Getters.getMeta(reduxStore);
this.getIsLoading = (reduxStore: Object) => {
const meta = this.getMeta(reduxStore);
return meta && meta.loading;
};
}
};

const BEGIN_LOADING = getLoadingType(name);
const END_LOADING = getEndLoadingType(name);
const ERRORS = getErrorsType(name);
const UPDATE_DATA = getUpdateType(name);
const RESET = getResetType(name);

class Actions {
static beginLoading = () => ({
const Actions = new function(){
this.beginLoading = () => ({
type: BEGIN_LOADING,
payload: null,
});

static endLoading = () => ({
this.endLoading = () => ({
type: END_LOADING,
payload: null,
});

static reset = () => ({
this.reset = () => ({
type: RESET,
payload: null,
});

static errors = (errors: Object | String) => ({
this.errors = (errors: Object | String) => ({
type: ERRORS,
payload: errors,
});

static clearErrors = () => ({
this.clearErrors = () => ({
type: ERRORS,
payload: null,
});

static updateData = (data: ?T) => ({
this.updateData = (data: ?T) => ({
type: UPDATE_DATA,
payload: data,
});

static update = (...args: *) => async (dispatch, getState: () => Object) => {
this.update = (...args: *) => async (dispatch, getState: () => Object) => {
if (!update || typeof update !== 'function') {
return;
}
try {
const manipulated = await Promise.resolve(update(Getters.getData(getState()), ...args));
dispatch(Actions.updateData(manipulated));
dispatch(this.updateData(manipulated));
} catch (err) {
dispatch(Actions.errors(err));
dispatch(this.errors(err));
}
};

static load = (...args: G) => async (
this.load = (...args: G) => async (
dispatch: Dispatch,
getState: () => Object
): Promise<?T> => {
Expand All @@ -181,19 +180,19 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
return state.data;
}
}
dispatch(Actions.beginLoading());
dispatch(this.beginLoading());
try {
const data = await Promise.resolve(load(...args));
dispatch(Actions.updateData(data));
dispatch(this.updateData(data));
return data;
} catch (err) {
dispatch(Actions.errors(err));
dispatch(this.errors(err));
return null;
} finally {
dispatch(Actions.endLoading());
dispatch(this.endLoading());
}
};
}
};

type Interpret = <R>((...Iterable<any>) => R) => R;
type ExtractReturn<Fn> = $Call<Interpret, Fn>;
Expand All @@ -204,11 +203,10 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
| ExtractReturn<typeof Actions.clearErrors>
| ExtractReturn<typeof Actions.updateData>
| ExtractReturn<typeof Actions.endLoading>;
const Subreducer = new function() {
this.partialReducer = partialReducer;

class Subreducer {
static partialReducer = partialReducer;

static subreduce = (
this.subreduce = (
state?: BoilerState<T> = Getters.getInitialState(),
action?: GeneratedAction | * // Support other action types
) => {
Expand Down Expand Up @@ -238,16 +236,16 @@ const createFactory = (injectReducer: Function) => <T, G: Array<mixed>>(config:
case RESET:
return (Getters.getInitialState(): BoilerState<T>);
default:
if (typeof Subreducer.partialReducer === 'function') {
if (typeof this.partialReducer === 'function') {
return {
...state,
...(Subreducer.partialReducer(state, action) || {}),
...(this.partialReducer(state, action) || {}),
};
}
return state;
}
};
}
};

if (!getInStore) {
injectReducer([prefix, safeDataName], Subreducer.subreduce);
Expand Down
Loading