-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathindex.js
107 lines (90 loc) · 3.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import printBuffer from './core';
import { timer } from './helpers';
import defaults from './defaults';
/* eslint max-len: ["error", 110, { "ignoreComments": true }] */
/**
* Creates logger with following options
*
* @namespace
* @param {object} options - options for logger
* @param {string | function | object} options.level - console[level]
* @param {boolean} options.duration - print duration of each action?
* @param {boolean} options.timestamp - print timestamp with each action?
* @param {object} options.colors - custom colors
* @param {object} options.logger - implementation of the `console` API
* @param {boolean} options.logErrors - should errors in action execution be caught, logged, and re-thrown?
* @param {boolean} options.collapsed - is group collapsed?
* @param {boolean} options.predicate - condition which resolves logger behavior
* @param {function} options.stateTransformer - transform state before print
* @param {function} options.actionTransformer - transform action before print
* @param {function} options.errorTransformer - transform error before print
*
* @returns {function} logger middleware
*/
function createLogger(options = {}) {
const loggerOptions = Object.assign({}, defaults, options);
const {
logger,
stateTransformer,
errorTransformer,
predicate,
diffPredicate, // TODO: where description?
} = loggerOptions;
// Return if 'console' object is not defined
if (typeof logger === 'undefined') {
return () => next => action => next(action);
}
// Detect if 'createLogger' was passed directly to 'applyMiddleware'.
if (options.getState && options.dispatch) {
// eslint-disable-next-line no-console
console.error(`[redux-logger] redux-logger not installed. Make sure to pass logger instance as middleware:
// Logger with default options
import { logger } from 'redux-logger'
const store = createStore(
reducer,
applyMiddleware(logger)
)
// Or you can create your own logger with custom options http://bit.ly/redux-logger-options
import { createLogger } from 'redux-logger'
const logger = createLogger({
// ...options
});
const store = createStore(
reducer,
applyMiddleware(logger)
)
`);
return mainReducer => (state, action) => mainReducer(state, action);
}
const logBuffer = [];
return mainReducer => (state, action) => {
// Exit early if predicate function returns 'false'
if (typeof predicate === 'function' && !predicate(state, action)) {
return mainReducer(state, action);
}
const logEntry = {};
logBuffer.push(logEntry);
logEntry.started = timer.now();
logEntry.startedTime = new Date();
logEntry.prevState = stateTransformer(state);
logEntry.action = action;
// TODO: make logErrors always true?
try {
logEntry.nextState = mainReducer(state, action);
} catch (e) {
logEntry.error = errorTransformer(e);
}
logEntry.took = timer.now() - logEntry.started;
const diff = loggerOptions.diff && typeof diffPredicate === 'function'
? diffPredicate(state, action)
: loggerOptions.diff;
printBuffer(logBuffer, Object.assign({}, loggerOptions, { diff }));
logBuffer.length = 0;
if (logEntry.error) throw logEntry.error;
return logEntry.nextState;
};
}
// eslint-disable-next-line consistent-return
const defaultLogger = mainReducer => createLogger()(mainReducer);
export { defaults, createLogger, defaultLogger as logger };
export default defaultLogger;