forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorhandling.js
More file actions
793 lines (667 loc) · 21 KB
/
Copy patherrorhandling.js
File metadata and controls
793 lines (667 loc) · 21 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
var mobx = require("../../src/mobx.ts")
var m = mobx
var utils = require("../utils/test-utils")
const { observable, computed, $mobx } = mobx
var voidObserver = function() {}
function buffer() {
var b = []
var res = function(newValue) {
b.push(newValue)
}
res.toArray = function() {
return b
}
return res
}
function checkGlobalState() {
const gs = mobx._getGlobalState()
expect(gs.isRunningReactions).toBe(false)
expect(gs.trackingDerivation).toBe(null)
expect(gs.inBatch).toBe(0)
expect(gs.allowStateChanges).toBe(!gs.strictMode)
expect(gs.pendingUnobservations.length).toBe(0)
}
test("exception1", function() {
var a = computed(function() {
throw "hoi"
})
expect(() => a.get()).toThrow(/hoi/)
checkGlobalState()
})
test("exceptions in computed values can be recovered from", () => {
var a = observable({
x: 1,
get y() {
if (this.x === 2) throw "Uhoh"
return this.x * 2
}
})
expect(a.y).toBe(2)
a.x = 2
expect(() => a.y).toThrowError(/Uhoh/)
checkGlobalState()
a.x = 3
expect(a.y).toBe(6)
checkGlobalState()
})
test("exception when starting autorun can be recovered from", () => {
var b = undefined
var a = observable({
x: 2,
get y() {
if (this.x === 2) throw "Uhoh"
return this.x * 2
}
})
utils.consoleError(() => {
mobx.autorun(() => {
b = a.y
})
}, /Uhoh/)
expect(b).toBe(undefined)
checkGlobalState()
a.x = 3
expect(b).toBe(6)
checkGlobalState()
expect(mobx.getAtom(a, "y").observers.size).toBe(1)
})
test("exception in autorun can be recovered from", () => {
var b = undefined
var a = observable({
x: 1,
get y() {
if (this.x === 2) throw "Uhoh"
return this.x * 2
}
})
var d = mobx.autorun(() => {
b = a.y
})
expect(a.y).toBe(2)
expect(b).toBe(2)
expect(mobx.getAtom(a, "y").observers.size).toBe(1)
utils.consoleError(() => {
a.x = 2
}, /Uhoh/)
// exception is also rethrown to each consumer
expect(() => {
expect(a.y).toBe(2) // old cached value!
}).toThrowError(/Uhoh/)
expect(mobx.getAtom(a, "y").observers.size).toBe(1)
expect(b).toBe(2)
checkGlobalState()
a.x = 3
expect(a.y).toBe(6)
expect(b).toBe(6)
checkGlobalState()
expect(mobx.getAtom(a, "y").observers.size).toBe(1)
d()
expect(mobx.getAtom(a, "y").observers.size).toBe(0)
})
test("multiple autoruns with exceptions are handled correctly", () => {
var a = mobx.observable.box(1)
var values = []
var d1 = mobx.autorun(() => values.push("a" + a.get()))
var d2 = mobx.autorun(() => {
if (a.get() === 2) throw /Uhoh/
values.push("b" + a.get())
})
var d3 = mobx.autorun(() => values.push("c" + a.get()))
expect(values).toEqual(["a1", "b1", "c1"])
values.splice(0)
utils.consoleError(() => a.set(2), /Uhoh/)
checkGlobalState()
expect(values.sort()).toEqual(["a2", "c2"]) // order is irrelevant
values.splice(0)
a.set(3)
expect(values.sort()).toEqual(["a3", "b3", "c3"]) // order is irrelevant
checkGlobalState()
d1()
d2()
d3()
})
test("deny state changes in views", function() {
var x = observable.box(3)
var z = observable.box(5)
var y = computed(function() {
z.set(6)
return x.get() * x.get()
})
expect(() => {
y.get() // modifying unobserved values in computeds is allowed, so that new observables can be created and returned
}).not.toThrow()
m.reaction(() => z.get(), () => {})
expect(() => {
y.get()
}).toThrow(/Computed values are not allowed to cause side effects/)
checkGlobalState()
})
test("allow state changes in autorun", function() {
var x = observable.box(3)
var z = observable.box(3)
m.autorun(function() {
if (x.get() !== 3) z.set(x.get())
})
expect(x.get()).toBe(3)
expect(z.get()).toBe(3)
x.set(5) // autorunneres are allowed to change state
expect(x.get()).toBe(5)
expect(z.get()).toBe(5)
expect(mobx._isComputingDerivation()).toBe(false)
checkGlobalState()
})
test("deny array change in view", function(done) {
var x = observable.box(3)
var z = observable([])
var y = computed(function() {
z.push(3)
return x.get() * x.get()
})
expect(function() {
y.get() // modifying z is allowed if nobody is observing
}).not.toThrow()
m.reaction(() => z.length, () => {})
expect(function() {
y.get()
}).toThrow(/Computed values are not allowed to cause side effects by changing observables/)
expect(z.slice()).toEqual([3])
expect(mobx._isComputingDerivation()).toBe(false)
checkGlobalState()
done()
})
test("allow array change in autorun", function() {
var x = observable.box(3)
var z = observable([])
var y = m.autorun(function() {
if (x.get() > 4) z.push(x.get())
})
x.set(5)
x.set(6)
expect(z.slice()).toEqual([5, 6])
x.set(2)
expect(z.slice()).toEqual([5, 6])
expect(mobx._isComputingDerivation()).toBe(false)
checkGlobalState()
})
test("throw error if modification loop", function() {
var x = observable.box(3)
var dis = m.autorun(function() {
x.set(x.get() + 1) // is allowed to throw, but doesn't as the observables aren't bound yet during first execution
})
utils.consoleError(() => {
x.set(5)
}, /Reaction doesn't converge to a stable state/)
checkGlobalState()
})
test("cycle1", function() {
var p = computed(function() {
return p.get() * 2
}) // thats a cycle!
utils.consoleError(() => {
p.observe(voidObserver, true)
}, /Cycle detected/)
checkGlobalState()
})
test("cycle2", function() {
var a = computed(function() {
return b.get() * 2
})
var b = computed(function() {
return a.get() * 2
})
expect(() => {
b.get()
}).toThrow(/Cycle detected/)
checkGlobalState()
})
test("cycle3", function() {
var p = computed(function() {
return p.get() * 2
})
expect(() => {
p.get()
}).toThrow(/Cycle detected/)
checkGlobalState()
})
test("cycle4", function() {
var z = observable.box(true)
var a = computed(function() {
return z.get() ? 1 : b.get() * 2
})
var b = computed(function() {
return a.get() * 2
})
m.observe(b, voidObserver)
expect(1).toBe(a.get())
utils.consoleError(() => {
z.set(false) // introduces a cycle!
}, /Cycle detected/)
checkGlobalState()
})
test("throws when the max iterations over reactions are done", () => {
var foo = mobx.observable({
a: 1
})
mobx.autorun(
() => {
var x = foo.a
foo.a = Math.random()
},
{ name: "bar" }
)
utils.consoleError(
() => foo.a++,
/Reaction doesn't converge to a stable state after 100 iterations/
)
mobx._resetGlobalState()
})
test("issue 86, converging cycles", function() {
function findIndex(arr, predicate) {
for (var i = 0, l = arr.length; i < l; i++) if (predicate(arr[i]) === true) return i
return -1
}
const deleteThisId = mobx.observable.box(1)
const state = mobx.observable({ someArray: [] })
var calcs = 0
state.someArray.push({ id: 1, text: "I am 1" })
state.someArray.push({ id: 2, text: "I am 2" })
// should delete item 1 in first run, which works fine
mobx.autorun(() => {
calcs++
const i = findIndex(state.someArray, item => item.id === deleteThisId.get())
state.someArray.remove(state.someArray[i])
})
expect(state.someArray.length).toBe(1) // should be 1, which prints fine
expect(calcs).toBe(1)
deleteThisId.set(2) // should delete item 2, but it errors on cycle
expect(state.someArray.length).toBe(0) // should be 0, which never prints
expect(calcs).toBe(3)
checkGlobalState()
})
test("slow converging cycle", function() {
var x = mobx.observable.box(1)
var res = -1
mobx.autorun(() => {
if (x.get() === 100) res = x.get()
else x.set(x.get() + 1)
})
// ideally the outcome should be 100 / 100.
// autorun is only an observer of x *after* the first run, hence the initial outcome is not as expected..
// is there a practical use case where such a pattern would be expected?
// maybe we need to immediately register observers on the observable? but that would be slow....
// or detect cycles and re-run the autorun in that case once?
expect(x.get()).toBe(2)
expect(res).toBe(-1)
x.set(7)
expect(x.get()).toBe(100)
expect(res).toBe(100)
checkGlobalState()
})
test("error handling assistence ", function(done) {
var baseError = console.error
var baseWarn = console.warn
var errors = [] // logged errors
var warns = [] // logged warns
var values = [] // produced errors
var thrown = [] // list of actually thrown exceptons
console.error = function(msg) {
errors.push(msg)
}
console.warn = function(msg) {
warns.push(msg)
}
var a = observable.box(3)
var b = computed(function() {
if (a.get() === 42) throw "should not be 42"
return a.get() * 2
})
var c = m.autorun(function() {
values.push(b.get())
})
a.set(2)
try {
a.set(42)
} catch (e) {
thrown.push(e)
}
a.set(7)
// Test recovery
setTimeout(function() {
a.set(4)
try {
a.set(42)
} catch (e) {
thrown.push(e)
}
expect(values).toEqual([6, 4, 14, 8])
expect(errors.length).toBe(2)
expect(warns.length).toBe(0)
expect(thrown.length).toBe(0) // Mobx doesn't propagate throws from reactions
console.error = baseError
console.warn = baseWarn
checkGlobalState()
done()
}, 10)
})
test("236 - cycles", () => {
var Parent = function() {
m.extendObservable(this, {
children: [],
get total0() {
// Sum "value" of children of kind "0"
return this.children
.filter(c => c.kind === 0)
.map(c => c.value)
.reduce((a, b) => a + b, 0)
},
get total1() {
// Sum "value" of children of kind "1"
return this.children
.filter(c => c.kind === 1)
.map(c => c.value)
.reduce((a, b) => a + b, 0)
}
})
}
var Child = function(parent, kind) {
this.parent = parent
m.extendObservable(this, {
kind: kind,
get value() {
if (this.kind === 0) {
return 3
} else {
// Value of child of kind "1" depends on the total value for all children of kind "0"
return this.parent.total0 * 2
}
}
})
}
const parent = new Parent()
parent.children.push(new Child(parent, 0))
parent.children.push(new Child(parent, 0))
parent.children.push(new Child(parent, 0))
var msg = []
var d = m.autorun(() => {
msg.push("total0:", parent.total0, "total1:", parent.total1)
})
// So far, so good: total0: 9 total1: 0
expect(msg).toEqual(["total0:", 9, "total1:", 0])
parent.children[0].kind = 1
expect(msg).toEqual(["total0:", 9, "total1:", 0, "total0:", 6, "total1:", 12])
checkGlobalState()
})
test("peeking inside erroring computed value doesn't bork (global) state", () => {
const a = mobx.observable.box(1)
const b = mobx.computed(() => {
a.get()
throw "chocolademelk"
})
expect(() => {
b.get()
}).toThrowError(/chocolademelk/)
expect(a.isPendingUnobservation).toBe(false)
expect(a.observers.size).toBe(0)
expect(a.diffValue).toBe(0)
expect(a.lowestObserverState).toBe(-1)
expect(a.hasUnreportedChange).toBe(false)
expect(a.value).toBe(1)
expect(b.dependenciesState).toBe(-1) // NOT_TRACKING
expect(b.observing.length).toBe(0)
expect(b.newObserving).toBe(null)
expect(b.isPendingUnobservation).toBe(false)
expect(b.observers.size).toBe(0)
expect(b.diffValue).toBe(0)
expect(b.lowestObserverState).toBe(0)
expect(b.unboundDepsCount).toBe(0)
expect(() => {
b.get()
}).toThrowError(/chocolademelk/)
expect(b.isComputing).toBe(false)
checkGlobalState()
})
describe("peeking inside autorun doesn't bork (global) state", () => {
var r = -1
const a = mobx.observable.box(1)
const b = mobx.computed(() => {
const res = (r = a.get())
if (res === 2) throw "chocolademelk"
return res
})
const d = mobx.autorun(() => b.get())
const c = d[$mobx]
expect(b.get()).toBe(1)
expect(r).toBe(1)
test("it should update correctly initially", () => {
expect(a.isPendingUnobservation).toBe(false)
expect(a.observers.size).toBe(1)
expect(a.diffValue).toBe(0)
expect(a.lowestObserverState).toBe(-1)
expect(a.hasUnreportedChange).toBe(false)
expect(a.value).toBe(1)
expect(b.dependenciesState).toBe(0)
expect(b.observing.length).toBe(1)
expect(b.newObserving).toBe(null)
expect(b.isPendingUnobservation).toBe(false)
expect(b.observers.size).toBe(1)
expect(b.diffValue).toBe(0)
expect(b.lowestObserverState).toBe(0)
expect(b.unboundDepsCount).toBe(1) // value is always the last bound amount of observers
expect(b.value).toBe(1)
expect(b.isComputing).toBe(false)
expect(c.dependenciesState).toBe(0)
expect(c.observing.length).toBe(1)
expect(c.newObserving).toBe(null)
expect(c.diffValue).toBe(0)
expect(c.unboundDepsCount).toBe(1)
expect(c.isDisposed).toBe(false)
expect(c._isScheduled).toBe(false)
expect(c._isTrackPending).toBe(false)
expect(c._isRunning).toBe(false)
checkGlobalState()
})
test("it should not break internal consistency when exception occurred", () => {
// Trigger exception
utils.consoleError(() => {
a.set(2)
}, /chocolademelk/)
expect(r).toBe(2)
expect(a.isPendingUnobservation).toBe(false)
expect(a.observers.size).toBe(1)
expect(a.diffValue).toBe(0)
expect(a.lowestObserverState).toBe(0)
expect(a.hasUnreportedChange).toBe(false)
expect(a.value).toBe(2)
expect(b.dependenciesState).toBe(0) // up to date (for what it's worth)
expect(b.observing.length).toBe(1)
expect(b.newObserving).toBe(null)
expect(b.isPendingUnobservation).toBe(false)
expect(b.observers.size).toBe(1)
expect(b.diffValue).toBe(0)
expect(b.lowestObserverState).toBe(0)
expect(b.unboundDepsCount).toBe(1)
expect(b.isComputing).toBe(false)
expect(() => b.get()).toThrowError(/chocolademelk/)
expect(c.dependenciesState).toBe(0)
expect(c.observing.length).toBe(1)
expect(c.newObserving).toBe(null)
expect(c.diffValue).toBe(0)
expect(c.unboundDepsCount).toBe(1)
expect(c.isDisposed).toBe(false)
expect(c._isScheduled).toBe(false)
expect(c._isTrackPending).toBe(false)
expect(c._isRunning).toBe(false)
checkGlobalState()
})
// Trigger a new change, will this recover?
// is this actually a supported case or should we just give up?
test("it should recover from errors", () => {
a.set(3)
expect(r).toBe(3)
expect(a.isPendingUnobservation).toBe(false)
expect(a.observers.size).toBe(1)
expect(a.diffValue).toBe(0)
expect(a.lowestObserverState).toBe(0)
expect(a.hasUnreportedChange).toBe(false)
expect(a.value).toBe(3)
expect(b.dependenciesState).toBe(0) // up to date
expect(b.observing.length).toBe(1)
expect(b.newObserving).toBe(null)
expect(b.isPendingUnobservation).toBe(false)
expect(b.observers.size).toBe(1)
expect(b.diffValue).toBe(0)
expect(b.lowestObserverState).toBe(0)
expect(b.unboundDepsCount).toBe(1)
expect(b.value).toBe(3)
expect(b.isComputing).toBe(false)
expect(c.dependenciesState).toBe(0)
expect(c.observing.length).toBe(1)
expect(c.newObserving).toBe(null)
expect(c.diffValue).toBe(0)
expect(c.unboundDepsCount).toBe(1)
expect(c.isDisposed).toBe(false)
expect(c._isScheduled).toBe(false)
expect(c._isTrackPending).toBe(false)
expect(c._isRunning).toBe(false)
checkGlobalState()
})
test("it should clean up correctly", () => {
d()
expect(a.isPendingUnobservation).toBe(false)
expect(a.observers.size).toBe(0)
expect(a.diffValue).toBe(0)
expect(a.lowestObserverState).toBe(0)
expect(a.hasUnreportedChange).toBe(false)
expect(a.value).toBe(3)
expect(b.dependenciesState).toBe(-1) // not tracking
expect(b.observing.length).toBe(0)
expect(b.newObserving).toBe(null)
expect(b.isPendingUnobservation).toBe(false)
expect(b.observers.size).toBe(0)
expect(b.diffValue).toBe(0)
expect(b.lowestObserverState).toBe(0)
expect(b.unboundDepsCount).toBe(1)
expect(b.value).not.toBe(3)
expect(b.isComputing).toBe(false)
expect(c.dependenciesState).toBe(-1)
expect(c.observing.length).toBe(0)
expect(c.newObserving).toBe(null)
expect(c.diffValue).toBe(0)
expect(c.unboundDepsCount).toBe(1)
expect(c.isDisposed).toBe(true)
expect(c._isScheduled).toBe(false)
expect(c._isTrackPending).toBe(false)
expect(c._isRunning).toBe(false)
expect(b.get()).toBe(3)
checkGlobalState()
})
})
test("it should be possible to handle exceptions in reaction", () => {
utils.supressConsole(() => {
const errors = []
const a = mobx.observable.box(1)
const d = mobx.autorun(
function() {
throw a.get()
},
{
onError(e) {
errors.push(e)
}
}
)
a.set(2)
a.set(3)
expect(errors).toEqual([1, 2, 3])
d()
checkGlobalState()
})
})
test("it should be possible to handle global errors in reactions", () => {
utils.supressConsole(() => {
const a = mobx.observable.box(1)
const errors = []
const d2 = mobx.onReactionError(e => errors.push(e))
const d = mobx.autorun(function() {
throw a.get()
})
a.set(2)
a.set(3)
d2()
a.set(4)
expect(errors).toEqual([1, 2, 3])
d()
checkGlobalState()
})
})
test("it should be possible to handle global errors in reactions - 2 - #1480", () => {
utils.supressConsole(() => {
const a = mobx.observable.box(1)
const errors = []
const d2 = mobx.onReactionError(e => errors.push(e))
const d = mobx.reaction(
() => a.get(),
a => {
if (a >= 2) throw a
}
)
a.set(2)
a.set(3)
d2()
a.set(4)
expect(errors).toEqual([2, 3])
d()
checkGlobalState()
})
})
test("global error handling will be skipped when using disableErrorBoundaries - 1", () => {
utils.supressConsole(() => {
mobx.configure({ disableErrorBoundaries: true })
try {
const a = mobx.observable.box(1)
expect(() => {
const d = mobx.autorun(function() {
throw "OOPS"
})
}).toThrowError(/OOPS/)
} finally {
mobx.configure({ disableErrorBoundaries: false })
mobx._resetGlobalState()
}
})
})
test("global error handling will be skipped when using disableErrorBoundaries - 2", () => {
utils.supressConsole(() => {
mobx.configure({ disableErrorBoundaries: true })
try {
const a = mobx.observable.box(1)
const d = mobx.reaction(
() => a.get(),
() => {
throw "OOPS"
}
)
expect(() => {
a.set(2)
}).toThrowError(/OOPS/)
d()
} finally {
mobx.configure({ disableErrorBoundaries: false })
mobx._resetGlobalState()
}
})
})
test("error in effect of when is properly cleaned up", () => {
checkGlobalState()
const b = mobx.observable.box(1)
utils.supressConsole(() => {
const d = mobx.when(
() => b.get() === 2,
() => {
throw "OOPS"
}
)
b.set(2)
})
checkGlobalState()
})