0

I have an observable chain that creates an object and then waits for user input until it continues, i.e. something like this:

of(document.createElement("button"))
  .pipe(
    switchMap((button) =>
      fromEvent(button, "click").pipe(
        switchMap(() => doSomething(button))
      )
    )
  )
  .subscribe();

I´m being forced to indent my chain in order to still have access to button. Instead I´d rather keep my chain on the top level, which I tried with skipUntil:

of(document.createElement("button"))
  .pipe(
    skipUntil((button) => fromEvent(button, "click")),
    switchMap((button) => doSomething(button))
  )
  .subscribe();

Sadly skipUntil doesn´t take a function but only an observable. Therefore I can´t seem to pass my button object to it.

Is there another way to make this work?

JonasK
  • 149
  • 2
  • 8

1 Answers1

0

Try this:

of(document.createElement("button"))
  .pipe(
    mergeMap(button => 
      fromEvent(button, "click").pipe(
        startWith(null),
        pairwise(),     
        filter(([prev, curr]) => prev === null && curr instanceof Event),
        switchMap(() => doSomething(button))
      )
    )
  )
  .subscribe();
jagmitg
  • 4,236
  • 7
  • 25
  • 59