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. ...