/
index.js
144 lines (129 loc) · 3.94 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const noop = () => {};
const defaultColors = {
success: 'green',
info: 'orange',
error: 'red',
text: 'inherit',
subText: 'gray'
};
const toFnName = (entity, fn) => `${entity.name}.${fn.name}`;
/* eslint-disable no-undef */
/* eslint-disable max-len */
const timer = (typeof performance !== 'undefined' && performance !== null) && typeof performance.now === 'function' ? performance : Date;
/* eslint-enable no-undef */
/* eslint-enable max-len */
const round = (value, decimals) => {
const rounded = Math.round(`${value}e${decimals}`);
return Number(`${rounded}e-${decimals}`);
};
const getDuration = (start) => round((timer.now() - start) / 1000, 3);
const inBoldStyle = (color) => `color: ${color}; font-weight: bold;`;
const toTimeText = (duration) => `in ${duration}ms`;
const logGroup = (impl, collapse, ...args) => {
if (collapse) {
impl.groupCollapsed(...args);
} else {
impl.group(...args);
}
};
const logResult = (impl, formattedText, titleColor, colors, collapse) => {
const args = [
formattedText,
inBoldStyle(colors.subText),
inBoldStyle(titleColor),
inBoldStyle(colors.subText)
];
logGroup(impl, collapse, ...args);
};
const createLogger = (impl, disabled, collapse, colors, noFormat) => {
if (disabled) {
return {
logChange: noop,
logSetup: noop,
logResolve: noop,
logReject: noop
};
}
return {
logSetup: (entityConfigs, config) => {
if (noFormat) {
impl.log('Ladda setup with entityConfigs', entityConfigs, 'and global config', config);
} else {
logGroup(impl, collapse, 'Ladda setup running');
impl.log('entity configs', entityConfigs);
impl.log('global config', config);
impl.groupEnd();
}
},
logChange: (change) => {
const text = 'Ladda cache change';
if (noFormat) {
impl.log(text, change);
} else {
const args = [
`%c${text} %c${change.type} ${change.entity}`,
inBoldStyle(colors.subText),
inBoldStyle(colors.info)
];
logGroup(impl, collapse, ...args);
impl.log('entities', change.entities);
impl.groupEnd();
}
},
logResolve: (fnName, start, res, args) => {
const text = 'Ladda resolved';
const timeText = toTimeText(getDuration(start));
if (noFormat) {
impl.log(`${text} ${fnName} ${timeText} with`, res, 'from args', args);
} else {
const title = `%c${text} %c${fnName} %c${timeText}`;
logResult(impl, title, colors.success, colors, collapse);
impl.log('args', args);
impl.log('res', res);
impl.groupEnd();
}
},
logReject: (fnName, startTime, start, err, args) => {
const text = 'Ladda rejected';
const timeText = toTimeText(startTime, getDuration(start));
if (noFormat) {
impl.log(`${text} ${fnName} ${timeText} with err`, err, 'from args', args);
} else {
const title = `%c${text} %c${fnName} %c@ ${timeText}`;
logResult(impl, title, colors.error, colors, collapse);
impl.log('args', args);
impl.log('err', err);
impl.groupEnd();
}
}
};
};
export const logger = ({
disable = false,
collapse = true,
colors = defaultColors,
implementation = console,
noFormat = false
} = {}) => {
const l = createLogger(implementation, disable, collapse, colors, noFormat);
return ({ addListener, entityConfigs, config }) => {
addListener((change) => l.logChange(change));
l.logSetup(entityConfigs, config);
return ({ entity, fn }) => {
const fnName = toFnName(entity, fn);
return (...args) => {
const start = timer.now();
return fn(...args).then(
(res) => {
l.logResolve(fnName, start, res, args);
return res;
},
(err) => {
l.logReject(fnName, start, err, args);
return Promise.reject(err);
}
);
};
};
};
};