-
Notifications
You must be signed in to change notification settings - Fork 26.3k
feat(core): add observed to OutputEmitterRef
#55793
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
Conversation
Prior to this commit, it wasn't possible to check whether the output had any listeners (if it was being observed). Before `output()`, we could check whether `EventEmitter` had any listeners by checking its `observed` property. In this commit, we convert `listeners` from a list to a signal so that we can have a computed `observed` signal. This commit only updates the `OutputEmitterRef` implementation. We can't update `OutputFromObservableRef` because it expects an observable signature to be provided, and the observable doesn't technically have any API to count subscribers; only `Subject` does. Please note that there have been different discussions on whether the `OutputEmitterRef` should be extended or not. This change is not considered breaking because the signature of the _private_ property has been changed and another property has been added to the public API. Related issue: angular#54837
|
I think we still want to discuss this change more— that's why we didn't create a PR for this. We are not aligned whether it makes sense to add this functionality, or if it just increases API surface unnecessarily and potentially even encourages non-ideal practices. |
|
I think we should discuss more potential use-cases in the issue. So far, there wasn't too much interest IIRC. |
Think about a component which has the ability to emit an output on every mouse move. If I knew no one listens to the output there is no reason to even add the event listener. Can really help with non angular third party libraries integration too. |
|
@Harpush There are ways to address this issue, which may not be super common, looking at the issue interest— E.g.
I'm not opposed against this property. It was something we considered during design, but simply wanted to collect more feedback. It's trivial to add |
By using output from observable - only when someone subscribes (which is essentially the same as having a consumer to the output) the observable will actually register the event? What about multiple subscribers? Do I need to share it? Although the chance of multiple subscriptions is extremely low... |
Arguably, if you are thinking of optimizations like this w/r/t to e.g. a mouse event being expensive, you are likely also considering what would happen with multiple subscribers, or investing time to think how this can be solved. |
|
when this feature will be merged ? |
|
Another use case for this is a directive that has a default behavior, but allows a dev to specify their own handler to be used in place of the default by handling an output. With |
|
(removing this from v19 scope as we don't have sufficient alignment on whether or how we want to expose this option) |
|
This is quite an artificial limitation. We were using something like: if (this.create.observed) {
this.create.emit({
name: value.name,
});
} else {
this.defaultHandler(value.name);
}I am surprised that this is no longer possible to do and requires an additional input. @alxhub, any notes on the best practice here and why usage like this should be prevented? |
|
Let's continue the discussion in #54837. |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Prior to this commit, it wasn't possible to check whether the output had any listeners (if it was being observed). Before
output(), we could check whetherEventEmitterhad any listeners by checking itsobservedproperty.In this commit, we convert
listenersfrom a list to a signal so that we can have a computedobservedsignal.This commit only updates the
OutputEmitterRefimplementation. We can't updateOutputFromObservableRefbecause it expects an observable signature to be provided, and the observable doesn't technically have any API to count subscribers; onlySubjectdoes.Please note that there have been different discussions on whether the
OutputEmitterRefshould be extended or not. This change is not considered breaking because the signature of the private property has been changed and another property has been added to the public API.Related issue: #54837