Skip to content

Commit 991cc09

Browse files
Ian-ReadIan-Read
authored andcommitted
Events examples
1 parent 09fdd0a commit 991cc09

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

events/READEME.md

Whitespace-only changes.

events/example-advanced.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// import the base? class, EventEmitter from the events module
2+
const { EventEmitter } = require('events')
3+
4+
// create some listener functions to handle when the events occur.
5+
// `foo` is expecting two parameters to be passed in.
6+
const foo = function foo(baz, qux) {
7+
console.log(`foo executed. baz: ${baz}, qux: ${qux}`)
8+
}
9+
10+
const bar = function bar() {
11+
console.log('bar executed.')
12+
}
13+
14+
const errorProne = function foo(obj) {
15+
this.emit('error', 'I am just not going to work')
16+
}
17+
18+
// create an emitter and bind some events to it
19+
const eventEmitter = new EventEmitter()
20+
21+
// Bind the connection event with the listner1 function
22+
eventEmitter.addListener('foo', foo)
23+
24+
// N.B. `addListener` is the same as `on`.
25+
26+
// Bind the connection event with the listner2 function -
27+
// IMPORTANT!! Listeners are called synchronously in the order in which they were added.
28+
// - i.e. `foo` will be called, and `bar` will be called once `foo` has completed.
29+
// This is to prevent race conditions and logic errors.
30+
// They can switch to an async mode using `setImmediate`.
31+
32+
eventEmitter.once('bar', bar)
33+
eventEmitter.once('liveDangerously', errorProne)
34+
35+
// handle errors in one of the listeners handlers
36+
eventEmitter.on('error', (err) => {
37+
console.error('There was an error!')
38+
})
39+
40+
for(let i = 0; i < 3; i++ ) {
41+
eventEmitter.emit('foo', 'value1', 'value2')
42+
eventEmitter.emit('bar')
43+
eventEmitter.emit('liveDangerously')
44+
}
45+
46+
const eventListeners = EventEmitter.listenerCount(eventEmitter, 'foo')
47+
48+
console.log(EventEmitter.listenerCount(eventEmitter, 'connection') + " Listner(s) listening to connection event")
49+
console.log(eventListeners + " Listner(s) listening to connection event")
50+
console.log('end of file...')

events/example-simple.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// import the class EventEmitter from the events module
2+
const { EventEmitter } = require('events')
3+
4+
// create a listener function. These can be arrow functions, but will
5+
// loose `this` refering to the EventEmitter object
6+
const foo = function foo() {
7+
console.log('foo executed.', this)
8+
}
9+
10+
// create an emitter and bind some events to it
11+
const eventEmitter = new EventEmitter()
12+
13+
// Bind the connection event with the listner1 function
14+
eventEmitter.on('foo', foo)
15+
16+
eventEmitter.emit('foo')

events/index.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,32 @@ layout: default.njk
33
title: Events
44
---
55

6-
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhäuser Gate. All those moments will be lost in time, like control flow.
6+
Events
7+
8+
The Node.js core API is built around the idea of events being "emitted" and "listened" to. Objects called "emitters" emit _named_ events, that are picked up by "listener" functions.
9+
10+
Objects that emit events extend the `EventEmitter` class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object.
11+
12+
When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously, in the order they were added. This is to prevent race conditions and logic errors. They can switch to an async mode using `setImmediate`.
13+
14+
```
15+
const { EventEmitter } = require('events');
16+
17+
// create a listener function. These can be arrow functions, but will
18+
// loose `this` refering to the EventEmitter object
19+
const foo = function foo() {
20+
console.log('foo executed.', this)
21+
}
22+
23+
// create an emitter and bind some events to it
24+
const eventEmitter = new EventEmitter()
25+
26+
// Bind the connection event with the listner1 function
27+
eventEmitter.on('foo', foo)
28+
29+
// fire the event
30+
eventEmitter.emit('foo')
31+
```
32+
33+
Why?
34+

0 commit comments

Comments
 (0)