Hi all.
So, observables are lazy, which means they don’t have the built-in caching that promises have.
So let’s say I need to request two resources from the server, then when they arrive execute a function that uses the two results.
Let’s also say that I’m going to repeat that request, but this time I can reuse one of those earlier results to save an HTTP call.
With promises this is trivial, but I’m not seeing an intuitive-looking way to do this with observables. I assume this is a fairly common thing … is there a widely-used pattern for such things?
Thank you in advance!
const foo = ["alpha", "bravo", "charlie"]
// With these types below
const fooObj = foo.reduce((acc, curr) => /* convert to object */)
/*
But fooObj now has type of
= {
alpha,
bravo,
charlie
}
*/
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?
Shouldnt
from([])
only result in completion? Seems like it just freezes
yes should be same as EMPTY
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.
merge(sub1, sub2)
zip
, concat
, merge
, etc , is treated like an array of arguments.
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?
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...