Skip to content
Open
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
128 changes: 69 additions & 59 deletions lib/ReduxInjector.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,102 @@
'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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.combineReducersRecurse = combineReducersRecurse;
exports.createInjectStore = createInjectStore;
exports.reloadReducer = reloadReducer;
exports.injectReducer = injectReducer;

var _redux = require('redux');

var _lodash = require('lodash');

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var store = {};
var combine = _redux.combineReducers;

function combineReducersRecurse(reducers) {
// If this is a leaf or already combined.
if (typeof reducers === 'function') {
return reducers;
}

// If this is an object of functions, combine reducers.
if ((typeof reducers === 'undefined' ? 'undefined' : _typeof(reducers)) === 'object') {
var combinedReducers = {};
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = Object.keys(reducers)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;

combinedReducers[key] = combineReducersRecurse(reducers[key]);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
// If this is a leaf or already combined.
if (typeof reducers === 'function') {
return reducers;
}

return combine(combinedReducers);
}
// If this is an object of functions, combine reducers.
if ((typeof reducers === 'undefined' ? 'undefined' : _typeof(reducers)) === 'object') {
var combinedReducers = {};
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = Object.keys(reducers)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;

combinedReducers[key] = combineReducersRecurse(reducers[key]);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

return combine(combinedReducers);
}

// If we get here we have an invalid item in the reducer path.
throw new Error({
message: 'Invalid item in reducer tree',
item: reducers
});
// If we get here we have an invalid item in the reducer path.
throw new Error({
message: 'Invalid item in reducer tree',
item: reducers
});
}

function createInjectStore(initialReducers) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}

// If last item is an object, it is overrides.
if (_typeof(args[args.length - 1]) === 'object') {
var overrides = args.pop();
// Allow overriding the combineReducers function such as with redux-immutable.
if (overrides.hasOwnProperty('combineReducers') && typeof overrides.combineReducers === 'function') {
combine = overrides.combineReducers;
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
}

store = _redux.createStore.apply(undefined, [combineReducersRecurse(initialReducers)].concat(args));
// If last item is an object, it is overrides.
if (_typeof(args[args.length - 1]) === 'object') {
var overrides = args.pop();
// Allow overriding the combineReducers function such as with redux-immutable.
if (overrides.hasOwnProperty('combineReducers') && typeof overrides.combineReducers === 'function') {
combine = overrides.combineReducers;
}
}

store.injectedReducers = initialReducers;
store = _redux.createStore.apply(undefined, [combineReducersRecurse(initialReducers)].concat(args));

return store;
store.injectedReducers = initialReducers;

return store;
}

function injectReducer(key, reducer) {
var force = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
function reloadReducer(key, reducer) {
store.replaceReducer(combineReducersRecurse(_extends({}, store.injectedReducers, _defineProperty({}, key, reducer))));
}

// If already set, do nothing.
if ((0, _lodash.has)(store.injectedReducers, key) || force) return;
function injectReducer(key, reducer) {
var force = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;

(0, _lodash.set)(store.injectedReducers, key, reducer);
store.replaceReducer(combineReducersRecurse(store.injectedReducers));
// If already set, do nothing.
if (!(0, _lodash.has)(store.injectedReducers, key) || force) {
(0, _lodash.set)(store.injectedReducers, key, reducer);
store.replaceReducer(combineReducersRecurse(store.injectedReducers));
}
}
82 changes: 43 additions & 39 deletions src/ReduxInjector.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,59 @@
import { createStore, combineReducers } from 'redux';
import { set, has } from 'lodash';
import {createStore, combineReducers} from 'redux';
import {set, has} from 'lodash';

let store = {};
let combine = combineReducers;

function combineReducersRecurse(reducers) {
// If this is a leaf or already combined.
if (typeof reducers === 'function') {
return reducers;
}

// If this is an object of functions, combine reducers.
if (typeof reducers === 'object') {
let combinedReducers = {};
for (let key of Object.keys(reducers)) {
combinedReducers[key] = combineReducersRecurse(reducers[key]);
export function combineReducersRecurse(reducers) {
// If this is a leaf or already combined.
if (typeof reducers === 'function') {
return reducers;
}
return combine(combinedReducers);
}

// If we get here we have an invalid item in the reducer path.
throw new Error({
message: 'Invalid item in reducer tree',
item: reducers
});

// If this is an object of functions, combine reducers.
if (typeof reducers === 'object') {
let combinedReducers = {};
for (let key of Object.keys(reducers)) {
combinedReducers[key] = combineReducersRecurse(reducers[key]);
}
return combine(combinedReducers);
}

// If we get here we have an invalid item in the reducer path.
throw new Error({
message: 'Invalid item in reducer tree',
item: reducers
});
}

export function createInjectStore(initialReducers, ...args) {
// If last item is an object, it is overrides.
if (typeof args[args.length - 1] === 'object') {
const overrides = args.pop();
// Allow overriding the combineReducers function such as with redux-immutable.
if (overrides.hasOwnProperty('combineReducers') && typeof overrides.combineReducers === 'function') {
combine = overrides.combineReducers;
// If last item is an object, it is overrides.
if (typeof args[args.length - 1] === 'object') {
const overrides = args.pop();
// Allow overriding the combineReducers function such as with redux-immutable.
if (overrides.hasOwnProperty('combineReducers') && typeof overrides.combineReducers === 'function') {
combine = overrides.combineReducers;
}
}
}

store = createStore(
combineReducersRecurse(initialReducers),
...args
);
store = createStore(
combineReducersRecurse(initialReducers),
...args
);

store.injectedReducers = initialReducers;
store.injectedReducers = initialReducers;

return store;
return store;
}

export function injectReducer(key, reducer, force = false) {
// If already set, do nothing.
if (has(store.injectedReducers, key) || force) return;
export function reloadReducer(key, reducer) {
store.replaceReducer(combineReducersRecurse({...store.injectedReducers, [key]: reducer}));
}

set(store.injectedReducers, key, reducer);
store.replaceReducer(combineReducersRecurse(store.injectedReducers));
export function injectReducer(key, reducer, force = false) {
// If already set, do nothing.
if (!has(store.injectedReducers, key) || force) {
set(store.injectedReducers, key, reducer);
store.replaceReducer(combineReducersRecurse(store.injectedReducers));
}
}