Skip to content

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

@ericmdantas

Description

@ericmdantas

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions