Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Last active January 16, 2019 15:42
Show Gist options
  • Save nhalstead/92208c5f61735659c392ebe7ce9d1f03 to your computer and use it in GitHub Desktop.
Save nhalstead/92208c5f61735659c392ebe7ce9d1f03 to your computer and use it in GitHub Desktop.
Handle Controller
/**
* You can use this to create callbacks by event names
* Use .on events to listen for the selected event
* Use .handle followed by the triggering event to execute
* all of the matching events
*
* @author Noah Halstead <[email protected]>
*/
function EventSpooler(){
this.Events = {};
this.OnceEvents = {};
}
/**
* Trigger all matching Events
*
* @param String Trigger Event Name
* @param Mixed Extra Data to pass to the calling function
*/
EventSpooler.prototype.handle = function(name, data){
data = data?data:null;
if(typeof this.Events[name] == "undefined"){
this.Events[name] = [];
}
if(typeof this.OnceEvents[name] == "undefined"){
this.OnceEvents[name] = [];
}
// Handle Always run events
var funcList = this.Events[name];
funcList.forEach(function(calling){
calling[0](data, name);
});
// Handle Run Once Events
var funcListOnce = this.OnceEvents[name];
funcListOnce.forEach(function(calling){
var i = funcListOnce.indexOf(calling);
calling[0](data, name);
if (i > -1) {
funcListOnce.splice(i, 1); // Drop the Element from the Run Once
}
});
this.OnceEvents = funcListOnce;
}
/**
* Add a trigger action
*
* @param String Trigger Name
* @param Function Function to call when trigger is being called
*/
EventSpooler.prototype.on = function(name, callback){
if(typeof this.Events[name] == "undefined"){
this.Events[name] = [];
}
if(typeof callback == "function"){
// Add Evevnt Action to the Callback System
this.Events[name].push([ callback ]);
return true;
}
else {
console.log("Can not handle "+ (typeof callback) +" callbacks")
return false;
}
}
/**
* Add a trigger action, Run Once
* It is removed from the Run Once after it is triggered Once
*
* @param String Trigger Name
* @param Function Function to call when trigger is being called
*/
EventSpooler.prototype.once = function(name, callback){
if(typeof this.OnceEvents[name] == "undefined"){
this.OnceEvents[name] = [];
}
if(typeof callback == "function"){
// Add Evevnt Action to the Callback System
this.OnceEvents[name].push([ callback ]);
return true;
}
else {
console.log("Can not handle "+ (typeof callback) +" callbacks")
return false;
}
}
/**
* You can use this to create callbacks by event names
* Use .on events to listen for the selected event
* Use .handle followed by the triggering event to execute
* all of the matching events
*
* @author Noah Halstead <[email protected]>
*/
function EventSpooler(){this.Events={},this.OnceEvents={}}EventSpooler.prototype.handle=function(n,t){t=t||null,void 0===this.Events[n]&&(this.Events[n]=[]),void 0===this.OnceEvents[n]&&(this.OnceEvents[n]=[]),this.Events[n].forEach(function(e){e[0](t,n)});var e=this.OnceEvents[n];e.forEach(function(o){var s=e.indexOf(o);o[0](t,n),s>-1&&e.splice(s,1)}),this.OnceEvents=e},EventSpooler.prototype.on=function(n,t){return void 0===this.Events[n]&&(this.Events[n]=[]),"function"==typeof t?(this.Events[n].push([t]),!0):(console.log("Can not handle "+typeof t+" callbacks"),!1)},EventSpooler.prototype.once=function(n,t){return void 0===this.OnceEvents[n]&&(this.OnceEvents[n]=[]),"function"==typeof t?(this.OnceEvents[n].push([t]),!0):(console.log("Can not handle "+typeof t+" callbacks"),!1)};
@nhalstead
Copy link
Author

Demo

<script type="text/javascript">
  // Generate a New Spooler
  var tSpool = new EventSpooler();

  // Add Event Listener
  tSpool.on("go", function(){
    console.log("24");
  });

  // Add Event Listener
  tSpool.on("go", function(){
    console.log("25");
  });

  // Add Event Listener BUT only Run Once
  tSpool.once("go", function(){
    console.log("Once");
  });

  // Call a Trigger with the Given Event.
  tSpool.handle("go");
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment