Showing
2 changed files
with
14 additions
and
21 deletions
1 | -const EventEmitter = require('events'); | 1 | +const Logger = require('./logger'); |
2 | -const emitter = new EventEmitter(); | 2 | +const logger = new Logger(); |
3 | 3 | ||
4 | -// Register a listener | 4 | +logger.on('messageLogged', (arg) => { |
5 | -emitter.on('messageLogged', (arg) => { | ||
6 | console.log('Listener called',arg); | 5 | console.log('Listener called',arg); |
7 | }); | 6 | }); |
8 | 7 | ||
9 | -const log = require('./logger'); | 8 | +logger.log('message'); |
10 | -log('message'); | ||
11 | - | ||
12 | - | ||
13 | - | ||
14 | - | ||
15 | - | ... | ... |
1 | const EventEmitter = require('events'); | 1 | const EventEmitter = require('events'); |
2 | -const emitter = new EventEmitter(); | ||
3 | var url = 'http:mylogger.io/log'; | 2 | var url = 'http:mylogger.io/log'; |
4 | 3 | ||
5 | -function log(message) { | 4 | +// function in class won't need function keyword |
6 | - // Send an HTTP request | 5 | +class Logger extends EventEmitter { |
7 | - console.log(message); | 6 | + log(message) { |
8 | - emitter.emit('messageLogged', {id: 1, url: 'http://' }); | 7 | + // Send an HTTP request |
9 | -} | 8 | + console.log(message); |
10 | - | ||
11 | - | ||
12 | - | ||
13 | 9 | ||
10 | + //Raise an event | ||
11 | + this.emit('messageLogged', {id: 1, url: 'http://' }); | ||
12 | + } | ||
13 | +} | ||
14 | 14 | ||
15 | -module.exports = log; | 15 | +module.exports = Logger; | ... | ... |
-
Please register or login to post a comment