Complete Output() eventemitter in ngOnDestroy callback
up vote
4
down vote
favorite
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
New contributor
add a comment |
up vote
4
down vote
favorite
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
New contributor
stackoverflow.com/questions/48759538/…
– yurzui
2 days ago
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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
New contributor
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
angular
New contributor
New contributor
edited 2 days ago
New contributor
asked 2 days ago
dalox
234
234
New contributor
New contributor
stackoverflow.com/questions/48759538/…
– yurzui
2 days ago
add a comment |
stackoverflow.com/questions/48759538/…
– yurzui
2 days ago
stackoverflow.com/questions/48759538/…
– yurzui
2 days ago
stackoverflow.com/questions/48759538/…
– yurzui
2 days ago
add a comment |
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
.
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 childEventEmitter
in the componentngOnDestroy
. so that would make sense for this particular case? thanks!
– dalox
2 days ago
@dalox, yeap. That makes total sense.
– SiddAjmera
yesterday
add a comment |
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
.
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 childEventEmitter
in the componentngOnDestroy
. so that would make sense for this particular case? thanks!
– dalox
2 days ago
@dalox, yeap. That makes total sense.
– SiddAjmera
yesterday
add a comment |
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
.
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 childEventEmitter
in the componentngOnDestroy
. so that would make sense for this particular case? thanks!
– dalox
2 days ago
@dalox, yeap. That makes total sense.
– SiddAjmera
yesterday
add a comment |
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
.
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
.
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 childEventEmitter
in the componentngOnDestroy
. so that would make sense for this particular case? thanks!
– dalox
2 days ago
@dalox, yeap. That makes total sense.
– SiddAjmera
yesterday
add a comment |
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 childEventEmitter
in the componentngOnDestroy
. 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
add a comment |
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.
dalox is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
stackoverflow.com/questions/48759538/…
– yurzui
2 days ago