Skip to content

Commit 08f504b

Browse files
committed
Refactor index.js
1 parent 07cb031 commit 08f504b

File tree

1 file changed

+58
-47
lines changed

1 file changed

+58
-47
lines changed

src/index.js

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,29 @@ import defaults from './defaults';
2121
*
2222
* @returns {function} logger middleware
2323
*/
24-
function createLogger(options = {}) {
25-
const loggerOptions = Object.assign({}, defaults, options);
24+
function directlyApplied(options) {
25+
return options.getState && options.dispatch;
26+
}
2627

27-
const {
28-
logger,
29-
stateTransformer,
30-
errorTransformer,
31-
predicate,
32-
logErrors,
33-
diffPredicate,
34-
} = loggerOptions;
28+
function noLogger(options) {
29+
return typeof options.logger === 'undefined';
30+
}
3531

36-
// Return if 'console' object is not defined
37-
if (typeof logger === 'undefined') {
38-
return () => next => action => next(action);
39-
}
32+
function shouldNotLog({ predicate }, getState, action) {
33+
return typeof predicate === 'function' && !predicate(getState, action);
34+
}
4035

41-
// Detect if 'createLogger' was passed directly to 'applyMiddleware'.
42-
if (options.getState && options.dispatch) {
43-
// eslint-disable-next-line no-console
44-
console.error(`[redux-logger] redux-logger not installed. Make sure to pass logger instance as middleware:
36+
function shouldDiff({ diff, diffPredicate }, getState, action) {
37+
return diff && typeof diffPredicate === 'function' && diffPredicate(getState, action);
38+
}
39+
40+
function emptyLogger() {
41+
return () => next => action => next(action);
42+
}
43+
44+
function emptyLoggerWarning() {
45+
// eslint-disable-next-line no-console
46+
console.error(`[redux-logger] redux-logger not installed. Make sure to pass logger instance as middleware:
4547
// Logger with default options
4648
import { logger } from 'redux-logger'
4749
const store = createStore(
@@ -58,49 +60,58 @@ const store = createStore(
5860
applyMiddleware(logger)
5961
)
6062
`);
63+
}
6164

62-
return () => next => action => next(action);
65+
function createLogger(options = {}) {
66+
// Detect if 'createLogger' was passed directly to 'applyMiddleware'.
67+
if (directlyApplied(options)) {
68+
emptyLoggerWarning();
69+
return emptyLogger();
6370
}
6471

65-
const logBuffer = [];
72+
// Return if 'console' object is not defined
73+
if (noLogger(options)) return emptyLogger();
74+
75+
const loggerOptions = Object.assign({}, defaults, options):
6676

6777
return ({ getState }) => next => (action) => {
6878
// Exit early if predicate function returns 'false'
69-
if (typeof predicate === 'function' && !predicate(getState, action)) {
70-
return next(action);
71-
}
72-
73-
const logEntry = {};
74-
75-
logBuffer.push(logEntry);
76-
77-
logEntry.started = timer.now();
78-
logEntry.startedTime = new Date();
79-
logEntry.prevState = stateTransformer(getState());
80-
logEntry.action = action;
79+
if (shouldNotLog(getState, action)) return next(action);
8180

81+
const started = timer.now();
82+
const startedTime = new Date();
83+
const prevState = loggerOptions.stateTransformer(getState());
8284
let returnedValue;
83-
if (logErrors) {
84-
try {
85-
returnedValue = next(action);
86-
} catch (e) {
87-
logEntry.error = errorTransformer(e);
88-
}
89-
} else {
85+
let error;
86+
87+
try {
9088
returnedValue = next(action);
89+
} catch (e) {
90+
if (loggerOptions.logErrors) {
91+
error = loggerOptions.errorTransformer(e);
92+
} else {
93+
throw e;
94+
}
9195
}
9296

93-
logEntry.took = timer.now() - logEntry.started;
94-
logEntry.nextState = stateTransformer(getState());
97+
const took = timer.now() - started;
98+
const nextState = loggerOptions.stateTransformer(getState());
9599

96-
const diff = loggerOptions.diff && typeof diffPredicate === 'function'
97-
? diffPredicate(getState, action)
98-
: loggerOptions.diff;
100+
loggerOptions.diff = shouldDiff(loggerOptions, getState, action);
99101

100-
printBuffer(logBuffer, Object.assign({}, loggerOptions, { diff }));
101-
logBuffer.length = 0;
102+
printBuffer(
103+
[{
104+
started,
105+
startedTime,
106+
prevState,
107+
action,
108+
error,
109+
took,
110+
nextState,
111+
}],
112+
loggerOptions);
102113

103-
if (logEntry.error) throw logEntry.error;
114+
if (error) throw error;
104115
return returnedValue;
105116
};
106117
}

0 commit comments

Comments
 (0)