create "catchTalkback" callbag operator
This commit is contained in:
parent
70946d450a
commit
633611056e
1 changed files with 25 additions and 0 deletions
|
@ -189,3 +189,28 @@ export function map<I, O, TB>(f: (input: I) => O): (source: Callbag<TB, I>) => C
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intercepts talkback data sent to a source and provides them to a closure instead.
|
||||||
|
* Does not disconnect signals; they are sent upstream as normal.
|
||||||
|
*/
|
||||||
|
export function catchTalkback<T, TB>(talkbackHandler: (data: TB) => void): (source: Source<T>) => Callbag<TB, T> {
|
||||||
|
return (source: Source<T>) => (type: START | DATA | END, data?: Callbag<T, TB> | TB) => {
|
||||||
|
if (type !== 0) return;
|
||||||
|
let sourceTalkback: Callbag<void, unknown> | undefined;
|
||||||
|
const sink = data as Callbag<T, TB>;
|
||||||
|
source(0, (type: START | DATA | END, data?: Callbag<void, unknown> | T) => {
|
||||||
|
if (type === 0) {
|
||||||
|
sourceTalkback = data as Callbag<void, unknown>;
|
||||||
|
sink(0, (type: START | DATA | END, data?: Callbag<T, TB> | TB) => {
|
||||||
|
if (type === 1) {
|
||||||
|
talkbackHandler(data as TB);
|
||||||
|
} else {
|
||||||
|
sourceTalkback?.(type as 0 & 1 & 2, data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sink(type as 1 & 2, data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue