0

This is app.js file;

const MyTest = require('./testApp');
const App = new MyTest();

App.on('ready', () => {
    console.log('Hello World');
});

Package.json file. (in) ./testApp/package.json;

{
...
"main": "main.js"
...
}

This is ./testApp/main.js file;

const EventEmitter = require('events'); // node event module

class Client extends EventEmitter {
  constructor() {
    super();
    this.sendReady();
  }
  sendReady() {
   this.emit('ready');
  }
}

module.exports = Client;

If "App" is successfully installed in the app.js file, I want the console to write " Hello World". I've changed a few times this content because I want to more simple.

I want to trigger App.on in app.js from ./testApp/main.js

If I define a function in the constructor, I can get it by calling "App." In this case this = App

Then, why don't emit anything?

Note: this app files working locally.

Yankı Küçük
  • 3
  • 1
  • 1
  • 5

1 Answers1

0

The emit call is in Client's constructor, i.e. it gets called when you invoke

const helper = new pulseHelper.Client();

However, you only register the ready handler after that. EventEmitters don't buffer their events; only listeners registered during the emit call will ever be able to act on the events.

EDIT: to clarify what I'm talking about, see this example:

const EventEmitter = require('events');

class Client extends EventEmitter {
  constructor() {
    super();
    this.emit('ready', 'I\'m Ready!');
  }
  boom() {
    this.emit('ready', 'Boom!');    
  }
  emit(event, payload) {
    console.log('Emitting', event, ':', payload);
    super.emit(event, payload);
  }
}

const c = new Client();
c.on('ready', (payload) => console.log(payload));
c.boom();

This prints (with annotations):

# client constructed, it emits an event but no one is listening.
# however the "debug" emit() implementation prints this:
Emitting ready : I'm Ready!
# the listener has been registered, now let's call .boom()!
# the "debug" emit() implementation prints this:
Emitting ready : Boom!
# and the handler prints the following:
Boom!
AKX
  • 152,115
  • 15
  • 115
  • 172
  • `this.emit(Events.READY, 'I\'m Ready!');` doesn't work for me. how can i call this listener? – Yankı Küçük Oct 26 '17 at 09:28
  • "helper.on(.." doesn't work in app.js file because "helper" is not defined in "client" constructor. I guess it needs to be something like `this.helper.emit('ready', 'I\'m Ready!');` – Yankı Küçük Oct 26 '17 at 10:24
  • and yes it works but it is not my answer. because this locally works. not triggered **helper.on** in app.js. question is "how to emit(triggered) an event **external** js file. but thanks btw. – Yankı Küçük Oct 26 '17 at 13:31
  • I made the content much simpler. – Yankı Küçük Oct 27 '17 at 07:33
  • @YankıKüçük The point _still_ is that if you emit events in the `constructor()`, or functions called from the `constructor()`, they won't have listeners installed yet. If you add `App.sendReady();` after the `App.on(...)` clause, you will see the `console.log()`. – AKX Oct 27 '17 at 09:08