46
*/
47
export function observableReducerSettable<T, TInChanges, TOutChange = void>(owner: DebugOwner, options: IReducerOptions<T, TInChanges, TOutChange>): ISettableObservable<T, TOutChange> {
48
>
let prevValue: T | undefined = undefined;
reducer.ts
49
>
let hasValue = false;
50
>
51
>
const d = new DerivedWithSetter(
52
>
new DebugNameData(owner, undefined, options.update),
53
>
(reader: IDerivedReader<TOutChange>, changeSummary) => {
54
>
if (!hasValue) {
55
>
prevValue = options.initial instanceof Function ? options.initial() : options.initial;
56
>
hasValue = true;
57
>
}
58
>
const newValue = options.update(reader, prevValue!, changeSummary);
59
>
prevValue = newValue;
60
>
return newValue;
61
>
},
62
>
options.changeTracker,
63
>
() => {
64
>
if (hasValue) {
65
>
options.disposeFinal?.(prevValue!);
66
>
hasValue = false;
67
>
}
68
>
},
69
>
options.equalityComparer ?? strictEquals,
70
>
(value, tx, change) => {
71
if (!hasValue) {
72
throw new BugIndicatingError('Can only set when there is a listener! This is to prevent leaks.');