Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 13:06
    demensky opened #7007
  • 11:38
    demensky synchronize #7001
  • 11:36
    demensky edited #6999
  • 11:35
    demensky edited #7000
  • 11:35
    demensky edited #7001
  • 11:34
    demensky edited #7006
  • Jun 29 22:40
    demensky opened #7006
  • Jun 29 20:08
    Yozmo closed #6996
  • Jun 29 20:08
    Yozmo commented #6996
  • Jun 29 19:46
    mokipedia commented #6903
  • Jun 29 09:07
    henry-alakazhang opened #7005
  • Jun 28 16:36
    kwonoj closed #7004
  • Jun 28 16:36
    kwonoj commented #7004
  • Jun 28 11:31
    kulame edited #7004
  • Jun 28 11:26
    kulame edited #7004
  • Jun 28 11:25
    kulame edited #7004
  • Jun 28 11:25
    kulame edited #7004
  • Jun 28 11:24
    kulame edited #7004
  • Jun 28 11:24
    kulame opened #7004
  • Jun 27 14:38
    TrevorKarjanis edited #7003
Stephen
@S-Stephen

Hi,
I'm using rxjs in an angular application, which is chaining several combineLatest() observables.

The source observable finishes with a share() (so that the observable is replayed to new subscribers?)

The trouble I'm having is that when I subscribe to the observable in my component I am not seeing extra values that are being added via one of the many combinedLatest calls. The values appear to be in the chain of commands. I log them via tap, however my the output from my subscription appears in the console boefore those log messages and doesn't appear to include the values I suggest should be there.

my code is something like this

obs1$ =  obs$.pipe(
   map( /* some operation */),
   share()
) 

obs2$ = combineLatest(
   [$obs1, $actionx] )
.pipe(
   map(/* reduce the data some way*/)
)

obs3$ = combineLatest(
  [ obs2$, newdata$])
.pipe(
   map(/*insert new data*/)
)

obs3$.subscribe(data => this.myappdata = data)

My current thoughts are that it does seem as though I'm overusing combine latest, what other appraoches can I use and then does the original share() affect when the subscribe reacts?

Jorrit
@jorrit-wehelp
@S-Stephen well, share() does not replay by itself... you add a replay() or use the convenient shareReplay operator :)
Stephen
@S-Stephen
@jorrit-wehelp sorry my mix up. I use share() to prevent the underlying get request to be re-actioned, so when there is a new subscriber it provides the last data? (I only want the last data that was provided, not the history)
Am I correct in understanding that when I subscribe to obs3$ then when there is a newdata$ event the subscribe should run with the new data?
Jorrit
@jorrit-wehelp
No, share does not cache/replay data by itself. I will just keep a single (ref-counted) subscription to the source
as for obs$3, only if obs2$ and newdata$ both have emitted atleast one value
Stephen
@S-Stephen
If obs2$ and newdata$ have emitted a value then newdata$ emits a second value, provided the subscribe happened before all the events, would we expect the subscription action to be called twice, ie with the updated data?
Jorrit
@jorrit-wehelp
yes
Stephen
@S-Stephen
okay thanks, I have some confidence that I'm doing things correctly (thought I have quite a few chained combineLatest -; I'll find the correct idiom later) I can see the data I expect in tap() but not when the observable is piped into async in the component. Looks like something quite subtle (or stupid by me ;-) )
Jorrit
@jorrit-wehelp
if you only subscribe in the asyc pipe that should work, but often the case with combineLatest that you need to give the sources some starting value, so you know for sure everything gets an initial value and you are not waiting on one of them
Stephen
@S-Stephen
Yep the observables are from a BehaviourSubject .asObservable(). I'm subscribing both in the init of the component and using the angular provided pipe in the template. It's a mystery at the moment why the updated data is not coming though ;-(
Lennu Vuolanne
@vuolen
Shouldnt from([]) only result in completion? Seems like it just freezes
Lennu Vuolanne
@vuolen
oops nevermind ^^ fixed it
Dorus
@Dorus
:+1:

Shouldnt from([]) only result in completion? Seems like it just freezes

yes should be same as EMPTY

Nicholas Cunningham
@ndcunningham

@derekkite

const fooObj = foo.reduce((acc, curr) => acc[curr] = curr, {})

This will not have a type on fooObj since we did the {} inside of the reduce

sorry kinda late :sweat_smile:
Tyler
@tkil_gitlab
Hi gang, did switchLatest for previous become switchMap? Thanks in advance.
Derek
@derekkite
Yes
Tyler
@tkil_gitlab
Thanks @derekkite :)
Reactist215
@Reactist215
Hi, all
I would like to subscribe to a certain API and get the data in realtime.
In this case, RxJS is the best option for it?
dennbagas
@dennbagas
Hello, all
Reactist215
@Reactist215
Hello @dennbagas
Hope you are doing great
dennbagas
@dennbagas
I have a question about how to make parallel http call using RXJS, im using NestJS
I posted the question on stackoverflow: https://stackoverflow.com/questions/64496237/rxjs-parallel-http-call-to-paginated-api-using-nestjs-httpservice
It have bountied for 50 reputation point, i got stuck on this batching task
Kate
@tabasca
hi. am I doing it wrong or why my merge Observable completes? https://stackblitz.com/edit/jwoztx?devtoolsheight=33&file=index.ts
docs say that "The output Observable only completes once all input Observables have completed" - so why does it complete?
Dorus
@Dorus
@tabasca Because the array has a finit lenght of 1 item?
If you meant to emit the stream from the behaviorsubject instead of the behaviorsubject itself, you should do merge(sub)

Notice

merge([sub])
.subscribe(
  (e) => console.log('subscribe' + e),
  () => {}, 
  () => console.log('complete'));

will emit

subscribe[object Object]
complete

the [object Object] here is the behaviorsubject.

Kate
@tabasca
@Dorus i don't understand - i need to merge two subjects, in my example there was only one, yep, but with two of them it is all the same - merge completes - but both subjects don't, so why merge completes?
Dorus
@Dorus
Because you are emitting the array
to merge 2 subjects, do merge(sub1, sub2)
Kate
@tabasca
@Dorus wow. works :) thank you <3
Ben Lesh
@BenLesh_twitter
@tabasca FWIW: in rxjs@next (7.0.0) what you had above would totally have worked.
We're trying to standardize the behavior that a single array passed to zip, concat, merge, etc , is treated like an array of arguments.
There's a lot of work we're trying to do to promote consistency so these quirks don't confuse people.
PikaChokeMe
@PikaChokeMe
getLastCommits(): Observable<PRData[]> {
    return this.bitbucketService
      .getLatestCommits()
      .pipe(
        concatMap((listOfCommits: any) => listOfCommits.values),
        map((commit: any) => {
          const hash = commit.hash;
          return this.bitbucketService.getPullRequestIDfor(hash)
            .pipe(
              map(id => ({
                commitHash: hash,
                pullRequestID: id
              } as PRData))
            );
        })
      );
  }

I'm trying to do this convoluted thing....
Bitbucket service gets my last 10 commits in an object with an array called values containing all my commits
I'm trying to use concatMap to peel away that object into an individual object for every value/commit in that object

From here I want to take the has for every commit....
reach out and get the pull request data for that hash

and somehow wrap and return both the hash and the pull request ID returned from that new subscription call

In the end I want an array of { commitHash, pullRequestID }, { commitHash, pullRequestID }, { commitHash, pullRequestID }
But right now with this I seem to be getting on single { commitHash, pullRequestID }

Ignoring the fact that at this point I don't really know what I'm doing, what am I doing wrong?

Derek
@derekkite
The map is returning an observable. Should be mergemap or concatmap
PikaChokeMe
@PikaChokeMe
getLastCommits(): Observable<PRData[]> {
    return this.bitbucketService
      .getLatestCommits()
      .pipe(
        map((listOfCommits: any) => listOfCommits.values),
        tap((whatever: any) => console.log(whatever)),
        map((listOfCommits: any) => {
          return listOfCommits.map((commit: any) => {
            const hash = commit.hash;
            return this.bitbucketService.getPullRequestIDfor(hash)
              .pipe(
                map(id => ({
                          commitHash: hash,
                          pullRequestID: id
                        } as PRData))
              );
          });
        }),
        tap((whatever: any) => console.log(whatever)),
      );
  }
I've gotten it down to this...
where I have an array of observables.... (possibly bad because it's nested)
Is there a way to unwrap all those inner subscriptions and accumulate all this data? when I subscribe?
Was my other way better?
Dorus
@Dorus
@TheeRFG you should look at the mergeMap/concatMap operators to flatten the observable of observables.
a very common patter is source.mergeMap(e => httpCall(e))
PikaChokeMe
@PikaChokeMe
I feel like I'm confused about where to actually use that operator
map((listOfCommits: any) => listOfCommits.values),

At this point I have an array,
using

mergeMap((listOfCommits: any) => {
          return listOfCommits.map((commit: any) => {

here instead of map just tells me that mergeMap is deprecated and that I should use an inner map instead... I'm not sure what that means or if this is where I was supposed to mergeMap to begin with

Dorus
@Dorus
when you call map<T, T2>(e => httpcall(e)) you're going from Observable<T> -> Observable<Observable<T2>> right?
mergeMap will flatten this (as will concatMap, exhaustMap, switchMap, but in different concurrent patterns). Thus your Observable<Observable<T2>> back to <Observable<T2>.
Just like flatten on an array T[][] will give T[].
@TheeRFG are you using one of the deprecated overloads of mergeMap?
PikaChokeMe
@PikaChokeMe
I think I actually had a misplaced parenthesis which was giving me that issue
Dorus
@Dorus
They deprecated mergeMap(selector, resultSelctor) in favor of mergeMap(selector) https://rxjs-dev.firebaseapp.com/guide/v6/migration#howto-result-selector-migration