1 | import {
|
2 | Lambda,
|
3 | ComputedValue,
|
4 | IDependencyTree,
|
5 | IDerivation,
|
6 | IDerivationState_,
|
7 | TraceMode,
|
8 | getDependencyTree,
|
9 | globalState,
|
10 | runReactions,
|
11 | checkIfStateReadsAreAllowed
|
12 | } from "../internal"
|
13 |
|
14 | export interface IDepTreeNode {
|
15 | name_: string
|
16 | observing_?: IObservable[]
|
17 | }
|
18 |
|
19 | export interface IObservable extends IDepTreeNode {
|
20 | diffValue_: number
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 | lastAccessedBy_: number
|
27 | isBeingObserved_: boolean
|
28 |
|
29 | lowestObserverState_: IDerivationState_
|
30 | isPendingUnobservation_: boolean
|
31 |
|
32 | observers_: Set<IDerivation>
|
33 |
|
34 | onBUO(): void
|
35 | onBO(): void
|
36 |
|
37 | onBUOL: Set<Lambda> | undefined
|
38 | onBOL: Set<Lambda> | undefined
|
39 | }
|
40 |
|
41 | export function hasObservers(observable: IObservable): boolean {
|
42 | return observable.observers_ && observable.observers_.size > 0
|
43 | }
|
44 |
|
45 | export function getObservers(observable: IObservable): Set<IDerivation> {
|
46 | return observable.observers_
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | export function addObserver(observable: IObservable, node: IDerivation) {
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | observable.observers_.add(node)
|
72 | if (observable.lowestObserverState_ > node.dependenciesState_)
|
73 | observable.lowestObserverState_ = node.dependenciesState_
|
74 |
|
75 |
|
76 |
|
77 | }
|
78 |
|
79 | export function removeObserver(observable: IObservable, node: IDerivation) {
|
80 |
|
81 |
|
82 |
|
83 | observable.observers_.delete(node)
|
84 | if (observable.observers_.size === 0) {
|
85 |
|
86 | queueForUnobservation(observable)
|
87 | }
|
88 |
|
89 |
|
90 | }
|
91 |
|
92 | export function queueForUnobservation(observable: IObservable) {
|
93 | if (observable.isPendingUnobservation_ === false) {
|
94 |
|
95 | observable.isPendingUnobservation_ = true
|
96 | globalState.pendingUnobservations.push(observable)
|
97 | }
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | export function startBatch() {
|
106 | globalState.inBatch++
|
107 | }
|
108 |
|
109 | export function endBatch() {
|
110 | if (--globalState.inBatch === 0) {
|
111 | runReactions()
|
112 |
|
113 | const list = globalState.pendingUnobservations
|
114 | for (let i = 0; i < list.length; i++) {
|
115 | const observable = list[i]
|
116 | observable.isPendingUnobservation_ = false
|
117 | if (observable.observers_.size === 0) {
|
118 | if (observable.isBeingObserved_) {
|
119 |
|
120 | observable.isBeingObserved_ = false
|
121 | observable.onBUO()
|
122 | }
|
123 | if (observable instanceof ComputedValue) {
|
124 |
|
125 |
|
126 | observable.suspend_()
|
127 | }
|
128 | }
|
129 | }
|
130 | globalState.pendingUnobservations = []
|
131 | }
|
132 | }
|
133 |
|
134 | export function reportObserved(observable: IObservable): boolean {
|
135 | checkIfStateReadsAreAllowed(observable)
|
136 |
|
137 | const derivation = globalState.trackingDerivation
|
138 | if (derivation !== null) {
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 | if (derivation.runId_ !== observable.lastAccessedBy_) {
|
145 | observable.lastAccessedBy_ = derivation.runId_
|
146 |
|
147 | derivation.newObserving_![derivation.unboundDepsCount_++] = observable
|
148 | if (!observable.isBeingObserved_ && globalState.trackingContext) {
|
149 | observable.isBeingObserved_ = true
|
150 | observable.onBO()
|
151 | }
|
152 | }
|
153 | return true
|
154 | } else if (observable.observers_.size === 0 && globalState.inBatch > 0) {
|
155 | queueForUnobservation(observable)
|
156 | }
|
157 |
|
158 | return false
|
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 | export function propagateChanged(observable: IObservable) {
|
185 |
|
186 | if (observable.lowestObserverState_ === IDerivationState_.STALE_) return
|
187 | observable.lowestObserverState_ = IDerivationState_.STALE_
|
188 |
|
189 |
|
190 | observable.observers_.forEach(d => {
|
191 | if (d.dependenciesState_ === IDerivationState_.UP_TO_DATE_) {
|
192 | if (__DEV__ && d.isTracing_ !== TraceMode.NONE) {
|
193 | logTraceInfo(d, observable)
|
194 | }
|
195 | d.onBecomeStale_()
|
196 | }
|
197 | d.dependenciesState_ = IDerivationState_.STALE_
|
198 | })
|
199 |
|
200 | }
|
201 |
|
202 |
|
203 | export function propagateChangeConfirmed(observable: IObservable) {
|
204 |
|
205 | if (observable.lowestObserverState_ === IDerivationState_.STALE_) return
|
206 | observable.lowestObserverState_ = IDerivationState_.STALE_
|
207 |
|
208 | observable.observers_.forEach(d => {
|
209 | if (d.dependenciesState_ === IDerivationState_.POSSIBLY_STALE_) {
|
210 | d.dependenciesState_ = IDerivationState_.STALE_
|
211 | if (__DEV__ && d.isTracing_ !== TraceMode.NONE) {
|
212 | logTraceInfo(d, observable)
|
213 | }
|
214 | } else if (
|
215 | d.dependenciesState_ === IDerivationState_.UP_TO_DATE_
|
216 | ) {
|
217 | observable.lowestObserverState_ = IDerivationState_.UP_TO_DATE_
|
218 | }
|
219 | })
|
220 |
|
221 | }
|
222 |
|
223 |
|
224 | export function propagateMaybeChanged(observable: IObservable) {
|
225 |
|
226 | if (observable.lowestObserverState_ !== IDerivationState_.UP_TO_DATE_) return
|
227 | observable.lowestObserverState_ = IDerivationState_.POSSIBLY_STALE_
|
228 |
|
229 | observable.observers_.forEach(d => {
|
230 | if (d.dependenciesState_ === IDerivationState_.UP_TO_DATE_) {
|
231 | d.dependenciesState_ = IDerivationState_.POSSIBLY_STALE_
|
232 | d.onBecomeStale_()
|
233 | }
|
234 | })
|
235 |
|
236 | }
|
237 |
|
238 | function logTraceInfo(derivation: IDerivation, observable: IObservable) {
|
239 | console.log(
|
240 | `[mobx.trace] '${derivation.name_}' is invalidated due to a change in: '${observable.name_}'`
|
241 | )
|
242 | if (derivation.isTracing_ === TraceMode.BREAK) {
|
243 | const lines = []
|
244 | printDepTree(getDependencyTree(derivation), lines, 1)
|
245 |
|
246 |
|
247 | new Function(
|
248 | `debugger;
|
249 | /*
|
250 | Tracing '${derivation.name_}'
|
251 |
|
252 | You are entering this break point because derivation '${derivation.name_}' is being traced and '${observable.name_}' is now forcing it to update.
|
253 | Just follow the stacktrace you should now see in the devtools to see precisely what piece of your code is causing this update
|
254 | The stackframe you are looking for is at least ~6-8 stack-frames up.
|
255 |
|
256 | ${derivation instanceof ComputedValue ? derivation.derivation.toString().replace(/[*]\//g, "/") : ""}
|
257 |
|
258 | The dependencies for this derivation are:
|
259 |
|
260 | ${lines.join("\n")}
|
261 | */
|
262 | `)()
|
263 | }
|
264 | }
|
265 |
|
266 | function printDepTree(tree: IDependencyTree, lines: string[], depth: number) {
|
267 | if (lines.length >= 1000) {
|
268 | lines.push("(and many more)")
|
269 | return
|
270 | }
|
271 | lines.push(`${"\t".repeat(depth - 1)}${tree.name}`)
|
272 | if (tree.dependencies) tree.dependencies.forEach(child => printDepTree(child, lines, depth + 1))
|
273 | }
|