-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventprogram.js
98 lines (77 loc) · 3.53 KB
/
eventprogram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//https://nodejs.org/api/events.html#events_eventemitter_listenercount_emitter_eventname
//Node uses event driven programming. Below is an example of same.
/*
EventEmitters -> Events -> Event loop -> EventHandlers
*/
//Import events module
var events = require("events");
// Create an eventEmitters object
var eventEmitter = new events.EventEmitter();
//create an event handlers as follows
var connectHandler = function connected(){
console.log("");
console.log("listener1 executed: connection successful");
console.log("fires data_recieved event");
//handle the event
eventEmitter.emit('data_recieved');
}
//Bind the data_recieved events
eventEmitter.on('data_recieved', function data_rec(){
console.log("listening 'data_recieved' event- data recieved successfully");
});
// Bind the data_recieved event with anonymous function
eventEmitter.on('data_recieved', function(){
console.log("listening 'data_recieved' event- anonymous function- data recieved successfully");
console.log("-------------------");
});
//Bind the connection event with the handlers
console.log("");
console.log("binding 'connection' event with listener1: connectHandler");
eventEmitter.on('connection', connectHandler);
//Fire the connection events
console.log("firing 'connection' event");
eventEmitter.emit('connection');
console.log("");
//Create a listener i.e listener2
var listener2 = function lisetner2(){
console.log("listener2 executed");
}
//Adding listeners to the event
eventEmitter.addListener('listener2', listener2);
//Check the listener counts
var eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listener(s) listening to connection event");
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'listener2');
console.log(eventListeners + " Listener(s) listening to listener2 event");
console.log("");
//Fire the connection event again
console.log("Fire connection event:");
eventEmitter.emit('connection');
console.log("Fire listener2 event: No event handler added to it.");
eventEmitter.emit('listener2');
console.log("-------------------");
console.log("");
//Remove the binding of the listner2
eventEmitter.removeListener('connection', connectHandler);
console.log("Removing connectionHandler for event 'connection' - it will now not listen for the event.");
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listener(s) listening to connection event");
console.log("");
//one time listen and then removed
console.log("adding one time listener which gets removed after its execution");
eventEmitter.once('connection',connectHandler);
console.log("Getting count of listener again.");
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listener(s) listening to connection event after creation on one time event listener");
console.log("<------------------->");
console.log("firing 'connection' event");
eventEmitter.emit('connection');
console.log("one time listener - connection gets removed now");
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listener(s) listening to connection event");
console.log("");
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'listener2');
console.log(eventListeners + " Listener(s) listening to listener2 event");
console.log("<------------------->");
console.log("Program Ended.");
console.log("");