Browse Source

`key | selectorFn` in sub{Middleware,Effex}.

Herbert Vojčík 7 years ago
parent
commit
aa710f07c8
3 changed files with 30 additions and 21 deletions
  1. 14 4
      README.md
  2. 8 12
      lib/sac.js
  3. 8 5
      src/sac.js

+ 14 - 4
README.md

@@ -60,10 +60,11 @@ composeReducers(
 )
 ```
 
-### `subMiddleware(key, middleware)`
+### `subMiddleware(key | selectorFn, middleware)`
 
 Creates a wrapper middleware
 on the sub-state specified by `key`
+or the selector function `selectorFn`
 by decorating `getState` part of the passed arg.
 You may use dot notation.
 Rest of the passed arg is left untouched.
@@ -74,7 +75,12 @@ const r = subMiddleware("persons", ({getState}) => next => action => {
   return next(action);
 });
   
-// use r in creation of store
+const altR = subMiddleware(state => state.persons, ({getState}) => next => action => {
+  console.log(JSON.stringify(getState()));
+  return next(action);
+});
+  
+// use r or altR in creation of store
   
 store.getState();
 // => {persons: ["John", "Jill"], cars: ["Honda"]}
@@ -91,11 +97,12 @@ one of the properties passed is `getState` function;
 it is not special-case for middleware.
 For example, it is usable for wrapping `redux-effex` effect function.
 
-### `subEffex(key, effects)`
+### `subEffex(key | selectorFn, effects)`
 
 Creates a wrapper around each element
 of the effects array
 on the sub-state specified by `key`
+or by selector function `selectorFn`
 by decorating `effect` function with `subMiddleware`
 and returns the array of the wrapped effects.
 
@@ -107,7 +114,10 @@ const effects = [{
   }
 }];
   
-// use effects in creation of store
+const e = subEffex("persons", effects);
+const altE = subEffex(state => state.persons, effects);
+  
+// use e or altE in creation of store
   
 store.getState();
 // => {persons: ["John", "Jill"], cars: ["Honda"]}

+ 8 - 12
lib/sac.js

@@ -9,8 +9,6 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < argument
 
 var _cowValueModel = require("./cow-value-model");
 
-function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
-
 function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
 
 var subReducer = exports.subReducer = function subReducer(key, reducer) {
@@ -46,21 +44,19 @@ var composeReducers = exports.composeReducers = function composeReducers() {
     };
 };
 
-var subMiddleware = function subMiddleware(key, middleware) {
-    var cow = (0, _cowValueModel.cowValueModel)(key);
-    return function (_ref) {
-        var _getState = _ref.getState,
-            rest = _objectWithoutProperties(_ref, ["getState"]);
+var subMiddleware = exports.subMiddleware = function subMiddleware(keyOrSelectorFn, middleware) {
+    var cow = typeof keyOrSelectorFn === "function" ? keyOrSelectorFn : (0, _cowValueModel.cowValueModel)(keyOrSelectorFn);
+    return function (store) {
+        var _getState = store.getState;
 
-        return middleware(_extends({ getState: function getState() {
+        return middleware(_extends({}, store, { getState: function getState() {
                 return cow(_getState());
-            } }, rest));
+            } }));
     };
 };
 
-exports.subMiddleware = subMiddleware;
-var subEffex = exports.subEffex = function subEffex(key, effects) {
+var subEffex = exports.subEffex = function subEffex(keyOrSelectorFn, effects) {
     return effects.map(function (each) {
-        return _extends({}, each, { effect: subMiddleware(key, each.effect) });
+        return _extends({}, each, { effect: subMiddleware(keyOrSelectorFn, each.effect) });
     });
 };

+ 8 - 5
src/sac.js

@@ -16,10 +16,13 @@ export const subReducer = (key, reducer, ...otherKeys) => {
 export const composeReducers = (...reducers) => (state, action) =>
     reducers.reduce((x, r) => r(x, action), state);
 
-export const subMiddleware = (key, middleware) => {
-    const cow = cowValueModel(key);
-    return ({getState, ...rest}) => middleware({getState: () => cow(getState()), ...rest});
+export const subMiddleware = (keyOrSelectorFn, middleware) => {
+    const cow = typeof keyOrSelectorFn === "function" ? keyOrSelectorFn : cowValueModel(keyOrSelectorFn);
+    return store => {
+        const {getState} = store;
+        return middleware({...store, getState: () => cow(getState())});
+    };
 };
 
-export const subEffex = (key, effects) =>
-    effects.map(each => ({...each, effect: subMiddleware(key, each.effect)}));
+export const subEffex = (keyOrSelectorFn, effects) =>
+    effects.map(each => ({...each, effect: subMiddleware(keyOrSelectorFn, each.effect)}));