Skip to content

Commit ee50fc4

Browse files
committed
small
1 parent 2091124 commit ee50fc4

28 files changed

+702
-2
lines changed
File renamed without changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const EventEmitter = require('./libs/EventEmitter')
2+
3+
function init() {
4+
// I'm creating a stand alone object, but you could create a new class which extends it using prototypical inheritence.
5+
var ee = new EventEmitter();
6+
7+
// Add some listeners.
8+
ee.addListener('create-user', sendCreateRequest);
9+
ee.addListener('request-complete', displayResponse);
10+
11+
// These are the callback functions.
12+
function sendCreateRequest(name) {
13+
// Somewhere else in the code handles the request...
14+
sendRequest('create', arguments, function(success, response) {
15+
// When the request is complete, trigger the event and pass it the response.
16+
ee.emitEvent('request-complete', arguments);
17+
18+
// emitEvent takes an array of arguments as its second parameter.
19+
// You can also use emit if you prefer the old style API:
20+
// ee.emit('request-complete', success, response);
21+
// The new array method is a lot more versatile.
22+
});
23+
}
24+
25+
function displayResponse(success, response) {
26+
console.log(success)
27+
28+
// Show a popup based on the arguments...
29+
if (success) {
30+
console.log('Yay!');
31+
}
32+
else {
33+
console.log('Boo!');
34+
}
35+
}
36+
37+
function sendRequest(type, data, callback) {
38+
// Do request magic here...
39+
callback(true, {name: data[0]});
40+
}
41+
42+
// Kick everything off by emitting a create-user event.
43+
ee.emitEvent('create-user', ['Oliver Caldwell']);
44+
}
45+
46+
init()

0 commit comments

Comments
 (0)