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.

[Question] Using EventEmitter to manipulate data and return an Observable.

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:20 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
woppa684commented, Aug 12, 2015

So, to get things straight, this is NOT what you mean?

import {Component, View, bootstrap, EventEmitter} from 'angular2/angular2'


class MyService {
    static doSomething(something: any): Rx.Observable<any> {
        var e = new EventEmitter();

        window.setTimeout(() => {
            something.createdAt = Date.now();
            e.next(something);
        }, 2000);

        return e.toRx();
    }
}

@Component({
    selector: 'main'
})
@View({
    template: ``
})
class Main {
    constructor() {
        MyService.doSomething({}).subscribe(info => console.log(info));
    }
}

bootstrap(Main, [MyService]);
0reactions
angular-automatic-lock-bot[bot]commented, Sep 7, 2019

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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