rxjs Observable or Subject instead EventEmitter
See original GitHub issueπ Docs or angular.io bug report
Description
In the times of Angular v2, there was a semi-official statement not to use rxjs Observable
or Subject
directly instead of EventEmitter
because the implementation of EventEmitter
might change in the future.
I am wondering if this is still valid today, given that rxjs is a first-class citizen of Angular and any serious project based on Angular?
I found myself always wishing to use observable or piped observable directly in my reactive component instead of going out of my way to call EventEmitter.emit
.
If the statement is still valid, could it perhaps be valuable to be documented?
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (11 by maintainers)
Top Results From Across the Web
Why EventEmitters instead of Subjects in Angular
Recently I was thinking about how Angular uses the awesome library RxJS, and specifically when observable objects are created and expected.
Read more >Difference Between Observable, Eventemitter, and Subject
It is recommended to use Subject for transferring data from one component to another as EventEmitter extends Subject, adding a method emit().
Read more >Angular 2 Event emitters vs Subject - Stack Overflow
EventEmitter by default is synchronous, whereas Subject is not. You can pass a flag to EventEmitter to make it asynchronousΒ ...
Read more >Event Emitter vs Service vs Subject( Cross component ...
A subject is a special kind of observable. It maintains the stream of data which is maintained actively. A subject is an observer...
Read more >EventEmitter vs. Observable Angular 10 - CodingCuriosities
EventEmitters are an awesome object extending the RxJs Subjects. They can be hooked up to the @Output decorator in typescript. However, it's easy...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Discussed with @jelbourn and we think this isnβt something that needs official documentation or guidance. There are myriad of ways to not do something and we shouldnβt attempt to document them. Our docs give guidance on the provided APIs, not a list of unsupported use-cases.
As far as the question of using something other than
EventEmitter
goes, as long as you follow the interface youβre more or less safe. Itβs just outside the official guidance and will not have official support. That said, itβs highly unlikely to change and even if it did, youβd know at compile-time so itβs more or less safe, relatively speaking.Itβs not so much the EventEmitterβs public API thatβs in question here. The question is whether weβre strictly allowed to assign plain Observables to class members that are attached to
@Output
attributes, instead of EventEmitters. To add an explicit type annotation to my earlier snippet:The place to put such a statement might be somewhere like the docs on communication between components, and might be an βalertβ box of some sort that says something like, βWe use EventEmitters in these examples because they provide the
emit
method, making it easy to emit a value from anywhere in the componentβs code or template. But if the only values to be emitted are all coming from a single Observable, it may make more sense to simply assign that Observable directly to the@Output
field. This is possible, and works as you would expect.β Maybe with a StackBlitz example showing that it does indeed work, including that it is unsubscribed on teardown.