Friday 17 March 2017

Why can't I capture events from an extended EventEmitter class in Node.js?

I've been banging my head against why I can't capture events emitted from this code in Node.js. My gut feeling is something is out of scope, but I can't figure out what it is. The console.log line of code in index.js never gets executed. Is my event not being emitted?

player.js

const EventEmitter = require('events').EventEmitter;

class rocketPlayer extends EventEmitter {
  constructor() {
    super(options);
  }

  update(connection) {
    //do some logic with playlists
    this.play(connection);
  }

  play(connection) {
    // do some stuff
    this.emit('nowPlaying',song.title);
  }
}

module.exports = rocketPlayer

index.js

const rocketPlayer = require('./player.js');
const player = new rocketPlayer();

player.on('nowPlaying', title => {
  console.log('caught nowPlaying event');
}

//define a connection
player.update(connection)



via Patrick Gray

No comments:

Post a Comment