How to use ReplaySubject such that its observers can fire repeatedly when it subscribes to a new observable?












0















I am trying to figure how to use ReplaySubject such that its observers can fire repeatedly when it subscribes to a new observable (e.g. HTTP GET).



This is provider that I used:



export class CatalogService {
public borrow$: ReplaySubject<Borrow> = new ReplaySubject<Borrow>(null);

constructor(public http: HttpClient) {
}

public getBorrowView() {
this.http.get<Borrow>(apiUrl + 'Borrow/View').subscribe(this.borrow$);
}
...
}


I have two observers - one in the main component and the other in the uninstantiated child component.



The observer in the main component subscribe in the following manner:



this.catalogService.borrow$.subscribe(
borrowList => {
if (borrowList != null)
this.pages[1].badge = borrowList.length;
}
);


The observer in the uninstantiated child component subscribe in the following manner:



export class BorrowViewPage {
items$: Observable<Borrow>;

constructor(public navCtrl: NavController, public catalogService: CatalogService, public navParams: NavParams) {
this.items$ = this.catalogService.borrow$;
}
}


I want both of them to subscribe to a HTTP request and continue to respond whenever I make the function call in the main component or elsewhere: this.catalogService.getBorrowView()



However, this method only works once. It works fine on the child component. It just doesn't work after the first time for the main component. I tried unsubscribe it and subscribe it back, but it ended up firing too early yielding the wrong result.










share|improve this question

























  • Could you please explain us what you goal is, and provide the code you have tried to reach it ? What you have posted is a solution you think will work, which makes your question a XY problem.

    – trichetriche
    Nov 15 '18 at 10:07











  • I didn't notice that adding additional information can be misleading. I removed what I attempted and left only of what I want to do and problem I encountered in doing so.

    – Ice Drake
    Nov 15 '18 at 22:47
















0















I am trying to figure how to use ReplaySubject such that its observers can fire repeatedly when it subscribes to a new observable (e.g. HTTP GET).



This is provider that I used:



export class CatalogService {
public borrow$: ReplaySubject<Borrow> = new ReplaySubject<Borrow>(null);

constructor(public http: HttpClient) {
}

public getBorrowView() {
this.http.get<Borrow>(apiUrl + 'Borrow/View').subscribe(this.borrow$);
}
...
}


I have two observers - one in the main component and the other in the uninstantiated child component.



The observer in the main component subscribe in the following manner:



this.catalogService.borrow$.subscribe(
borrowList => {
if (borrowList != null)
this.pages[1].badge = borrowList.length;
}
);


The observer in the uninstantiated child component subscribe in the following manner:



export class BorrowViewPage {
items$: Observable<Borrow>;

constructor(public navCtrl: NavController, public catalogService: CatalogService, public navParams: NavParams) {
this.items$ = this.catalogService.borrow$;
}
}


I want both of them to subscribe to a HTTP request and continue to respond whenever I make the function call in the main component or elsewhere: this.catalogService.getBorrowView()



However, this method only works once. It works fine on the child component. It just doesn't work after the first time for the main component. I tried unsubscribe it and subscribe it back, but it ended up firing too early yielding the wrong result.










share|improve this question

























  • Could you please explain us what you goal is, and provide the code you have tried to reach it ? What you have posted is a solution you think will work, which makes your question a XY problem.

    – trichetriche
    Nov 15 '18 at 10:07











  • I didn't notice that adding additional information can be misleading. I removed what I attempted and left only of what I want to do and problem I encountered in doing so.

    – Ice Drake
    Nov 15 '18 at 22:47














0












0








0








I am trying to figure how to use ReplaySubject such that its observers can fire repeatedly when it subscribes to a new observable (e.g. HTTP GET).



This is provider that I used:



export class CatalogService {
public borrow$: ReplaySubject<Borrow> = new ReplaySubject<Borrow>(null);

constructor(public http: HttpClient) {
}

public getBorrowView() {
this.http.get<Borrow>(apiUrl + 'Borrow/View').subscribe(this.borrow$);
}
...
}


I have two observers - one in the main component and the other in the uninstantiated child component.



The observer in the main component subscribe in the following manner:



this.catalogService.borrow$.subscribe(
borrowList => {
if (borrowList != null)
this.pages[1].badge = borrowList.length;
}
);


The observer in the uninstantiated child component subscribe in the following manner:



export class BorrowViewPage {
items$: Observable<Borrow>;

constructor(public navCtrl: NavController, public catalogService: CatalogService, public navParams: NavParams) {
this.items$ = this.catalogService.borrow$;
}
}


I want both of them to subscribe to a HTTP request and continue to respond whenever I make the function call in the main component or elsewhere: this.catalogService.getBorrowView()



However, this method only works once. It works fine on the child component. It just doesn't work after the first time for the main component. I tried unsubscribe it and subscribe it back, but it ended up firing too early yielding the wrong result.










share|improve this question
















I am trying to figure how to use ReplaySubject such that its observers can fire repeatedly when it subscribes to a new observable (e.g. HTTP GET).



This is provider that I used:



export class CatalogService {
public borrow$: ReplaySubject<Borrow> = new ReplaySubject<Borrow>(null);

constructor(public http: HttpClient) {
}

public getBorrowView() {
this.http.get<Borrow>(apiUrl + 'Borrow/View').subscribe(this.borrow$);
}
...
}


I have two observers - one in the main component and the other in the uninstantiated child component.



The observer in the main component subscribe in the following manner:



this.catalogService.borrow$.subscribe(
borrowList => {
if (borrowList != null)
this.pages[1].badge = borrowList.length;
}
);


The observer in the uninstantiated child component subscribe in the following manner:



export class BorrowViewPage {
items$: Observable<Borrow>;

constructor(public navCtrl: NavController, public catalogService: CatalogService, public navParams: NavParams) {
this.items$ = this.catalogService.borrow$;
}
}


I want both of them to subscribe to a HTTP request and continue to respond whenever I make the function call in the main component or elsewhere: this.catalogService.getBorrowView()



However, this method only works once. It works fine on the child component. It just doesn't work after the first time for the main component. I tried unsubscribe it and subscribe it back, but it ended up firing too early yielding the wrong result.







angular typescript ionic-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 22:53







Ice Drake

















asked Nov 15 '18 at 10:02









Ice DrakeIce Drake

6910




6910













  • Could you please explain us what you goal is, and provide the code you have tried to reach it ? What you have posted is a solution you think will work, which makes your question a XY problem.

    – trichetriche
    Nov 15 '18 at 10:07











  • I didn't notice that adding additional information can be misleading. I removed what I attempted and left only of what I want to do and problem I encountered in doing so.

    – Ice Drake
    Nov 15 '18 at 22:47



















  • Could you please explain us what you goal is, and provide the code you have tried to reach it ? What you have posted is a solution you think will work, which makes your question a XY problem.

    – trichetriche
    Nov 15 '18 at 10:07











  • I didn't notice that adding additional information can be misleading. I removed what I attempted and left only of what I want to do and problem I encountered in doing so.

    – Ice Drake
    Nov 15 '18 at 22:47

















Could you please explain us what you goal is, and provide the code you have tried to reach it ? What you have posted is a solution you think will work, which makes your question a XY problem.

– trichetriche
Nov 15 '18 at 10:07





Could you please explain us what you goal is, and provide the code you have tried to reach it ? What you have posted is a solution you think will work, which makes your question a XY problem.

– trichetriche
Nov 15 '18 at 10:07













I didn't notice that adding additional information can be misleading. I removed what I attempted and left only of what I want to do and problem I encountered in doing so.

– Ice Drake
Nov 15 '18 at 22:47





I didn't notice that adding additional information can be misleading. I removed what I attempted and left only of what I want to do and problem I encountered in doing so.

– Ice Drake
Nov 15 '18 at 22:47












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316847%2fhow-to-use-replaysubject-such-that-its-observers-can-fire-repeatedly-when-it-sub%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53316847%2fhow-to-use-replaysubject-such-that-its-observers-can-fire-repeatedly-when-it-sub%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini