Created
January 26, 2018 21:05
-
-
Save caracal7/b597c68ffe0a336f9d96da707370043f to your computer and use it in GitHub Desktop.
This is a basic, cross-platform, valid Node.js EventEmitter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
o, // object to add the event emitter to | |
i, a // placeholder's | |
) { | |
o.A = e = {}; // events holder | |
o.B = function ( // set a new event listener | |
n, // event name | |
f // listener | |
) { | |
(e[n] = e[n] || []) // if event dousnt exist, create it | |
.push(f) // adds listener | |
}; | |
o.C = function ( // event emit | |
n // name of event to run | |
) { | |
for ( | |
i = 0; // Start at first event | |
a = (e[n] || [])[i]; // every loop, set listener to run | |
i++ // Increment index by 1 | |
) a.apply(this,arguments); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Just a more condensed, easy-to-read version | |
function ( o, i, a ) { | |
o.A = e = {}; | |
o.B = function ( n, f ) { | |
(e[n] = e[n] || []).push(f) | |
}; | |
o.C = function ( n ) { | |
for (i = 0; a = (e[n] || [])[i]; i++) a.apply(this,arguments); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(o,i,a){o.A=e={};o.B=function(n,f){(e[n]=e[n]||[]).push(f)};o.C=function(n){for(i=0;a=(e[n]||[])[i];i++)a.apply(this,arguments);}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Vitron Prince | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "EventEmitter", | |
"description": "This is a basic, cross-platform, valid Node.js EventEmitter.", | |
"keywords": [ | |
"EventEmitter", | |
"events", | |
"emitter", | |
"140bytes", | |
"nodejs" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<title>EventEmitter example</title> | |
<!-- Clock: is a button input to enable on/off --> | |
<input type="button" id="clock" value="00:00:00" /> | |
<!-- Clock stats --> | |
<div><b>Seconds passed: </b><i id="second">0</i></div> | |
<div><b>Minutes passed: </b><i id="minute">0</i></div> | |
<div><b>Hours passed: </b><i id="hour">0</i></div> | |
<!-- js code --> | |
<script> | |
// the event emitter | |
var EventEmitter = function(o,i,a){o.A=e={};o.B=function(n,f){(e[n]=e[n]||[]).push(f)};o.C=function(n){for(i=0;a=(e[n]||[])[i];i++)a.apply(this,arguments);}}, | |
// returns the current time | |
fix_digit = function ( i ) { return i < 10 ? '0' + i : i; }, | |
time = function ( ) { | |
var d = new Date(), f = fix_digit; | |
return [f(d.getHours()), f(d.getMinutes()), f(d.getSeconds())].join(':'); | |
}, | |
// for the sake of lesser code in the demo | |
domID = function ( id ) { return document.getElementById(id); }, | |
echo = function ( id, str ) { domID(id).innerHTML = str; }, | |
// the clock demo | |
clock = {}; | |
EventEmitter(clock); | |
clock.on = clock.B; // `B` is the default name for EventEmitter.on | |
clock.emit = clock.C; // `C` is the default name for EventEmitter.emit | |
clock.timer = null; | |
clock.passed = -500; | |
clock.lastSec = 0; | |
clock.lastMin = 0; | |
clock.lastHour = 0; | |
clock.start = function ( ) { | |
var C = this, now = 0, emit = function ( name ) { clock.emit(name, now, clock.passed); }; | |
function show ( ) { | |
now = time(); | |
C.passed += 500; | |
C.lastSec++; | |
emit('second'); | |
if ( (30000 * C.lastMin) === C.passed ) { | |
C.lastMin++; | |
emit('minute'); | |
if ( (1800000 * C.lastHour) === C.passed ) { | |
C.lastHour++; | |
emit('hour'); | |
} | |
} | |
C.start(); | |
clock.emit('start'); | |
} | |
this.timer = setTimeout(show,500); | |
}; | |
clock.stop = function ( ) { | |
clearTimeout(this.timer); | |
clock.emit('stop'); | |
}; | |
// start the demo | |
clock.on('start',function(e,now){ | |
var btn = domID("clock"); | |
btn.onclick = function(){clock.stop();}; | |
btn.title = 'click to stop...'; | |
}); | |
clock.on('stop',function(e,now){ | |
var btn = domID("clock"); | |
btn.onclick = function(){clock.start();}; | |
btn.title = 'click to start...'; | |
}); | |
clock.on('second',function(e,now){ | |
domID("clock").value = now; | |
echo("second", clock.lastSec); | |
}); | |
clock.on('minute',function(){ | |
echo("minute", clock.lastMin); | |
}); | |
clock.on('hour',function(){ | |
echo("hour", clock.lastHour); | |
}); | |
clock.start(); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// attach this to the global Object, so that any js object can access | |
Object.prototype.useEmitter = function ( overwrite ) { | |
if ( !('_events' in this) || overwrite === true ) EventEmitter(this); | |
}; | |
// another version, independent of the original function | |
Object.prototype.useEmitter = function ( overwrite, a,e,i ) { | |
if ( !('_events' in this) || overwrite === true ) { | |
this._events = e = {}; | |
this.on = function ( n, f ) { | |
(e[n] = e[n] || []).push(f) | |
}; | |
this.emit = function ( n ) { | |
for (i = 0; a = (e[n] || [])[i]; i++) a.apply(this,arguments); | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fucking awesome license!