Understanding Modular Events in NodeJS


#1

I'm following through an edX NodeJS Course as a bit of a refresher at the moment, but the material below isn't gelling for me at the moment, particularly how weekly.js is customising job.js. Could someone give me a bit of guidance on what is going on?


Modular Events

The observer pattern is often used to modularize code. A typical usage is to separate the event emitter class definition and the event emission into its own module but allow the observers to be defined in the main program. This allows us to customize the module behavior without changing the module code itself.

In job.js, we use a generic job module that emits a done event after 700ms. However, in weekly.js, we can customize the event handler of the observer to do whatever we want once a done event is triggered.

job.js:

const EventEmitter = require('events')
class Job extends EventEmitter {
  constructor(ops) {
    super(ops)
    this.on('start', ()=>{
      this.process()
    })
  }
  process() {
     setTimeout(()=>{
      // Emulate the delay of the job - async!
      this.emit('done', { completedOn: new Date() })
    }, 700)
  }
}

module.exports = Job

weekly.js:

var Job = require('./job.js')
var job = new Job()

job.on('done', function(details){
  console.log('Weekly email job was completed at',
    details.completedOn)
})

job.emit('start')

When you run weekly.js, the custom logic pertaining to the done event will be executed from weekly.js. This way the creators of the job.js module can keep the module flexible. They don't have to hard code any logic for the done event in the module. Consumers of the module job.js, people who write weekly.js, have the power to customize the behavior for the done event, and not only for that event. Event emitters can have multiple events: in the middle, at the start, in the end, etc. They can be called (emitted or triggered) multiple times and with data (passed as the second argument to emit() as can be seen in job.js). Furthermore, there are methods to list or remove event listeners (observers) or to specify the execution of an event listener (observer) just once (.once() method).



#2

Weekly isn't customing the job. Instead it's waiting for an event to be emitted from the job.

      this.emit('done', { completedOn: new Date() })

That done event has attached to it an object that contains completedOn information, which weekly can then use.

job.on('done', function(details){
  console.log('Weekly email job was completed at',
    details.completedOn)
})

I would though reorder the weekly code, so that starting the job occurs before waiting for the job to end. That way it becomes slightly less confusing.

job.emit('start')

job.on('done', function(details){
  // ...
})

#3

Slightly confusing wording on their part I think.

Presented like that, it does make more sense. Think it will need a few more practical examples before it becomes a bit more natural.

Thanks Paul


#4

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.