Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lets wrap reducer #276

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
21 changes: 12 additions & 9 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sinon from 'sinon';
import { applyMiddleware, createStore } from 'redux';
import { default as logger, createLogger } from '../src';
import { createStore } from 'redux';
import logger, { createLogger } from '../src';

context('default logger', () => {
describe('init', () => {
Expand All @@ -13,7 +13,8 @@ context('default logger', () => {
});

it('should be ok', () => {
const store = createStore(() => ({}), applyMiddleware(logger));
const mainReducer = () => ({});
const store = createStore(logger(mainReducer));

store.dispatch({ type: 'foo' });
sinon.assert.notCalled(console.error);
Expand All @@ -31,15 +32,17 @@ context('createLogger', () => {
console.error.restore();
});

it('should throw error if passed direct to applyMiddleware', () => {
const store = createStore(() => ({}), applyMiddleware(createLogger));
// it('should throw error if passed mainReducer', () => {
// const mainReducer = () => ({})
// const store = createStore(createLogger(mainReducer));

store.dispatch({ type: 'foo' });
sinon.assert.calledOnce(console.error);
});
// store.dispatch({ type: 'foo' });
// sinon.assert.calledOnce(console.error);
// });

it('should be ok', () => {
const store = createStore(() => ({}), applyMiddleware(createLogger()));
const mainReducer = () => ({});
const store = createStore(createLogger()(mainReducer));

store.dispatch({ type: 'foo' });
sinon.assert.notCalled(console.error);
Expand Down
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function printBuffer(buffer, options) {
}

if (logger.withTrace) {
logger.groupCollapsed(`TRACE`);
logger.groupCollapsed('TRACE');
logger.trace();
logger.groupEnd();
}
Expand Down
47 changes: 14 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ function createLogger(options = {}) {
stateTransformer,
errorTransformer,
predicate,
logErrors,
diffPredicate,
diffPredicate, // TODO: where description?
} = loggerOptions;

// Return if 'console' object is not defined
Expand Down Expand Up @@ -59,15 +58,15 @@ const store = createStore(
)
`);

return () => next => action => next(action);
return mainReducer => (state, action) => mainReducer(state, action);
}

const logBuffer = [];

return ({ getState }) => next => (action) => {
return mainReducer => (state, action) => {
// Exit early if predicate function returns 'false'
if (typeof predicate === 'function' && !predicate(getState, action)) {
return next(action);
if (typeof predicate === 'function' && !predicate(state, action)) {
return mainReducer(state, action);
}

const logEntry = {};
Expand All @@ -76,50 +75,32 @@ const store = createStore(

logEntry.started = timer.now();
logEntry.startedTime = new Date();
logEntry.prevState = stateTransformer(getState());
logEntry.prevState = stateTransformer(state);
logEntry.action = action;

let returnedValue;
if (logErrors) {
try {
returnedValue = next(action);
} catch (e) {
logEntry.error = errorTransformer(e);
}
} else {
returnedValue = next(action);
// TODO: make logErrors always true?
try {
logEntry.nextState = mainReducer(state, action);
} catch (e) {
logEntry.error = errorTransformer(e);
}

logEntry.took = timer.now() - logEntry.started;
logEntry.nextState = stateTransformer(getState());

const diff = loggerOptions.diff && typeof diffPredicate === 'function'
? diffPredicate(getState, action)
? diffPredicate(state, action)
: loggerOptions.diff;

printBuffer(logBuffer, Object.assign({}, loggerOptions, { diff }));
logBuffer.length = 0;

if (logEntry.error) throw logEntry.error;
return returnedValue;
return logEntry.nextState;
};
}

// eslint-disable-next-line consistent-return
const defaultLogger = ({ dispatch, getState } = {}) => {
if (typeof dispatch === 'function' || typeof getState === 'function') {
return createLogger()({ dispatch, getState });
}
// eslint-disable-next-line no-console
console.error(`
[redux-logger v3] BREAKING CHANGE
[redux-logger v3] Since 3.0.0 redux-logger exports by default logger with default settings.
[redux-logger v3] Change
[redux-logger v3] import createLogger from 'redux-logger'
[redux-logger v3] to
[redux-logger v3] import { createLogger } from 'redux-logger'
`);
};
const defaultLogger = mainReducer => createLogger()(mainReducer);

export { defaults, createLogger, defaultLogger as logger };

Expand Down