-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
342 lines (283 loc) · 8.12 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*eslint-disable no-console*/
import { SagaCancellationException } from 'redux-saga'
import {
monitorActions as actions,
is, asEffect,
MANUAL_CANCEL
} from 'redux-saga/utils'
const PENDING = 'PENDING'
const RESOLVED = 'RESOLVED'
const REJECTED = 'REJECTED'
export const LOG_EFFECT = 'LOG_EFFECT'
export const logEffect = (effectId = 0) => ({type: LOG_EFFECT, effectId})
const DEFAULT_STYLE = 'color: black'
const LABEL_STYLE = 'font-weight: bold'
const EFFECT_TYPE_STYLE = 'color: blue'
const ERROR_STYLE = 'color: red'
const AUTO_CANCEL_STYLE = 'color: lightgray'
const time = () => performance.now()
const env = process.env.NODE_ENV
function checkEnv() {
if (env !== 'production' && env !== 'development') {
console.error('Saga Monitor cannot be used outside of NODE_ENV === \'development\'. ' +
'Consult tools such as loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
'and DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) ' +
'to build with proper NODE_ENV')
return next(action)
}
}
let effectsById = {}
export default () => next => action => {
switch (action.type) {
case actions.EFFECT_TRIGGERED:
effectsById[action.effectId] = Object.assign({},
action,
{
status: PENDING,
start: time()
}
)
break;
case actions.EFFECT_RESOLVED:
resolveEffect(action.effectId, action.result)
break;
case actions.EFFECT_REJECTED:
rejectEffect(action.effectId, action.error)
break;
case LOG_EFFECT:
checkEnv()
logEffectTree(action.effectId || 0)
break;
default:
return next(action)
}
}
window.$$LogSagas = () => logEffectTree(0)
function resolveEffect(effectId, result) {
const effect = effectsById[effectId]
const now = time()
if(is.task(result)) {
result.done.then(
taskResult => resolveEffect(effectId, taskResult),
taskError => rejectEffect(effectId, taskError)
)
} else {
effectsById[effectId] = Object.assign({},
effect,
{
result: result,
status: RESOLVED,
end: now,
duration: now - effect.start
}
)
if(effect && asEffect.race(effect.effect))
setRaceWinner(effectId, result)
}
}
function rejectEffect(effectId, error) {
const effect = effectsById[effectId]
const now = time()
effectsById[effectId] = Object.assign({},
effect,
{
error: error,
status: REJECTED,
end: now,
duration: now - effect.start
}
)
if(effect && asEffect.race(effect.effect))
setRaceWinner(effectId, error)
}
function setRaceWinner(raceEffectId, result) {
const winnerLabel = Object.keys(result)[0]
const children = getChildEffects(raceEffectId)
for (var i = 0; i < children.length; i++) {
const childEffect = effectsById[ children[i] ]
if(childEffect.label === winnerLabel)
childEffect.winner = true
}
}
function getChildEffects(parentEffectId) {
return Object.keys(effectsById)
.filter(effectId => effectsById[effectId].parentEffectId === parentEffectId)
.map(effectId => +effectId)
}
function logEffectTree(effectId) {
const effect = effectsById[effectId]
if(effectId === undefined) {
console.log('Saga monitor: No effect data for', effectId)
return
}
const childEffects = getChildEffects(effectId)
if(!childEffects.length)
logSimpleEffect(effect)
else {
if(effect) {
const {formatter} = getEffectLog(effect)
console.group(...formatter.getLog())
} else
console.group('root')
childEffects.forEach(logEffectTree)
console.groupEnd()
}
}
function logSimpleEffect(effect) {
const {method, formatter} = getEffectLog(effect)
console[method](...formatter.getLog())
}
/*eslint-disable no-cond-assign*/
function getEffectLog(effect) {
let data, log
if(data = asEffect.take(effect.effect)) {
log = getLogPrefix('take', effect)
log.formatter.addValue(data)
logResult(effect, log.formatter)
}
else if(data = asEffect.put(effect.effect)) {
log = getLogPrefix('put', effect)
logResult(Object.assign({}, effect, { result: data }), log.formatter)
}
else if(data = asEffect.call(effect.effect)) {
log = getLogPrefix('call', effect)
log.formatter.addCall(data.fn.name, data.args)
logResult(effect, log.formatter)
}
else if(data = asEffect.cps(effect.effect)) {
log = getLogPrefix('cps', effect)
log.formatter.addCall(data.fn.name, data.args)
logResult(effect, log.formatter)
}
else if(data = asEffect.fork(effect.effect)) {
log = getLogPrefix('', effect)
log.formatter.addCall(data.fn.name, data.args)
logResult(effect, log.formatter)
}
else if(data = asEffect.join(effect.effect)) {
log = getLogPrefix('join', effect)
logResult(effect, log.formatter)
}
else if(data = asEffect.race(effect.effect)) {
log = getLogPrefix('race', effect)
logResult(effect, log.formatter, true)
}
else if(data = asEffect.cancel(effect.effect)) {
log = getLogPrefix('cancel', effect)
log.formatter.appendData(data.name)
}
else if(data = is.array(effect.effect)) {
log = getLogPrefix('parallel', effect)
logResult(effect, log.formatter, true)
}
else {
log = getLogPrefix('unkown', effect)
logResult(effect, log.formatter)
}
return log
}
function getLogPrefix(type, effect) {
const autoCancel = isAutoCancel(effect.error)
const isError = effect && effect.status === REJECTED && !autoCancel
const method = isError ? 'error' : 'log'
const winnerInd = effect && effect.winner
? ( isError ? '✘' : '✓' )
: ''
const style = s =>
autoCancel ? AUTO_CANCEL_STYLE
: isError ? ERROR_STYLE
: s
const formatter = logFormatter()
if(winnerInd)
formatter.add(`%c ${winnerInd}`, style(LABEL_STYLE))
if(effect && effect.label)
formatter.add(`%c ${effect.label}: `, style(LABEL_STYLE))
if(type)
formatter.add(`%c ${type} `, style(EFFECT_TYPE_STYLE))
formatter.add('%c', style(DEFAULT_STYLE))
return {
method,
formatter
}
}
function argToString(arg) {
return (
typeof arg === 'function' ? `${arg.name}`
: typeof arg === 'string' ? `'${arg}'`
: arg
)
}
function logResult({status, result, error, duration}, formatter, ignoreResult) {
if(status === RESOLVED && !ignoreResult) {
if( is.array(result) ) {
formatter.addValue(' → ')
formatter.addValue(result)
} else
formatter.appendData('→',result)
}
else if(status === REJECTED) {
if(isAutoCancel(error))
return
if(isManualCancel(error))
formatter.appendData('→ ⚠', 'Cancelled!')
else
formatter.appendData('→ ⚠', error)
}
else if(status === PENDING)
formatter.appendData('⌛')
if(status !== PENDING)
formatter.appendData(`(${duration.toFixed(2)}ms)`)
}
function isAutoCancel(error) {
return error instanceof SagaCancellationException && error.type !== MANUAL_CANCEL
}
function isManualCancel(error) {
return error instanceof SagaCancellationException && error.type === MANUAL_CANCEL
}
function isPrimitive(val) {
return typeof val === 'string' ||
typeof val === 'number' ||
typeof val === 'boolean' ||
typeof val === 'symbol' ||
val === null ||
val === undefined;
}
function logFormatter() {
const logs = []
let suffix = []
function add(msg, ...args) {
logs.push({msg, args})
}
function appendData(...data) {
suffix = suffix.concat(data)
}
function addValue(value) {
if(isPrimitive(value))
add(value)
else
add('%O', value)
}
function addCall(name, args) {
if(!args.length)
add( `${name}()` )
else {
add(name)
add('(')
args.forEach( (arg, i) => {
addValue( argToString(arg) )
addValue( i === args.length - 1 ? ')' : ', ')
})
}
}
function getLog() {
let msgs = [], msgsArgs = []
for (var i = 0; i < logs.length; i++) {
msgs.push(logs[i].msg)
msgsArgs = msgsArgs.concat(logs[i].args)
}
return [msgs.join('')].concat(msgsArgs).concat(suffix)
}
return {
add, addValue, addCall, appendData, getLog
}
}