1 | import {
|
2 | IAtom,
|
3 | IDepTreeNode,
|
4 | IObservable,
|
5 | addObserver,
|
6 | globalState,
|
7 | isComputedValue,
|
8 | removeObserver
|
9 | } from "../internal"
|
10 |
|
11 | export enum IDerivationState_ {
|
12 |
|
13 |
|
14 | NOT_TRACKING_ = -1,
|
15 |
|
16 |
|
17 |
|
18 | UP_TO_DATE_ = 0,
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | POSSIBLY_STALE_ = 1,
|
26 |
|
27 |
|
28 | STALE_ = 2
|
29 | }
|
30 |
|
31 | export enum TraceMode {
|
32 | NONE,
|
33 | LOG,
|
34 | BREAK
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | export interface IDerivation extends IDepTreeNode {
|
42 | observing_: IObservable[]
|
43 | newObserving_: null | IObservable[]
|
44 | dependenciesState_: IDerivationState_
|
45 | |
46 |
|
47 |
|
48 |
|
49 | runId_: number
|
50 | |
51 |
|
52 |
|
53 | unboundDepsCount_: number
|
54 | onBecomeStale_(): void
|
55 | isTracing_: TraceMode
|
56 |
|
57 | |
58 |
|
59 |
|
60 | requiresObservable_?: boolean
|
61 | }
|
62 |
|
63 | export class CaughtException {
|
64 | constructor(public cause: any) {
|
65 |
|
66 | }
|
67 | }
|
68 |
|
69 | export function isCaughtException(e: any): e is CaughtException {
|
70 | return e instanceof CaughtException
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | export function shouldCompute(derivation: IDerivation): boolean {
|
85 | switch (derivation.dependenciesState_) {
|
86 | case IDerivationState_.UP_TO_DATE_:
|
87 | return false
|
88 | case IDerivationState_.NOT_TRACKING_:
|
89 | case IDerivationState_.STALE_:
|
90 | return true
|
91 | case IDerivationState_.POSSIBLY_STALE_: {
|
92 |
|
93 | const prevAllowStateReads = allowStateReadsStart(true)
|
94 | const prevUntracked = untrackedStart()
|
95 | const obs = derivation.observing_,
|
96 | l = obs.length
|
97 | for (let i = 0; i < l; i++) {
|
98 | const obj = obs[i]
|
99 | if (isComputedValue(obj)) {
|
100 | if (globalState.disableErrorBoundaries) {
|
101 | obj.get()
|
102 | } else {
|
103 | try {
|
104 | obj.get()
|
105 | } catch (e) {
|
106 |
|
107 | untrackedEnd(prevUntracked)
|
108 | allowStateReadsEnd(prevAllowStateReads)
|
109 | return true
|
110 | }
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 | if ((derivation.dependenciesState_ as any) === IDerivationState_.STALE_) {
|
116 | untrackedEnd(prevUntracked)
|
117 | allowStateReadsEnd(prevAllowStateReads)
|
118 | return true
|
119 | }
|
120 | }
|
121 | }
|
122 | changeDependenciesStateTo0(derivation)
|
123 | untrackedEnd(prevUntracked)
|
124 | allowStateReadsEnd(prevAllowStateReads)
|
125 | return false
|
126 | }
|
127 | }
|
128 | }
|
129 |
|
130 | export function isComputingDerivation() {
|
131 | return globalState.trackingDerivation !== null
|
132 | }
|
133 |
|
134 | export function checkIfStateModificationsAreAllowed(atom: IAtom) {
|
135 | if (!__DEV__) {
|
136 | return
|
137 | }
|
138 | const hasObservers = atom.observers_.size > 0
|
139 |
|
140 | if (!globalState.allowStateChanges && (hasObservers || globalState.enforceActions === "always"))
|
141 | console.warn(
|
142 | "[MobX] " +
|
143 | (globalState.enforceActions
|
144 | ? "Since strict-mode is enabled, changing (observed) observable values without using an action is not allowed. Tried to modify: "
|
145 | : "Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, a computed value or the render function of a React component? You can wrap side effects in 'runInAction' (or decorate functions with 'action') if needed. Tried to modify: ") +
|
146 | atom.name_
|
147 | )
|
148 | }
|
149 |
|
150 | export function checkIfStateReadsAreAllowed(observable: IObservable) {
|
151 | if (__DEV__ && !globalState.allowStateReads && globalState.observableRequiresReaction) {
|
152 | console.warn(
|
153 | `[mobx] Observable '${observable.name_}' being read outside a reactive context.`
|
154 | )
|
155 | }
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | export function trackDerivedFunction<T>(derivation: IDerivation, f: () => T, context: any) {
|
164 | const prevAllowStateReads = allowStateReadsStart(true)
|
165 |
|
166 |
|
167 | changeDependenciesStateTo0(derivation)
|
168 | derivation.newObserving_ = new Array(derivation.observing_.length + 100)
|
169 | derivation.unboundDepsCount_ = 0
|
170 | derivation.runId_ = ++globalState.runId
|
171 | const prevTracking = globalState.trackingDerivation
|
172 | globalState.trackingDerivation = derivation
|
173 | globalState.inBatch++
|
174 | let result
|
175 | if (globalState.disableErrorBoundaries === true) {
|
176 | result = f.call(context)
|
177 | } else {
|
178 | try {
|
179 | result = f.call(context)
|
180 | } catch (e) {
|
181 | result = new CaughtException(e)
|
182 | }
|
183 | }
|
184 | globalState.inBatch--
|
185 | globalState.trackingDerivation = prevTracking
|
186 | bindDependencies(derivation)
|
187 |
|
188 | warnAboutDerivationWithoutDependencies(derivation)
|
189 | allowStateReadsEnd(prevAllowStateReads)
|
190 | return result
|
191 | }
|
192 |
|
193 | function warnAboutDerivationWithoutDependencies(derivation: IDerivation) {
|
194 | if (!__DEV__) return
|
195 |
|
196 | if (derivation.observing_.length !== 0) return
|
197 |
|
198 | if (globalState.reactionRequiresObservable || derivation.requiresObservable_) {
|
199 | console.warn(
|
200 | `[mobx] Derivation '${derivation.name_}' is created/updated without reading any observable value.`
|
201 | )
|
202 | }
|
203 | }
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | function bindDependencies(derivation: IDerivation) {
|
211 |
|
212 | const prevObserving = derivation.observing_
|
213 | const observing = (derivation.observing_ = derivation.newObserving_!)
|
214 | let lowestNewObservingDerivationState = IDerivationState_.UP_TO_DATE_
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | let i0 = 0,
|
220 | l = derivation.unboundDepsCount_
|
221 | for (let i = 0; i < l; i++) {
|
222 | const dep = observing[i]
|
223 | if (dep.diffValue_ === 0) {
|
224 | dep.diffValue_ = 1
|
225 | if (i0 !== i) observing[i0] = dep
|
226 | i0++
|
227 | }
|
228 |
|
229 |
|
230 |
|
231 | if (((dep as any) as IDerivation).dependenciesState_ > lowestNewObservingDerivationState) {
|
232 | lowestNewObservingDerivationState = ((dep as any) as IDerivation).dependenciesState_
|
233 | }
|
234 | }
|
235 | observing.length = i0
|
236 |
|
237 | derivation.newObserving_ = null
|
238 |
|
239 |
|
240 |
|
241 |
|
242 | l = prevObserving.length
|
243 | while (l--) {
|
244 | const dep = prevObserving[l]
|
245 | if (dep.diffValue_ === 0) {
|
246 | removeObserver(dep, derivation)
|
247 | }
|
248 | dep.diffValue_ = 0
|
249 | }
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | while (i0--) {
|
255 | const dep = observing[i0]
|
256 | if (dep.diffValue_ === 1) {
|
257 | dep.diffValue_ = 0
|
258 | addObserver(dep, derivation)
|
259 | }
|
260 | }
|
261 |
|
262 |
|
263 |
|
264 | if (lowestNewObservingDerivationState !== IDerivationState_.UP_TO_DATE_) {
|
265 | derivation.dependenciesState_ = lowestNewObservingDerivationState
|
266 | derivation.onBecomeStale_()
|
267 | }
|
268 | }
|
269 |
|
270 | export function clearObserving(derivation: IDerivation) {
|
271 |
|
272 | const obs = derivation.observing_
|
273 | derivation.observing_ = []
|
274 | let i = obs.length
|
275 | while (i--) removeObserver(obs[i], derivation)
|
276 |
|
277 | derivation.dependenciesState_ = IDerivationState_.NOT_TRACKING_
|
278 | }
|
279 |
|
280 | export function untracked<T>(action: () => T): T {
|
281 | const prev = untrackedStart()
|
282 | try {
|
283 | return action()
|
284 | } finally {
|
285 | untrackedEnd(prev)
|
286 | }
|
287 | }
|
288 |
|
289 | export function untrackedStart(): IDerivation | null {
|
290 | const prev = globalState.trackingDerivation
|
291 | globalState.trackingDerivation = null
|
292 | return prev
|
293 | }
|
294 |
|
295 | export function untrackedEnd(prev: IDerivation | null) {
|
296 | globalState.trackingDerivation = prev
|
297 | }
|
298 |
|
299 | export function allowStateReadsStart(allowStateReads: boolean) {
|
300 | const prev = globalState.allowStateReads
|
301 | globalState.allowStateReads = allowStateReads
|
302 | return prev
|
303 | }
|
304 |
|
305 | export function allowStateReadsEnd(prev: boolean) {
|
306 | globalState.allowStateReads = prev
|
307 | }
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | export function changeDependenciesStateTo0(derivation: IDerivation) {
|
314 | if (derivation.dependenciesState_ === IDerivationState_.UP_TO_DATE_) return
|
315 | derivation.dependenciesState_ = IDerivationState_.UP_TO_DATE_
|
316 |
|
317 | const obs = derivation.observing_
|
318 | let i = obs.length
|
319 | while (i--) obs[i].lowestObserverState_ = IDerivationState_.UP_TO_DATE_
|
320 | }
|