0

I am using eventemitters in class A and extending it to class B. But when I try to trigger the event from class B, it does not identify the event. Both class A and class B are injectables. I have to work around the situation by injecting the class A into class B.

Does Not Work

import { Injectable, EventEmitter } from '@angular/core';

@Injectable()
export class A {
  
  _sm: EventEmitter<any>;

  constructor() {
    this._sm = new EventEmitter();
  }
  
}

.

import { Injectable, EventEmitter } from '@angular/core';
import { A } from '../svc/class.a.service';
@Injectable()
export class B extends A {

  constructor() {
    super();
  }
  
  triggerEvent(){
    this._sm.emit({});
  }

}

===

Works

import { Injectable, EventEmitter } from '@angular/core';

@Injectable()
export class A {
  
  _sm: EventEmitter<any>;

  constructor() {
    this._sm = new EventEmitter();
  }
  
}

.

import { Injectable, EventEmitter } from '@angular/core';
import { A } from '../svc/class.a.service';
@Injectable()
export class B {

  constructor(private _a: A) {
    
  }
  
  triggerEvent(){
    this._a._sm.emit({});
  }

}

.

Update of usage

import { Component, OnInit } from '@angular/core';
import { B } from '../svc/class.a.service';

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: ['./menu.component.css']
})
export class MenuComponent implements OnInit {

  constructor(private _es: B) {
    this._es._sm.subscribe((data)=>{console.log('Testing Event')});
 }

}

Is there an issue with extending an Injectable EventEmitter class? The reason why I am not keeping it a simple non-injectable class is because there are two such more classes M and C extending the class A. I want same instances of classes and events being captured by components using B, M, and C should be the same ones. They are inter-used and subscribed with multiple listeners.

Community
  • 1
  • 1
Gary
  • 2,293
  • 2
  • 25
  • 47
  • What does 'does not identify the event' mean? The question is pretty much pointless without http://stackoverflow.com/help/mcve , because these are simple classes and they should work as expected. Also, EventEmitter is supposed to be used for Angular outputs, for general purposes RxJS subjects should be used. – Estus Flask Nov 15 '17 at 03:14
  • Ah, no. This does not trigger. `triggerEvent(){ this._sm.emit({}); }` in the first example (does not work) . Extension of an eventemitter class and using the event from a child class is the issue. The app is already heavy and the initial load times are high. I am avoiding extra libraries. I am not doing async calls and is not a use case for RxJS. – Gary Nov 15 '17 at 03:19
  • How do you check that it 'triggers'? Please, provide http://stackoverflow.com/help/mcve , otherwise the question is off-topic that cannot be answered. A working example (plunk) that shows the behaviour you're having will be helpful. – Estus Flask Nov 15 '17 at 03:23
  • 1
    It *is* a use case for RxJS, Angular depends on RxJS and already uses Subject for EventEmitter implementation. Using EventEmitter for anything but Angular outputs is discouraged. – Estus Flask Nov 15 '17 at 03:26
  • The usage is simple - updated the code wit usage. Just need to use the core and the usage is within angular zones, so I do not think it should be a problem. RxJS for this app is a no. This is a simple app, don't want to complicate it. Keeping it an lean as possible. If its a subject, then I believe considering a custom event is better? – Gary Nov 15 '17 at 03:39
  • The example isn't really explanatory, triggerEvent never gets called. – Estus Flask Nov 15 '17 at 03:54
  • Give me some mins. Putting a plunker. – Gary Nov 15 '17 at 03:59
  • A quick question - does ng-cli do tree shaking for unused angular core sections? or it does tree shaking for application only? or no tree shaking happens? Angular CLI: 1.5.0, Node: 8.9.0, OS: win32 ia32, Angular: 5.0.0. I completely missed that RxJS/Subject is being imported for EventEmitter. I was presuming it allows non-RxJS implement. Just saw the source – Gary Nov 15 '17 at 03:59
  • 1
    Yes, tree-shaking is supposed to take care of it. EventEmitter currently extends Subject but is intended to be standalone implementation in future framework versions, so its behaviour (besides basic `emit` and `subscribe`) may change. – Estus Flask Nov 15 '17 at 04:07
  • 1
    Replace `export class B extends class A {` with `export class B extends A {` https://stackblitz.com/edit/angular-nuappq?file=app%2Fapp.component.html – yurzui Nov 15 '17 at 04:39
  • Yes, this event seems to be captured. Let me check my codes again. export class B extends A is the right one. It was my error in the question. Just changed the question. Thank you for your time – Gary Nov 15 '17 at 05:38
  • @estus - may be you should have two APIs - One standalone and one RxJS implementation. That would be my recommendation though. Let me check the Tree shaking part. I am unsure if that is happening. Are build options the same as normal? I don't see anything other than Vendor and css bundling. – Gary Nov 15 '17 at 05:41
  • 1
    Yes, Angular CLI should use tree shaking. You can inspect your bundles. E.g. with this tool https://github.com/webpack-contrib/webpack-bundle-analyzer – Estus Flask Nov 15 '17 at 05:54
  • Awesome. I was unaware of the analyser package. Thank you for your time. – Gary Nov 15 '17 at 05:56

0 Answers0