Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache(), share(), publish().refCount() and publishReplay().refCount() do not work as told #1405

Closed
jvanbruegge opened this issue Mar 1, 2016 · 5 comments

Comments

@jvanbruegge
Copy link

Hi,
I'm fairly new to RxJS, so this could be a misunderstanding, but for the following situation:
I have a Service singleton that queries a REST API, like this:

export class Service
{
  public get RestStream() : Observable<RestModel> { 
    return this.getStuff().publishReplay().refCount(); 
  }
...
}

"this.getStuff()" queries the API and returns an Observable
In my understanding, every further transformation of the RestStream in other classes shouldnt requery the API, because I cached it (Say for example I want a part of the RestModel and add a function somewhere that returns service.RestStream.map(<something>)). So any subscribtion to the service or depenent services that further transform the Observable should not requery to API.
I guess this is wrong, because I get plenty of requests in chrome to an url that is only requested in that getStuff() method.
What is the correct way to implement the behavior that I want? (I already tried the operators in the heading)

@jvanbruegge jvanbruegge changed the title cache(), share() and publishReplay().refCount() do not work as told cache(), share(), publish().refCount() and publishReplay().refCount() do not work as told Mar 1, 2016
@tariqkb
Copy link

tariqkb commented Mar 1, 2016

I'm still new to typescript, but I have a feeling that it's because every time you call the getter, it creates a new observable. Try something like this?

export class Service {
    private _restStream: Observable<RestModel>;

    public get RestStream(): Observable<RestModel> {
        return this._restStream;
    }

    constructor() {
        this._restStream = this.getStuff().publishReplay().refCount();
    }
}

@benlesh
Copy link
Member

benlesh commented Mar 1, 2016

@SuperManitu I have to say that @tbugrara is right on.

Think of an Observable like a function. In this case, it's a function that starts a request the first time it's called (subscribed to) and shares that request (or it's result) with everyone else that calls it (subscribes to it).

@tbugrara's example is exactly right. Also in beta.2 you can replace .publishReplay().refCount() with .cache() as they are the same.

@benlesh benlesh closed this as completed Mar 1, 2016
@benlesh
Copy link
Member

benlesh commented Mar 1, 2016

Closing this for now, as it's been answered, feel free to reopen if you think there is an actual bug or issue with the library to be addressed.

@jvanbruegge
Copy link
Author

I have to reopen, because that was what I tried in the first place, saving getStuff().cache() to a global variable, but that didnt word either

@lock
Copy link

lock bot commented Jun 7, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants