Skip to content

Commit

Permalink
added support for selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
anish000kumar committed Apr 20, 2018
1 parent 5940d47 commit 2d18704
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 281 deletions.
43 changes: 43 additions & 0 deletions dist/composeEnhancers.js
@@ -0,0 +1,43 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.default = composeEnhancers;

var _redux = require("redux");

function composeEnhancers(config) {
/*
detect the environment to decide whether or not to plug in dev tools.
In react process.env.NODE_ENV refelcts the environment
while in react-native __DEV__ flag reflects the same
*/
var devTools = (typeof window === "undefined" ? "undefined" : _typeof(window)) === "object" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;

var enableDevTools = function enableDevTools() {
var devMode = false;
//check if it's development mode in react-native
if (typeof __DEV__ === "boolean" && __DEV__) {
devMode = true;
}
//check if it's development mode in react
else if ((typeof process === "undefined" ? "undefined" : _typeof(process)) == "object" && process.env && process.env.NODE_ENV && process.env.NODE_ENV === "development") {
devMode = true;
}

if (config && config.enableDevTools) {
return config.enableDevTools(devMode);
}
return devMode;
};

var composeEnhancers = enableDevTools() ? devTools || _redux.compose : _redux.compose;
if (config && config.composeRedux) {
return config.composeRedux(composeEnhancers);
}
return composeEnhancers;
}
87 changes: 87 additions & 0 deletions dist/connectStore.js
@@ -0,0 +1,87 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.default = connectStore;

var _reactRedux = require('react-redux');

var _helpers = require('./helpers');

var attachModuleSelectors = function attachModuleSelectors(moduleInstance, stateObj, state, props) {
var module = null;
if (moduleInstance.module && moduleInstance.get) {
module = moduleInstance.module;
} else {
module = moduleInstance;
}

if (_typeof(module.selectors) == 'object') {
Object.keys(module.selectors).forEach(function (selector_name) {
var selector = module.selectors[selector_name];
stateObj[selector_name] = selector(state[module.name], state);
});
}

return stateObj;
};

/*
Connect a component to any module
TODO: namespacing
*/
function connectStore(modules) {
var mapStateToProps = function mapStateToProps(state, props) {
var finalState = {};
Object.keys(modules).forEach(function (key) {
var moduleInstance = modules[key];
var module_name = moduleInstance.module && moduleInstance.module.name || moduleInstance.name;
var stateObj = state[module_name];
if (moduleInstance.get) {
var filter_array = moduleInstance.get.split(",");
stateObj = (0, _helpers.pluck)(stateObj, filter_array);
}
stateObj = attachModuleSelectors(moduleInstance, stateObj, state, props);
finalState[key] = stateObj;
});
return finalState;
};

var mapDispatchToProps = function mapDispatchToProps(dispatch) {
var finalProps = {};
Object.keys(modules).forEach(function (key) {
var moduleInstance = modules[key];
var actions_obj = {};
var module_actions = moduleInstance.module && moduleInstance.module.actions || moduleInstance.actions;
if (module_actions) {
Object.keys(module_actions).forEach(function (action_key) {
var action = module_actions[action_key];
actions_obj[action_key] = function () {
return dispatch(action.apply(undefined, arguments));
};
});
finalProps[key] = actions_obj;
}
});
return finalProps;
};
var mergeProps = function mergeProps(state, actions, ownProps) {
var finalModule = {};
Object.keys(state).forEach(function (key) {
var module_state = state[key];
var module_actions = actions[key];
finalModule[key] = Object.assign({}, module_state, module_actions);
});
return Object.assign({}, finalModule, ownProps);
};
return (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps, {
pure: true,
areStatePropsEqual: function areStatePropsEqual(a, b) {
return (0, _helpers.areSame)(a, b);
}
});
};
28 changes: 28 additions & 0 deletions dist/createContainer.js
@@ -0,0 +1,28 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = createContainer;

var _reactRedux = require("react-redux");

/*
utility to access the store using render function
*/
function createContainer(module) {
var mapStateToProps = function mapStateToProps(state) {
return state[module.name];
};
var mapDispatchToProps = function mapDispatchToProps(dispatch) {
return Object.keys(module.actions).map(function (key) {
var action = module.actions[key];
return dispatch(action());
});
};

var Container = function Container(props) {
return props.children(props);
};
return (0, _reactRedux.connect)(mapStateToProps, module.actions || {})(Container);
};
68 changes: 68 additions & 0 deletions dist/createSagas.js
@@ -0,0 +1,68 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = createSagas;

var _effects = require("redux-saga/effects");

/*
Syntactic sugar for easily accessing sagas
*/
function createSagas(saga_list) {
var arr = [];
var GeneratorFunction = Object.getPrototypeOf( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
case "end":
return _context.stop();
}
}
}, _callee, this);
})).constructor;
var saga_keys = Object.keys(saga_list);
saga_keys.forEach(function (key) {
var action = key.split(".")[0];
var worker_saga = saga_list[key];
var mode = key.split(".")[1] || "latest";
var watcher = null;
if (mode == "latest") {
watcher = /*#__PURE__*/regeneratorRuntime.mark(function watcher() {
return regeneratorRuntime.wrap(function watcher$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return (0, _effects.takeLatest)(action, worker_saga);

case 2:
case "end":
return _context2.stop();
}
}
}, watcher, this);
});
} else if (mode == "every") {
watcher = /*#__PURE__*/regeneratorRuntime.mark(function watcher() {
return regeneratorRuntime.wrap(function watcher$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return (0, _effects.takeEvery)(action, worker_saga);

case 2:
case "end":
return _context3.stop();
}
}
}, watcher, this);
});
}
arr.push(watcher());
});
return arr;
};
116 changes: 116 additions & 0 deletions dist/createStore.js
@@ -0,0 +1,116 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = createStore;

var _composeEnhancers = require("./composeEnhancers");

var _composeEnhancers2 = _interopRequireDefault(_composeEnhancers);

var _reduxSaga = require("redux-saga");

var _reduxSaga2 = _interopRequireDefault(_reduxSaga);

var _effects = require("redux-saga/effects");

var _reducer = require("./reducer");

var _reducer2 = _interopRequireDefault(_reducer);

var _redux = require("redux");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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); } }

/*
Iterate through each module and keep stacking our reducers
and sagas in their respective arrays. Finally
we use these arrays to initialize the store using
'createStore' from redux.
*/
function createStore(modules) {
var _marked = /*#__PURE__*/regeneratorRuntime.mark(rootSaga);

var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};


//Initialize middleware array
var sagaMiddleware = (0, _reduxSaga2.default)();
var middlewares = [sagaMiddleware];

//push the provided middlewares in config object, to the middleware array
if (config && config.middlewares && config.middlewares.length > 0) {
middlewares = middlewares.concat(config.middlewares);
}
var reducerList = Object.assign({}, config.reducers);
var sagas = [];

//iterate through each module and push the sagas and reducers of each module in thier respective array
modules.forEach(function (module) {
sagas = sagas.concat(module.sagas);
var moduleReducer = (0, _reducer2.default)(module.mutations, module.state, module.name);
if (module.decorateReducer) moduleReducer = module.decorateReducer(moduleReducer);
reducerList[module.name] = moduleReducer;
});
config.sagas && config.sagas.forEach(function (saga) {
return sagas.concat(saga);
});

var combinedReducer = (0, _redux.combineReducers)(reducerList);
if (config.decorateReducer) {
combinedReducer = config.decorateReducer(combinedReducer);
}
var preloadedState = config.preloadedState || {};
var composeRedux = (0, _composeEnhancers2.default)(config);
//initialize the store using preloaded state, reducers and middlewares
var store = (0, _redux.createStore)(combinedReducer, preloadedState, composeRedux(_redux.applyMiddleware.apply(undefined, _toConsumableArray(middlewares))));

// Default configuration for sagas
var sagaConfig = Object.assign({}, {
retryDelay: 2000,
onError: function onError(err) {}
}, config.sagaConfig);

function rootSaga() {
return regeneratorRuntime.wrap(function rootSaga$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!true) {
_context.next = 13;
break;
}

_context.prev = 1;
_context.next = 4;
return (0, _effects.all)(sagas);

case 4:
_context.next = 11;
break;

case 6:
_context.prev = 6;
_context.t0 = _context["catch"](1);

sagaConfig.onError(_context.t0);
_context.next = 11;
return (0, _effects.call)(_reduxSaga.delay, sagaConfig.retryDelay);

case 11:
_context.next = 0;
break;

case 13:
case "end":
return _context.stop();
}
}
}, _marked, this, [[1, 6]]);
}
sagaMiddleware.run(rootSaga);
return store;
};
29 changes: 9 additions & 20 deletions dist/helpers.js
Expand Up @@ -70,33 +70,22 @@ var Shallowdiffers = function Shallowdiffers(a, b) {

var doubleDiffers = function doubleDiffers(a, b) {
for (var i in a) {
if (!(i in b)) return true;
if (!(i in b)) {
return true;
}
}for (var _i2 in b) {
if (_typeof(a[_i2]) == 'object' && _typeof(b[_i2]) == 'object') {
if (Shallowdiffers(a[_i2], b[_i2])) return true;
} else if (a[_i2] !== b[_i2]) return true;
if (Shallowdiffers(a[_i2], b[_i2])) {
return true;
}
} else if (a[_i2] !== b[_i2]) {
return true;
}
}
return false;
};

var areSame = exports.areSame = function areSame(a, b) {
var x = doubleDiffers(a, b);
return !x;
};

/*
utility to reset the state of any module
(to it's default state)
*/
var resetModules = exports.resetModules = function resetModules(dispatch) {
return function () {
var modules = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

for (var i = 0; i < modules.length; i++) {
var module = modules[i];
dispatch({
type: module.name + "__RESET__"
});
}
};
};

0 comments on commit 2d18704

Please sign in to comment.