question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

3reactions
atscottcommented, May 25, 2021

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.

2reactions
arch-daemonecommented, Jan 21, 2021

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:

class Component {
  @Output() entityIdChange: Observable<number> = this.entityFacade.entityId$;
}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found