The Notes model as an EventEmitter class

The EventEmitter is the class that implements listener support. Let's create a new module, models/notes-events.mjs, containing the following:

import EventEmitter from 'events'; class NotesEmitter extends EventEmitter {    noteCreated(note)  { this.emit('notecreated', note); }    noteUpdate (note)  { this.emit('noteupdate', note); }    noteDestroy (data) { this.emit('notedestroy', data); }} export default new NotesEmitter(); 

This module maintains the listeners to Notes-related events for us. We've created a subclass of EventEmitter because it already knows how to manage the listeners. An instance of that object is exported as the default export. 

Let's now update models/notes.mjs to use notes-events to emit events. ...

Get Node.js Web Development - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.