Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Oct 23 21:07
    cartant edited #5066
  • Oct 23 19:35
    cartant edited #5066
  • Oct 23 19:26
    cartant edited #5066
  • Oct 23 19:21
    cartant edited #5066
  • Oct 23 13:07
    cartant edited #5066
  • Oct 23 13:06
    cartant edited #5066
  • Oct 23 13:03
    cartant edited #5066
  • Oct 23 13:02
    cartant edited #5066
  • Oct 23 13:01
    cartant edited #5066
  • Oct 23 13:00
    cartant edited #5066
  • Oct 23 12:59
    cartant edited #5066
  • Oct 23 12:58
    cartant edited #5066
  • Oct 23 12:58
    cartant edited #5066
  • Oct 23 12:57
    cartant edited #5066
  • Oct 23 12:57
    cartant edited #5066
  • Oct 23 12:55
    cartant edited #5066
  • Oct 23 12:49
    cartant edited #5066
  • Oct 23 12:49
    cartant edited #5066
  • Oct 23 12:49
    cartant edited #5066
  • Oct 23 12:48
    cartant edited #5066
Dorus
@Dorus
more fancy ways are possible too
Renke Grunwald
@renke
yeah, was trying something similar, not exactly an generic approach, but I really only have two observables here, so no problem
Dorus
@Dorus
another way is to split stream two with share and then use toarray and mergeall to buffer it.
source1.pipe(publish(src1 => source2.pipe(publish(src2 =>
  concat(
    merge(
      src1,
      src2.pipe(
        takeUntil(src1Completed),
        toArray(),
        mergeAll()
    )), src2)
))))
thats probably the most fancy varriant.
@renke
Renke Grunwald
@renke
trying to understand it ;-)
Dorus
@Dorus
Let me switch to pc and format it :)
Renke Grunwald
@renke
Where is src1Completed coming from?
Dorus
@Dorus
@renke Fixed a missing ) wile formatting. The idea is to gather the output of source2 till source1completes into toArray, and then flatten it back with mergeAll

Where is src1Completed coming from?

Oops yeah i skipped over that one too. Should be something like concat(src1.pipe(ignoreElements()), of(1))

Anywawy that's enough Rx for me tonight :)
Renke Grunwald
@renke
Thanks, trying to grok it in my dreams or so I guess ;-)
Renke Grunwald
@renke
@Dorus I've never used publish with a selector as you do in the snippet you sent. Is this used so we can subscribe to source1 and source2 multiple times without actually executing the side effects multiple times?
and more specifically, I was confused about takeUntil(), I would have expected something like delayWhen() (assuming it does what I think it does)
Anton Abilov
@anton164
I recently gave a talk about how we're building our single-page application architecture with RxJS: https://www.youtube.com/watch?v=vd5S_7Qm9WE I guess we're not the first to do so, but curious to hear other people's thoughts about it :)
Renke Grunwald
@renke
@anton164 I was actually looking for stuff like that a few weeks ago, thanks for sharing
Will Mruzek
@mruzekw
If I have two observables, one of numbers and another of click events, how would I combine them to a new stream that would only emit on the click event?
const nums$ = of(0,1,2,3,4,5)
const clicks$ = fromEvent(document, 'click')

const currentNum$ = ...
For click one, it should emit 0. For click two, it should emit 1
Karsten Pruehss
@kpruehss
@mruzekw You should be able to do that using zip
Will Mruzek
@mruzekw
Thanks! I'll give that a look :pray:
Karsten Pruehss
@kpruehss
const currentNum$ = zip(nums$, clicks$).pipe(map([nums, clicks]) => ([nums, clicks]))
this should emit a tuple of the number and the associated click (if you want to just emit the numbers, or an object etc, just change the map function)
Will Mruzek
@mruzekw
It worked! Thanks
Karsten Pruehss
@kpruehss
no worries, glad it helped :)
A-STAR
@A-STAR
what does mean cum$?
Karsten Pruehss
@kpruehss
@A-STAR lol typo...thats supposed to be num$
A-STAR
@A-STAR
:D
Dorus
@Dorus

@Dorus I've never used publish with a selector as you do in the snippet you sent. Is this used so we can subscribe to source1 and source2 multiple times without actually executing the side effects multiple times?

Yes

and more specifically, I was confused about takeUntil(), I would have expected something like delayWhen() (assuming it does what I think it does)

But you cannot stop delayWhen so it would keep delaying even after you no longer need so. There used to be a pauseWhen many versions ago.
@renke

Karsten Pruehss
@kpruehss
i must have had things on my mind
Dorus
@Dorus
const currentNum$ = zip(nums$, clicks$).pipe(map([nums, clicks]) => ([nums, clicks]))

this should emit a tuple of the number and the associated click (if you want to just emit the numbers, or an object etc, just change the map function)

What does this map add exactly?

Karsten Pruehss
@kpruehss
@Dorus in this particular snippet, nothing. But if you wanted to transform the emitted value into something else, like say, only emit the numbers, or as an object literal, you'd do that through the map. I guess I could have left it off, but i didn't want to assume knowledge
@Dorus also bad wording on my part perhaps, next time i'll actually just transform the emission into something. i blame the lack of coffee
Renke Grunwald
@renke
@Dorus I've played around with your second snippet, but I couldn't get it to work; source2 values come before value from source1 and come again after the values from source1. The very first snippet you sent uses shareReplay with a selector which my RxJS version (~6.5) does not seem to support .
      const foo = first$.pipe(
        publish(firstSubject$ => {
          return second$.pipe(secondSubject$ => {
            return concat(
              merge(
                firstSubject$,
                secondSubject$.pipe(
                  takeUntil(concat(firstSubject$.pipe(ignoreElements()), of())),
                  toArray(),
                  mergeAll()
                )
              ),
              secondSubject$
            );
          });
        })
      );
const first$ = of("first1", "first2");
const second$ = of("second1", "second2");
Dorus
@Dorus
@renke You're missing an publish to create secondSubject
Renke Grunwald
@renke
oh...
Dorus
@Dorus
Like this it will subscribe to second$ twice and idk what effect that has.
Renke Grunwald
@renke
mh, I still see the values from second$ first. Is it a problem that I use of as to create my observables here?
Dorus
@Dorus

@renke https://rxviz.com/v/moY6P51O

works for me.

@renke Oops indeed, looks like that when source2 completes, it instantly emit.
@renke https://rxviz.com/v/38MaX4Y8
source1.pipe(publish(src1 => source2.pipe(publish(src2 =>
  concat(
    merge(
      src1,
      merge(NEVER, src2).pipe(
        takeUntil(concat(src1.pipe(ignoreElements()), of(1))),
        toArray(),
        mergeAll()
    )), src2)
))))
I wonder if i missed a way to simplify this...
Renke Grunwald
@renke
yes, that makes sense, I guess, that would prevent it from completing before src1 completes.
Dorus
@Dorus
yup
toArray react to completion and you dont want src2 completing to trigger that before src1 does.
Renke Grunwald
@renke
I still don't understand why delayWhen is not the right choice here:
      const foo = first$.pipe(
        publish(firstSubject$ => {
          return merge(
            firstSubject$,
            second$.pipe(
              delayWhen(() =>
                concat(firstSubject$.pipe(ignoreElements()), of())
              )
            )
          );
        })
      );
I would imagine that when firstSubject$ completes there is no leakage from delayWhen whatsoever
Dorus
@Dorus
You should test it. But i dont think this will allow second$ to emit once first is done, not after the initial burst.