47099

Node ES6 class event emitter function?

I'm trying to create a class for which all instances respond to an event:

const events = require("events"); const eventEmitter = new events.EventEmitter(); class Camera { constructor(ip) { this.ip = ip; } eventEmitter.on("recordVideo", recordClip); recordClip() { console.log("running record video"); } } // emit event once a minute setInterval(function(){ eventEmitter.emit('recordVideo'); }, 1000*60);

The recordClip function never seems to be called. Is this possible?

I also tried running this.recordClip instead of recordClip.

Answer1:

Move it inside the constructor.

const events = require("events"); const eventEmitter = new events.EventEmitter(); class Camera { constructor(ip) { this.ip = ip; eventEmitter.on("recordVideo", this.recordClip.bind(this)); } recordClip() { console.log("running record video"); } }

Recommend

  • Angular 6 FormGroup.disable() method is not working with my template driven NgForm
  • why @output not working in angular 2?
  • when trying to configure or rebuild node-gyp, getting errors: mac osx mavericks
  • Child component not emitting value to parent component's function
  • sails-mongo auth error in sails 0.10
  • Error: socket hang up
  • Angular2 fire click event on unknown html element
  • Error while adding android platform for cordova
  • Resource for understanding view, projection, 'virtual camera' in OpenGL or graphics in gen
  • Unable to scan Code 128
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • How to resolve docker host names (/etc/hosts) in containers
  • LNK1104: cannot open file 'kernel32.lib'
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • Phonegap GA plugin not working
  • Galaxy S4 Android Custom Camera Issue
  • Invert string in Rust
  • Less Conflicting Session Manager for Zope 2
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • calculate gradient output for Theta update rule
  • rspec simple example getting error on request variable in integration test
  • How can I set a binding to a Combox in a UserControl?
  • Create DicomImage from scratch using Dcmtk
  • how to display data from 1st point on words on y axis for line chart in d3.js
  • How to attach a node.js readable stream to a Sendgrid email?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Cannot resolve symbol 'MyApi'
  • Spray.io: When (not) to use non-blocking route handling?
  • WinForms: two way TextBox problem
  • sending/ receiving email in Java
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app