Complete Output() eventemitter in ngOnDestroy callback











up vote
4
down vote

favorite
1












Is it a good practice to call the complete() method for each Output() EventEmitter in my angular component's ngOnDestroy callback?
That way any subscription made to those outputs would end straight when the component is destroyed. And in most cases, I would not have to worry about unsubscribing anymore in parent components. What do you think?



Example



I have a parent component that subscribes to an output event of a child component.
I usally do:



childCompoment.event.pipe(
takeUntil(this.parentComponentDestroyed$), // end subscription when parent is destroyed
).subscribe((eventData: any) => {
// do stuff
});
}


Now if the child component gets destroyed, that does not end my parent subscription automatically. I have to wait until the parent component is destroyed itself.
If i call event.complete() in the child components' OnDestroy callback then my subscription will end as soon the child component is destroyed.
What is the best approach in that case?










share|improve this question









New contributor




dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • stackoverflow.com/questions/48759538/…
    – yurzui
    2 days ago















up vote
4
down vote

favorite
1












Is it a good practice to call the complete() method for each Output() EventEmitter in my angular component's ngOnDestroy callback?
That way any subscription made to those outputs would end straight when the component is destroyed. And in most cases, I would not have to worry about unsubscribing anymore in parent components. What do you think?



Example



I have a parent component that subscribes to an output event of a child component.
I usally do:



childCompoment.event.pipe(
takeUntil(this.parentComponentDestroyed$), // end subscription when parent is destroyed
).subscribe((eventData: any) => {
// do stuff
});
}


Now if the child component gets destroyed, that does not end my parent subscription automatically. I have to wait until the parent component is destroyed itself.
If i call event.complete() in the child components' OnDestroy callback then my subscription will end as soon the child component is destroyed.
What is the best approach in that case?










share|improve this question









New contributor




dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • stackoverflow.com/questions/48759538/…
    – yurzui
    2 days ago













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





Is it a good practice to call the complete() method for each Output() EventEmitter in my angular component's ngOnDestroy callback?
That way any subscription made to those outputs would end straight when the component is destroyed. And in most cases, I would not have to worry about unsubscribing anymore in parent components. What do you think?



Example



I have a parent component that subscribes to an output event of a child component.
I usally do:



childCompoment.event.pipe(
takeUntil(this.parentComponentDestroyed$), // end subscription when parent is destroyed
).subscribe((eventData: any) => {
// do stuff
});
}


Now if the child component gets destroyed, that does not end my parent subscription automatically. I have to wait until the parent component is destroyed itself.
If i call event.complete() in the child components' OnDestroy callback then my subscription will end as soon the child component is destroyed.
What is the best approach in that case?










share|improve this question









New contributor




dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Is it a good practice to call the complete() method for each Output() EventEmitter in my angular component's ngOnDestroy callback?
That way any subscription made to those outputs would end straight when the component is destroyed. And in most cases, I would not have to worry about unsubscribing anymore in parent components. What do you think?



Example



I have a parent component that subscribes to an output event of a child component.
I usally do:



childCompoment.event.pipe(
takeUntil(this.parentComponentDestroyed$), // end subscription when parent is destroyed
).subscribe((eventData: any) => {
// do stuff
});
}


Now if the child component gets destroyed, that does not end my parent subscription automatically. I have to wait until the parent component is destroyed itself.
If i call event.complete() in the child components' OnDestroy callback then my subscription will end as soon the child component is destroyed.
What is the best approach in that case?







angular






share|improve this question









New contributor




dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago





















New contributor




dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









dalox

234




234




New contributor




dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






dalox is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • stackoverflow.com/questions/48759538/…
    – yurzui
    2 days ago


















  • stackoverflow.com/questions/48759538/…
    – yurzui
    2 days ago
















stackoverflow.com/questions/48759538/…
– yurzui
2 days ago




stackoverflow.com/questions/48759538/…
– yurzui
2 days ago












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Well, I personally feel that it's not really necessary to call complete() on the @Output EventEmitter. I'm saying that because it's up to us to call emit from the Child Component.



It's similar to not calling unsubscribe on an Observable Subscription that is returned from HttpClient in cases it's just called once.



Here's an amazingly enlightening article by Ben Lesh who's Rxjs lead. Reading this article will make it clear of when to unsubscribe and when not to.



You can think about the idea of calling complete() on similar lines.



UPDATE:



The example added by you exhibits an extremely rare use-case when the ChildComponent was used inside the Parent Component Typescript class, probably using ViewChild and it's events were listened to in the Parent Component. In this particular scenario, I think it would be important to unsubscribe in ngOnDestroy.



But in most of the cases, the Child Component's @Output events are generally listened to by a handler in the Parent Component. And the events fired from a Child Component are generally in our hands. And if the Child Component is destroyed, then it won't really fire any events, so I don't really think there's a need to explicitly call complete or unsubscribe in ngOnDestroy.






share|improve this answer























  • thanks for the reply and for the great article. I understand that it's our responsibility to unsubscribe from the parent component. I have updated my question with an example. Which approach would you take in this example?
    – dalox
    2 days ago










  • @dalox, I've updated my answer. Please have a look.
    – SiddAjmera
    2 days ago










  • indeed, in my scenario the parent is a list component and the children are list items retrieved by @ContentChildren. Since i am using virtual scroll, the children are constantly destroyed and recreated as i scroll. If i don't end the subscription as soon as an item is destroyed. i can end up with hundreds of 'passive' subscriptions (for items already destroyed) that will only end when my list component is destroyed. Hence my initial question to complete the child EventEmitter in the component ngOnDestroy. so that would make sense for this particular case? thanks!
    – dalox
    2 days ago












  • @dalox, yeap. That makes total sense.
    – SiddAjmera
    yesterday













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',
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
});


}
});






dalox is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342421%2fcomplete-output-eventemitter-in-ngondestroy-callback%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Well, I personally feel that it's not really necessary to call complete() on the @Output EventEmitter. I'm saying that because it's up to us to call emit from the Child Component.



It's similar to not calling unsubscribe on an Observable Subscription that is returned from HttpClient in cases it's just called once.



Here's an amazingly enlightening article by Ben Lesh who's Rxjs lead. Reading this article will make it clear of when to unsubscribe and when not to.



You can think about the idea of calling complete() on similar lines.



UPDATE:



The example added by you exhibits an extremely rare use-case when the ChildComponent was used inside the Parent Component Typescript class, probably using ViewChild and it's events were listened to in the Parent Component. In this particular scenario, I think it would be important to unsubscribe in ngOnDestroy.



But in most of the cases, the Child Component's @Output events are generally listened to by a handler in the Parent Component. And the events fired from a Child Component are generally in our hands. And if the Child Component is destroyed, then it won't really fire any events, so I don't really think there's a need to explicitly call complete or unsubscribe in ngOnDestroy.






share|improve this answer























  • thanks for the reply and for the great article. I understand that it's our responsibility to unsubscribe from the parent component. I have updated my question with an example. Which approach would you take in this example?
    – dalox
    2 days ago










  • @dalox, I've updated my answer. Please have a look.
    – SiddAjmera
    2 days ago










  • indeed, in my scenario the parent is a list component and the children are list items retrieved by @ContentChildren. Since i am using virtual scroll, the children are constantly destroyed and recreated as i scroll. If i don't end the subscription as soon as an item is destroyed. i can end up with hundreds of 'passive' subscriptions (for items already destroyed) that will only end when my list component is destroyed. Hence my initial question to complete the child EventEmitter in the component ngOnDestroy. so that would make sense for this particular case? thanks!
    – dalox
    2 days ago












  • @dalox, yeap. That makes total sense.
    – SiddAjmera
    yesterday

















up vote
1
down vote



accepted










Well, I personally feel that it's not really necessary to call complete() on the @Output EventEmitter. I'm saying that because it's up to us to call emit from the Child Component.



It's similar to not calling unsubscribe on an Observable Subscription that is returned from HttpClient in cases it's just called once.



Here's an amazingly enlightening article by Ben Lesh who's Rxjs lead. Reading this article will make it clear of when to unsubscribe and when not to.



You can think about the idea of calling complete() on similar lines.



UPDATE:



The example added by you exhibits an extremely rare use-case when the ChildComponent was used inside the Parent Component Typescript class, probably using ViewChild and it's events were listened to in the Parent Component. In this particular scenario, I think it would be important to unsubscribe in ngOnDestroy.



But in most of the cases, the Child Component's @Output events are generally listened to by a handler in the Parent Component. And the events fired from a Child Component are generally in our hands. And if the Child Component is destroyed, then it won't really fire any events, so I don't really think there's a need to explicitly call complete or unsubscribe in ngOnDestroy.






share|improve this answer























  • thanks for the reply and for the great article. I understand that it's our responsibility to unsubscribe from the parent component. I have updated my question with an example. Which approach would you take in this example?
    – dalox
    2 days ago










  • @dalox, I've updated my answer. Please have a look.
    – SiddAjmera
    2 days ago










  • indeed, in my scenario the parent is a list component and the children are list items retrieved by @ContentChildren. Since i am using virtual scroll, the children are constantly destroyed and recreated as i scroll. If i don't end the subscription as soon as an item is destroyed. i can end up with hundreds of 'passive' subscriptions (for items already destroyed) that will only end when my list component is destroyed. Hence my initial question to complete the child EventEmitter in the component ngOnDestroy. so that would make sense for this particular case? thanks!
    – dalox
    2 days ago












  • @dalox, yeap. That makes total sense.
    – SiddAjmera
    yesterday















up vote
1
down vote



accepted







up vote
1
down vote



accepted






Well, I personally feel that it's not really necessary to call complete() on the @Output EventEmitter. I'm saying that because it's up to us to call emit from the Child Component.



It's similar to not calling unsubscribe on an Observable Subscription that is returned from HttpClient in cases it's just called once.



Here's an amazingly enlightening article by Ben Lesh who's Rxjs lead. Reading this article will make it clear of when to unsubscribe and when not to.



You can think about the idea of calling complete() on similar lines.



UPDATE:



The example added by you exhibits an extremely rare use-case when the ChildComponent was used inside the Parent Component Typescript class, probably using ViewChild and it's events were listened to in the Parent Component. In this particular scenario, I think it would be important to unsubscribe in ngOnDestroy.



But in most of the cases, the Child Component's @Output events are generally listened to by a handler in the Parent Component. And the events fired from a Child Component are generally in our hands. And if the Child Component is destroyed, then it won't really fire any events, so I don't really think there's a need to explicitly call complete or unsubscribe in ngOnDestroy.






share|improve this answer














Well, I personally feel that it's not really necessary to call complete() on the @Output EventEmitter. I'm saying that because it's up to us to call emit from the Child Component.



It's similar to not calling unsubscribe on an Observable Subscription that is returned from HttpClient in cases it's just called once.



Here's an amazingly enlightening article by Ben Lesh who's Rxjs lead. Reading this article will make it clear of when to unsubscribe and when not to.



You can think about the idea of calling complete() on similar lines.



UPDATE:



The example added by you exhibits an extremely rare use-case when the ChildComponent was used inside the Parent Component Typescript class, probably using ViewChild and it's events were listened to in the Parent Component. In this particular scenario, I think it would be important to unsubscribe in ngOnDestroy.



But in most of the cases, the Child Component's @Output events are generally listened to by a handler in the Parent Component. And the events fired from a Child Component are generally in our hands. And if the Child Component is destroyed, then it won't really fire any events, so I don't really think there's a need to explicitly call complete or unsubscribe in ngOnDestroy.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









SiddAjmera

9,45321037




9,45321037












  • thanks for the reply and for the great article. I understand that it's our responsibility to unsubscribe from the parent component. I have updated my question with an example. Which approach would you take in this example?
    – dalox
    2 days ago










  • @dalox, I've updated my answer. Please have a look.
    – SiddAjmera
    2 days ago










  • indeed, in my scenario the parent is a list component and the children are list items retrieved by @ContentChildren. Since i am using virtual scroll, the children are constantly destroyed and recreated as i scroll. If i don't end the subscription as soon as an item is destroyed. i can end up with hundreds of 'passive' subscriptions (for items already destroyed) that will only end when my list component is destroyed. Hence my initial question to complete the child EventEmitter in the component ngOnDestroy. so that would make sense for this particular case? thanks!
    – dalox
    2 days ago












  • @dalox, yeap. That makes total sense.
    – SiddAjmera
    yesterday




















  • thanks for the reply and for the great article. I understand that it's our responsibility to unsubscribe from the parent component. I have updated my question with an example. Which approach would you take in this example?
    – dalox
    2 days ago










  • @dalox, I've updated my answer. Please have a look.
    – SiddAjmera
    2 days ago










  • indeed, in my scenario the parent is a list component and the children are list items retrieved by @ContentChildren. Since i am using virtual scroll, the children are constantly destroyed and recreated as i scroll. If i don't end the subscription as soon as an item is destroyed. i can end up with hundreds of 'passive' subscriptions (for items already destroyed) that will only end when my list component is destroyed. Hence my initial question to complete the child EventEmitter in the component ngOnDestroy. so that would make sense for this particular case? thanks!
    – dalox
    2 days ago












  • @dalox, yeap. That makes total sense.
    – SiddAjmera
    yesterday


















thanks for the reply and for the great article. I understand that it's our responsibility to unsubscribe from the parent component. I have updated my question with an example. Which approach would you take in this example?
– dalox
2 days ago




thanks for the reply and for the great article. I understand that it's our responsibility to unsubscribe from the parent component. I have updated my question with an example. Which approach would you take in this example?
– dalox
2 days ago












@dalox, I've updated my answer. Please have a look.
– SiddAjmera
2 days ago




@dalox, I've updated my answer. Please have a look.
– SiddAjmera
2 days ago












indeed, in my scenario the parent is a list component and the children are list items retrieved by @ContentChildren. Since i am using virtual scroll, the children are constantly destroyed and recreated as i scroll. If i don't end the subscription as soon as an item is destroyed. i can end up with hundreds of 'passive' subscriptions (for items already destroyed) that will only end when my list component is destroyed. Hence my initial question to complete the child EventEmitter in the component ngOnDestroy. so that would make sense for this particular case? thanks!
– dalox
2 days ago






indeed, in my scenario the parent is a list component and the children are list items retrieved by @ContentChildren. Since i am using virtual scroll, the children are constantly destroyed and recreated as i scroll. If i don't end the subscription as soon as an item is destroyed. i can end up with hundreds of 'passive' subscriptions (for items already destroyed) that will only end when my list component is destroyed. Hence my initial question to complete the child EventEmitter in the component ngOnDestroy. so that would make sense for this particular case? thanks!
– dalox
2 days ago














@dalox, yeap. That makes total sense.
– SiddAjmera
yesterday






@dalox, yeap. That makes total sense.
– SiddAjmera
yesterday












dalox is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















dalox is a new contributor. Be nice, and check out our Code of Conduct.













dalox is a new contributor. Be nice, and check out our Code of Conduct.












dalox is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53342421%2fcomplete-output-eventemitter-in-ngondestroy-callback%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







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]