Last active
October 21, 2016 12:17
-
-
Save Purexo/0d27aad481158047ae852dc10d779cd9 to your computer and use it in GitHub Desktop.
A simple event manager JS (browser) with an simple API like socket.io : on and emit function
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
'use strict'; | |
/** | |
* SimpleEventManager is a simple API for create and listen Event | |
* It's a superset of DOM Event listening API | |
* | |
* Example : | |
let event = new SimpleEventManager(function(lastState) { | |
// watched variable | |
let actualNbThings = document.querySelectorAll('.things').length; | |
// verify if it changed | |
if (lastState.nbThings != actualNbThings) { | |
// emit event | |
this.emit('nbthings-changed', { | |
last: lastState.nbThings, | |
actual: actualNbThings | |
}); | |
// actualyze variable | |
lastState.nbThings = actualNbThings | |
} | |
}); | |
// log data and event on change | |
event.on('nbthings-changed', function(data, event) { | |
console.log(data); | |
console.log(event); | |
}); | |
// create new .things element. | |
let div = document.createElement('div'); | |
div.className = 'things'; | |
document.body.appendChild(div); | |
// or simply | |
let event = new SimpleEventManager(); | |
event.on('take-that-bitch', function(data) { | |
console(data.msg); | |
}) | |
event.emit('take-that-bitch', { | |
msg: 'goal !' | |
}) | |
// or that, eventLoop management | |
let event = new SimpleEventManager(); | |
// log data and event on change | |
event.on('nbthings-changed', function(data, event) { | |
console.log(data); | |
console.log(event); | |
}); | |
event.start(function(lastState) { | |
// watched variable | |
let actualNbThings = document.querySelectorAll('.things').length; | |
// verify if it changed | |
if (lastState.nbThings != actualNbThings) { | |
// emit event | |
this.emit('nbthings-changed', { | |
last: lastState.nbThings, | |
actual: actualNbThings | |
}); | |
// actualyze variable | |
lastState.nbThings = actualNbThings | |
} | |
}, 1000) | |
document.querySelector('#btn-stop-event').addEventListener('click', function() { | |
event.stop(); | |
}); | |
document.querySelector('#btn-start-event').addEventListener('click', function() { | |
event.start(); | |
}); | |
*/ | |
class SimpleEventManager { | |
/** | |
* eventLoop : function(lastState) - binded this : SimpleEvent instance (Optional) | |
* - save eventLoop in instance for futur usage | |
* refresh : number in ms (Optional, 60 by default) | |
*/ | |
constructor(eventLoop, refresh) { | |
this.lastState = {}; | |
this.start(eventLoop, refresh); | |
} | |
/** | |
* Stop the eventLoop | |
*/ | |
stop() { | |
clearInterval(this.idRefresh); | |
} | |
/** | |
* Start a new eventLoop with another refresh time | |
* - if eventLoop not precise, take the eventLoop of constructor | |
* - if no saved eventLoop, start nothing | |
* eventLoop : function(lastState) - binded this : SimpleEvent instance (Optional) | |
* - save eventLoop in instance for futur usage | |
* refresh : number in ms (Optional, 60 by default) | |
*/ | |
start(eventLoop, refresh) { | |
if (typeof eventLoop == 'function') { | |
this.eventLoop = eventLoop; | |
} | |
if (typeof this.eventLoop == 'function') { | |
this.idRefresh = setInterval(_ => this.eventLoop.call(this, this.lastState), refresh || 60); | |
} | |
} | |
/** | |
* Stop eventLoop, restart with another refresh time | |
* - if no eventLoop precised, take the eventLoop of constructor | |
* - if no saved eventLoop, restart nothing | |
* refresh : number in ms (Optional, 60 by default) | |
* eventLoop : function(lastState) - binded this : SimpleEvent instance (Optional) | |
* - save eventLoop in instance for futur usage | |
*/ | |
restart(refresh, eventLoop) { | |
this.stop(); | |
this.start(eventLoop, refresh); | |
} | |
/** | |
* name : string (event type) | |
* data : any (some data to transmit to event listener) (Optional) | |
*/ | |
emit(name, data) { | |
var event = new Event(name); | |
event.data = data; | |
document.dispatchEvent(event); | |
} | |
/** | |
* name : string (event type you want catch) | |
* callback : function(data, event) for make some nasty thing with catched event - binded this : SimpleEvent instance | |
*/ | |
on(name, callback) { | |
document.addEventListener(name, event => callback.call(this, event.data, event)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment