diff --git a/.babelrc b/.babelrc index 70904de..257745c 100644 --- a/.babelrc +++ b/.babelrc @@ -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"] } diff --git a/package-lock.json b/package-lock.json index 3dc2910..3b18a7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "redux-rags", - "version": "1.0.1", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -734,12 +734,6 @@ "integrity": "sha1-a8lj67FuzLrmuStZbrfzXDQqi5o=", "dev": true }, - "babel-plugin-syntax-class-properties": { - "version": "6.13.0", - "resolved": "http://registry.npmjs.org/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz", - "integrity": "sha1-1+sjt5oxf4VDlixQW4J8fWysJ94=", - "dev": true - }, "babel-plugin-syntax-dynamic-import": { "version": "6.18.0", "resolved": "http://registry.npmjs.org/babel-plugin-syntax-dynamic-import/-/babel-plugin-syntax-dynamic-import-6.18.0.tgz", @@ -792,18 +786,6 @@ "babel-runtime": "6.26.0" } }, - "babel-plugin-transform-class-properties": { - "version": "6.24.1", - "resolved": "https://registry.npmjs.org/babel-plugin-transform-class-properties/-/babel-plugin-transform-class-properties-6.24.1.tgz", - "integrity": "sha1-anl2PqYdM9NvN7YRqp3vgagbRqw=", - "dev": true, - "requires": { - "babel-helper-function-name": "6.24.1", - "babel-plugin-syntax-class-properties": "6.13.0", - "babel-runtime": "6.26.0", - "babel-template": "6.26.0" - } - }, "babel-plugin-transform-es2015-arrow-functions": { "version": "6.22.0", "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-arrow-functions/-/babel-plugin-transform-es2015-arrow-functions-6.22.0.tgz", @@ -1237,6 +1219,7 @@ "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz", "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", + "dev": true, "requires": { "core-js": "2.5.7", "regenerator-runtime": "0.11.1" @@ -1735,7 +1718,8 @@ "core-js": { "version": "2.5.7", "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.7.tgz", - "integrity": "sha512-RszJCAxg/PP6uzXVXL6BsxSXx/B05oJAQ2vkJRjyjrEcNVycaqOmNb5OTxZPE3xa5gwZduqza6L9JOCenh/Ecw==" + "integrity": "sha512-RszJCAxg/PP6uzXVXL6BsxSXx/B05oJAQ2vkJRjyjrEcNVycaqOmNb5OTxZPE3xa5gwZduqza6L9JOCenh/Ecw==", + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -6599,7 +6583,8 @@ "regenerator-runtime": { "version": "0.11.1", "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", - "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==" + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==", + "dev": true }, "regenerator-transform": { "version": "0.10.1", diff --git a/package.json b/package.json index b06379e..c41e1ef 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", @@ -63,8 +62,5 @@ "flow-bin": "^0.86.0", "jest": "^23.6.0", "redux": "^4.0.1" - }, - "dependencies": { - "babel-runtime": "^6.26.0" } } diff --git a/src/combineAsyncReducers.js b/src/combineAsyncReducers.js index 3f8e224..07a2931 100644 --- a/src/combineAsyncReducers.js +++ b/src/combineAsyncReducers.js @@ -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; diff --git a/src/factory.js b/src/factory.js index bfa24e1..7e8e31e 100644 --- a/src/factory.js +++ b/src/factory.js @@ -86,17 +86,16 @@ const createFactory = (injectReducer: Function) => >(config: generatedCount += 1; const safeDataName = `${name}/${generatedCount}`; + const Getters = new function () { + const wrappedGetInitialState: () => BoilerState = createGetInitialState(getInitialState); + this.getInitialState = wrappedGetInitialState; - class Getters { - static getInitialState: () => BoilerState = createGetInitialState(getInitialState); - - static get = - getInStore || + this.get = getInStore || ((reduxStore: Object): BoilerState => - reduxStore[prefix][safeDataName] || Getters.getInitialState()); + reduxStore[prefix][safeDataName] || this.getInitialState()); - static getData = (reduxStore: Object): $PropertyType, 'data'> => { - const state = Getters.get(reduxStore); + this.getData = (reduxStore: Object): $PropertyType, '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.`); @@ -104,8 +103,8 @@ const createFactory = (injectReducer: Function) => >(config: return state.data; }; - static getMeta = (reduxStore: Object): $PropertyType, 'meta'> => { - const state = Getters.get(reduxStore); + this.getMeta = (reduxStore: Object): $PropertyType, '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.`); @@ -113,11 +112,11 @@ const createFactory = (injectReducer: Function) => >(config: 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); @@ -125,50 +124,50 @@ const createFactory = (injectReducer: Function) => >(config: 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 => { @@ -181,19 +180,19 @@ const createFactory = (injectReducer: Function) => >(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 = ((...Iterable) => R) => R; type ExtractReturn = $Call; @@ -204,11 +203,10 @@ const createFactory = (injectReducer: Function) => >(config: | ExtractReturn | ExtractReturn | ExtractReturn; + const Subreducer = new function() { + this.partialReducer = partialReducer; - class Subreducer { - static partialReducer = partialReducer; - - static subreduce = ( + this.subreduce = ( state?: BoilerState = Getters.getInitialState(), action?: GeneratedAction | * // Support other action types ) => { @@ -238,16 +236,16 @@ const createFactory = (injectReducer: Function) => >(config: case RESET: return (Getters.getInitialState(): BoilerState); 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); diff --git a/src/factoryMap.js b/src/factoryMap.js index 70f74cc..532ce84 100644 --- a/src/factoryMap.js +++ b/src/factoryMap.js @@ -41,55 +41,54 @@ const createFactoryMap = (injectReducer: Function) => { const safeDataName = `${name}/${generatedCount}`; const mapArgsToGenerated = {}; + const Getters = new function() { + const _getInitialStateForKey: () => BoilerState = createGetInitialState(getInitialState); - class Getters { - static _getInitialStateForKey: () => BoilerState = createGetInitialState(getInitialState); - - static get = (reduxStore: Object): MapState => + this.get = (reduxStore: Object): MapState => reduxStore[prefix] && reduxStore[prefix][safeDataName]; - static getWithArgs = (reduxStore, ...args) => { + this.getWithArgs = (reduxStore, ...args) => { const argsKey = convertArgsToString(...args); - const state = Getters.get(reduxStore); + const state = this.get(reduxStore); if (!state || !state.hasOwnProperty(argsKey)) { - return Getters._getInitialStateForKey(); + return _getInitialStateForKey(); } return state[argsKey]; }; - static getData = (reduxStore, ...args) => Getters.getWithArgs(reduxStore, ...args).data; + this.getData = (reduxStore, ...args) => this.getWithArgs(reduxStore, ...args).data; - static getMeta = (reduxStore, ...args) => Getters.getWithArgs(reduxStore, ...args).meta; + this.getMeta = (reduxStore, ...args) => this.getWithArgs(reduxStore, ...args).meta; - static getIsLoading = (reduxStore: Object, ...args) => { - const meta = Getters.getMeta(reduxStore, ...args); + this.getIsLoading = (reduxStore: Object, ...args) => { + const meta = this.getMeta(reduxStore, ...args); return meta.loading; }; - } - - const queryOrCreateBoilerplate = (...args) => { - const stringHash = convertArgsToString(...args); - if (!mapArgsToGenerated[stringHash]) { - // Need to generate everything for this. Luckily we have a generator - const getOutOfStore: any = store => Getters.getWithArgs(store, ...args); - mapArgsToGenerated[stringHash] = factory({ - name: safeDataName, - load, - getInitialState, - getInStore: getOutOfStore, - }); - const subreducer = mapArgsToGenerated[stringHash].subreducer; - injectReducer([prefix, safeDataName, stringHash], subreducer); - } - return mapArgsToGenerated[stringHash]; }; - class Actions { + const Actions = new function() { + const _queryOrCreateBoilerplate = (...args) => { + const stringHash = convertArgsToString(...args); + if (!mapArgsToGenerated[stringHash]) { + // Need to generate everything for this. Luckily we have a generator + const getOutOfStore: any = store => Getters.getWithArgs(store, ...args); + mapArgsToGenerated[stringHash] = factory({ + name: safeDataName, + load, + getInitialState, + getInStore: getOutOfStore, + }); + const subreducer = mapArgsToGenerated[stringHash].subreducer; + injectReducer([prefix, safeDataName, stringHash], subreducer); + } + return mapArgsToGenerated[stringHash]; + }; + // Links to argument-less actions generated by the factory. - static _forwardActionForSubreducer = (actionName: string, { forwardArgs = false }: * = {}) => ( + const _forwardActionForSubreducer = (actionName: string, { forwardArgs = false }: * = {}) => ( ...args: Array ) => async dispatch => { - const actions = queryOrCreateBoilerplate(...args).actions; + const actions = _queryOrCreateBoilerplate(...args).actions; const action = actions[actionName]; if (forwardArgs) { return dispatch(action(...args)); // Assumed to be loading arguments. @@ -97,11 +96,11 @@ const createFactoryMap = (injectReducer: Function) => { return dispatch(action()); }; - static load = Actions._forwardActionForSubreducer('load', { forwardArgs: true }); + this.load = _forwardActionForSubreducer('load', { forwardArgs: true }); - static reset = Actions._forwardActionForSubreducer('reset'); + this.reset = _forwardActionForSubreducer('reset'); - static clearErrors = Actions._forwardActionForSubreducer('clearErrors'); + this.clearErrors = _forwardActionForSubreducer('clearErrors'); } return { diff --git a/src/index.js b/src/index.js index cd030b3..3a4028a 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,7 @@ let injectReducer = noConfigurationWarning; let ragFactory = noConfigurationWarning; let ragFactoryMap = noConfigurationWarning; -const configureRags = (store: *, createRootReducer: *) => { +function configureRags(store: *, createRootReducer: *) { injectReducer = makeReducerInjector(store, createRootReducer); ragFactory = createFactory(injectReducer); ragFactoryMap = createFactoryMap(injectReducer); diff --git a/src/injectReducer.js b/src/injectReducer.js index 8b67a34..e4a9c8c 100644 --- a/src/injectReducer.js +++ b/src/injectReducer.js @@ -1,23 +1,23 @@ // @flow // Assumings keys came from 'path.to.reducer'.split('.'), or some equivalent nesting structure // Recurse in reducers until we find where this one should go. -const replaceAsyncReducers = (reducers: Object, keys: Array, reducer: Function) => { - let key = keys.shift(); - if (keys.length === 0) { - reducers[key] = reducer; - return; - } - if (reducers[key] === undefined) { - reducers[key] = {}; - } - let nextReducers = reducers[key]; - replaceAsyncReducers(nextReducers, keys, reducer); +function replaceAsyncReducers(reducers: Object, keys: Array, reducer: Function) { + let key = keys.shift(); + if (keys.length === 0) { + reducers[key] = reducer; + return; + } + if (reducers[key] === undefined) { + reducers[key] = {}; + } + let nextReducers = reducers[key]; + replaceAsyncReducers(nextReducers, keys, reducer); }; const dynamicReducers = {}; const makeReducerInjector = (store: { replaceReducer: Function }, createRootReducer: Function) => (keys: Array, reducer: *) => { - replaceAsyncReducers(dynamicReducers, keys, reducer); - store.replaceReducer(createRootReducer(dynamicReducers)); + replaceAsyncReducers(dynamicReducers, keys, reducer); + store.replaceReducer(createRootReducer(dynamicReducers)); }; export default makeReducerInjector; diff --git a/src/utils/noConfigurationWarning.js b/src/utils/noConfigurationWarning.js index af76877..16a1477 100644 --- a/src/utils/noConfigurationWarning.js +++ b/src/utils/noConfigurationWarning.js @@ -1,7 +1,7 @@ // @flow import warning from './warning'; -const noConfigurationWarning = () => { +const noConfigurationWarning = function() { if (process.env.NODE_ENV !== 'production') { warning('You must call configureRags(store, createRootReducer) to use redux-rags!'); } diff --git a/test/jest.transform.js b/test/jest.transform.js index f3d809f..e9dcafb 100644 --- a/test/jest.transform.js +++ b/test/jest.transform.js @@ -2,7 +2,6 @@ module.exports = require('babel-jest').createTransformer({ presets: ['flow', 'env', 'stage-3'], plugins: [ 'babel-plugin-transform-runtime', - 'babel-plugin-transform-class-properties', 'syntax-dynamic-import', 'babel-plugin-dynamic-import-node', ]