Skip to content

Create Emitter (as replacement for EventEmitter) #1250

@mhevery

Description

@mhevery

Current @EventEmitter has fallowing issues:

  • It pollutes the constructor.
  • It makes it difficult to unit test the code because it requires a lot of mock code.

Replacement

Use inline Emitter class which reverses the dependency.

class Emitter {
  listener; // we only support one
  constructor() {
    this.listener = null;
  }
  emit(data) {
    return if (this.listener) this.listener(data);
  }
}

Here is what the code would look like.

@Component({
  selector: 'my-component',
  events: ['open', 'close']
})
@View(...)
class MyComponent {
  open:Emitter;
  close:Emitter;

  constructor() {
    this.open = new Emitter();
    this.close = new Emitter();
  }
}

previous code:

@Component({
  selector: 'my-component'
  // events not listed here.
})
@View(...)
class MyComponent {
  open:Function;
  close:Function;

  constructor(@EventEmitter('open') open:Function, @EventEmitter('close') close:Function) {
    this.open = open;
    this.close = close;
  }
}

Rx and Streams

Angular should be able to consume not only Emitter but also Rx and Streams. (We prefer Emitter because it is much more light weight and synchronous)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions