Open History to see the last commit
1type TrackableQueryObserver<TResult extends object> = {
2 options: { notifyOnChangeProps?: unknown }
3 trackResult: (result: TResult) => unknown
4}
5β
6/**
7 * Wraps observer results with property tracking when `notifyOnChangeProps` is
8 * not set, so the observer only notifies about the properties the host reads.
9 */
10export class QueryObserverResultTracker<TResult extends object> {
11 private result: TResult | undefined
12 private usesTracking = false
13β
14 /**
15 * Forgets the last result, so the next `update` returns a result again.
16 */
17 reset(): void {
18 this.result = undefined
19 this.usesTracking = false
20 }
21β
22 /**
23 * Returns the result to expose, tracked through the observer when it uses
24 * property tracking.
25 * @param observer - The observer the result comes from, if any.
26 * @param result - The latest result of the observer.
27 * @returns The result to expose, or `undefined` if neither the result nor
28 * the tracking mode changed since the last call.
29 */
30 update(
31 observer: TrackableQueryObserver<TResult> | undefined,
32 result: TResult,
33 ): TResult | undefined {
34 const usesTracking = !!observer && !observer.options.notifyOnChangeProps
35β
36 if (Object.is(this.result, result) && this.usesTracking === usesTracking) {
37 return undefined
38 }
39β
40 this.result = result
41 this.usesTracking = usesTracking
42β
43 return usesTracking ? (observer.trackResult(result) as TResult) : result
44 }
45}