MCPcopy Create free account
hub / github.com/deanrad/rxfx / toggleMap

Function toggleMap

operators/src/toggleMap.ts:26–57  ·  view source on GitHub ↗
(
  workCreator: (event: T) => ObservableInput<R>,
  mapper?: (_: T, inner: R) => S
)

Source from the content-addressed store, hash-verified

24 * ```
25 */
26export function toggleMap<T, R, S = R>(
27 workCreator: (event: T) => ObservableInput<R>,
28 mapper?: (_: T, inner: R) => S
29): OperatorFunction<T, S> {
30 return function (source: Observable<T>) {
31 return new Observable((notify) => {
32 let innerSub: Subscription;
33
34 return source.subscribe({
35 next(trigger) {
36 if (!innerSub || innerSub.closed) {
37 innerSub = from(workCreator(trigger)).subscribe({
38 next: (inner) => {
39 const result = (mapper ? mapper(trigger, inner) : inner) as S;
40 notify.next(result);
41 },
42 error: (e) => notify.error(e),
43 });
44 } else {
45 innerSub.unsubscribe();
46 }
47 },
48 error(e) {
49 notify.error(e);
50 },
51 complete() {
52 notify.complete();
53 },
54 });
55 });
56 };
57}

Callers 1

toggleMap.spec.tsFile · 0.90

Calls 1

subscribeMethod · 0.80

Tested by

no test coverage detected