forked from mobxjs/mobx.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreaction.dart
192 lines (147 loc) · 3.84 KB
/
reaction.dart
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
part of '../core.dart';
abstract class Reaction implements Derivation {
bool get isDisposed;
void dispose();
void _run();
StackTrace? get debugCreationStack;
}
class ReactionImpl with DebugCreationStack implements Reaction {
ReactionImpl(this._context, Function() onInvalidate,
{required this.name, void Function(Object, Reaction)? onError}) {
_onInvalidate = onInvalidate;
_onError = onError;
}
void Function(Object, ReactionImpl)? _onError;
final ReactiveContext _context;
late void Function() _onInvalidate;
bool _isScheduled = false;
bool _isDisposed = false;
bool _isRunning = false;
@override
final String name;
@override
Set<Atom>? _newObservables;
@override
// ignore: prefer_final_fields
Set<Atom> _observables = {};
bool get hasObservables => _observables.isNotEmpty;
@override
// ignore: prefer_final_fields
DerivationState _dependenciesState = DerivationState.notTracking;
@override
MobXCaughtException? _errorValue;
@override
MobXCaughtException? get errorValue => _errorValue;
@override
bool get isDisposed => _isDisposed;
@override
void _onBecomeStale() {
schedule();
}
Derivation? startTracking() {
_context.startBatch();
_isRunning = true;
return _context._startTracking(this);
}
void endTracking(Derivation? previous) {
_context._endTracking(this, previous);
_isRunning = false;
if (_isDisposed) {
_context.clearObservables(this);
}
_context.endBatch();
}
void track(void Function() fn) {
_context.startBatch();
final notify = _context.isSpyEnabled;
DateTime? startTime;
if (notify) {
startTime = DateTime.now();
_context.spyReport(ReactionSpyEvent(name: name));
}
_isRunning = true;
_context.trackDerivation(this, fn);
_isRunning = false;
if (_isDisposed) {
_context.clearObservables(this);
}
if (_context._hasCaughtException(this)) {
_reportException(_errorValue!, _errorValue!.stackTrace);
}
if (notify) {
_context.spyReport(EndedSpyEvent(
type: 'reaction',
name: name,
duration: DateTime.now().difference(startTime!)));
}
_context.endBatch();
}
@override
void _run() {
if (_isDisposed) {
return;
}
_context.startBatch();
_isScheduled = false;
if (_context._shouldCompute(this)) {
try {
_onInvalidate();
} on Object catch (e, s) {
// Note: "on Object" accounts for both Error and Exception
_errorValue = MobXCaughtException(e, stackTrace: s);
_reportException(_errorValue!, s);
}
}
_context.endBatch();
}
@override
void dispose() {
if (_isDisposed) {
return;
}
_isDisposed = true;
if (_isRunning) {
return;
}
_context.spyReport(ReactionDisposedSpyEvent(name: name));
// ignore: cascade_invocations
_context
..startBatch()
..clearObservables(this)
..endBatch();
}
void schedule() {
if (_isScheduled) {
return;
}
_isScheduled = true;
_context
..addPendingReaction(this)
..runReactions();
}
@override
// ignore: unused_element
void _suspend() {
// Not applicable right now
}
void _reportException(Object exception, StackTrace? stackTrace) {
if (_onError != null) {
_onError!(exception, this);
return;
}
if (_context.config.disableErrorBoundaries == true) {
if (stackTrace != null) {
Error.throwWithStackTrace(exception, stackTrace);
} else {
throw exception;
}
}
if (_context.isSpyEnabled) {
_context.spyReport(ReactionErrorSpyEvent(exception, name: name));
}
_context._notifyReactionErrorHandlers(exception, this);
}
@override
String toString() =>
'Reaction(name: $name, identity: ${identityHashCode(this)})';
}