Search code examples
javascriptnode.jsvue.jseventemitter

Emitting Event from node_modules to vue


How can I receive an event emitted by a node_module? Want to receive it on a vue file.

Here is from the js node_module:

const EventEmitter = require('events')

class Find extends EventEmitter {
  // some codes here
}
class FindInPage extends Find {
  constructor (webContents, options = {}) {
    super(webContents)
  }
  initialize () {
    this.on('onValueInput', (value) => {
      console.log('onValueInput received, value = ' + value)
    })
  }

  // emit event triggered somewhere after
  this.emit('onValueInput', this[findInput].value)
}

And on my vue file, I am instantiating the class from the js node_module and I want to receive the event:

this.findInPage = new FindInPage(this.$q.electron.remote.getCurrentWebContents(), configToApply)

this.findInPage.on('onValueInput', (value) => {
  console.log('onValueInput received from module, value = ' + value)
})

But I'm not receiving the event onValueInput. It works well just inside the js module. Help please!


Solution

  • This now works. Apparently, it was just a case of misplaced call. Thanks guys.