[Question] Using EventEmitter to manipulate data and return an Observable.
See original GitHub issueI have asked this in the gitter room, but there were no responses, so I thought I might ask this here.
It looks like if we want to work with Rx.Observables, we’ll have to use the EventEmitter class.
I was wondering how to do the following:
- My component calls a service passing something by parameter;
- My service modifies the parameter;
- My service returns an Observables so that my Component can observe and subscribe to it;
If I am using Rx.Observable (by importing it directly - import * as Rx from 'rx'
), I can do the following:
// MyService
export class MyService {
static doSomething(something: Object):Rx.Observable {
return Rx.Observable.create(o => {
something.createdAt = Date.now();
o.onNext(something);
o.onCompleted();
})
}
}
And then in my component:
// MyComponent
class MyComponent {
doSomethingHandler(something:Object):void {
MyService.doSomething(something).subscribe(info => console.log(info));
}
}
So, that’d work.
But how to do it with the EventEmitter wrapper?
I saw it returns a Subject
instead of an Observable
, and it looks like I’d have to pass callbacks left and right to make it work - but I’d like to have the logic separated - having the component to only call the service, and this one would do the heavy work, returning something already manipulated to the component.
Issue Analytics
- State:
- Created 8 years ago
- Comments:20 (12 by maintainers)
Top Results From Across the Web
Delegation: EventEmitter or Observable in Angular
A Subject is both an Observable (so we can subscribe() to it) and an Observer (so we can call next() on it to...
Read more >Observable - RxJS
Observables may act like EventEmitters in some cases, namely when they are multicasted using RxJS Subjects, but usually they don't act like EventEmitters....
Read more >Everything You Need To Know About RxJS in Angular With ...
The EventEmitter class is essentially an RxJS observable stream which powers component and directive outputs – extends the RxJS subject class. The subject...
Read more >How Do I Create A Simple Event Emitter? | RxJS
How might one implement this using the Reactive Extensions for JavaScript? Using an Rx.Subject will solve this problem easily. As you may remember,...
Read more >48 answers on StackOverflow to the most popular Angular ...
I gathered the most common questions and answers from Stackoverflow. ... With Observable it doesn't matter if you want to handle 0, 1,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
So, to get things straight, this is NOT what you mean?
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.