12
13
export function runOnChange<T, TChange>(observable: IObservableWithChange<T, TChange>, cb: (value: T, previousValue: T, deltas: RemoveUndefined<TChange>[]) => void): IDisposable {
15
>
let _firstRun = true;
16
>
return autorunWithStoreHandleChanges({
17
>
changeTracker: {
18
>
createChangeSummary: () => ({ deltas: [] as RemoveUndefined<TChange>[], didChange: false }),
19
>
handleChange: (context, changeSummary) => {
20
>
if (context.didChange(observable)) {
21
>
const e = context.change;
22
>
if (e !== undefined) {
23
>
changeSummary.deltas.push(e as RemoveUndefined<TChange>);
24
>
}
25
>
changeSummary.didChange = true;
26
>
}
27
>
return true;
28
>
},
29
>
}
30
>
}, (reader, changeSummary) => {
31
>
const value = observable.read(reader);
32
>
const previousValue = _previousValue;
33
>
if (changeSummary.didChange) {
34
>
_previousValue = value;
35
>
// didChange can never be true on the first autorun, so we know previousValue is defined
36
>
cb(value, previousValue!, changeSummary.deltas);
37
>
}
38
>
if (_firstRun) {
39
>
_firstRun = false;
40
>
_previousValue = value;
41
>
}
42
>
});
43
>
}
44
45
export function runOnChangeWithStore<T, TChange>(observable: IObservableWithChange<T, TChange>, cb: (value: T, previousValue: T, deltas: RemoveUndefined<TChange>[], store: DisposableStore) => void): IDisposable {