Skip to content

Commit 39b9bfa

Browse files
committed
chore: add option to disable diffing
1 parent 4dc963b commit 39b9bfa

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
},
1515
"scripts": {
1616
"start": "run-s start:client",
17-
"start:client": "node ./scripts/build-client",
18-
"start:devtools": "node ./scripts/build-devtools",
17+
"start:client": "cross-env NODE_ENV=development node ./scripts/build-client",
18+
"start:devtools": "cross-env NODE_ENV=development node ./scripts/build-devtools",
1919
"build": "run-s clean build:*",
2020
"build:client": "cross-env NODE_ENV=production node ./scripts/build-client",
2121
"build:server": "cross-env NODE_ENV=production node ./scripts/build-lambda",

src/lib/logger.js

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const styles = {
3131

3232
export const pad = (num, maxLength) => `${num}`.padStart(0, maxLength);
3333

34+
function omit(keys, object) {
35+
return Object.entries(object).reduce((acc, [key, value]) => {
36+
acc[key] = keys.includes(key) ? '_ignored_' : value;
37+
return acc;
38+
}, {});
39+
}
40+
3441
function formatTime(time) {
3542
return `${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(
3643
time.getSeconds(),
@@ -60,10 +67,11 @@ function renderDiff(diff) {
6067
}
6168
}
6269

70+
const logLevel = JSON.parse(localStorage.getItem('debug'));
6371
const isLoggingEnabled =
64-
process.env.NODE_ENV === 'development'
65-
? localStorage.getItem('debug') !== false
66-
: localStorage.getItem('debug') === true;
72+
process.env.NODE_ENV === 'development' ? logLevel !== false : !!logLevel;
73+
const isDiffEnabled = logLevel === 'diff';
74+
const diffIgnoreKeys = ['markupEditor', 'queryEditor', 'sandbox'];
6775

6876
export function withLogging(reducerFn) {
6977
if (!isLoggingEnabled) {
@@ -79,7 +87,6 @@ export function withLogging(reducerFn) {
7987
const newState = reducerFn(prevState, action);
8088

8189
const took = timer.now() - started;
82-
const diff = differ(prevState, newState);
8390

8491
const header = [
8592
[
@@ -94,7 +101,7 @@ export function withLogging(reducerFn) {
94101
];
95102

96103
if (supportsGroups) {
97-
console.group(...header);
104+
console.groupCollapsed(...header);
98105
} else {
99106
console.log(...header);
100107
}
@@ -103,29 +110,38 @@ export function withLogging(reducerFn) {
103110
console.log('%c action %O', styles.action, action);
104111
console.log('%c new state %O', styles.nextState, newState);
105112

106-
if (!diff) {
107-
console.log(
108-
'%c diff %cno state change!',
109-
styles.prevState,
110-
styles.error,
111-
);
113+
if (!isDiffEnabled) {
114+
console.log('%c diff %c _disabled_', styles.prevState, styles.note);
112115
} else {
113-
if (supportsGroups) {
114-
console.groupCollapsed(' diff');
115-
} else {
116-
console.log('diff');
117-
}
116+
const diff = differ(
117+
omit(diffIgnoreKeys, prevState),
118+
omit(diffIgnoreKeys, newState),
119+
);
118120

119-
diff.forEach((elem) => {
121+
if (!diff) {
120122
console.log(
121-
`%c ${styles.diff[elem.kind].text}`,
122-
styles.diff[elem.kind].style,
123-
...renderDiff(elem),
123+
'%c diff %cno state change!',
124+
styles.prevState,
125+
styles.error,
124126
);
125-
});
126-
127-
if (supportsGroups) {
128-
console.groupEnd();
127+
} else {
128+
if (supportsGroups) {
129+
console.groupCollapsed(' diff');
130+
} else {
131+
console.log('diff');
132+
}
133+
134+
diff.forEach((elem) => {
135+
console.log(
136+
`%c ${styles.diff[elem.kind].text}`,
137+
styles.diff[elem.kind].style,
138+
...renderDiff(elem),
139+
);
140+
});
141+
142+
if (supportsGroups) {
143+
console.groupEnd();
144+
}
129145
}
130146
}
131147

0 commit comments

Comments
 (0)