Skip to content

Instantly share code, notes, and snippets.

@jiggliemon
Created March 24, 2011 03:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiggliemon/884484 to your computer and use it in GitHub Desktop.
Save jiggliemon/884484 to your computer and use it in GitHub Desktop.
Custom Event Queue for Objects.
;(function () {
var REGEX = /:(latch(ed$)?)/i;
function removeLatched(type){
if(type.indexOf(':')){
if(REGEX.test(type)){
type = type.replace(REGEX,'');
this._latched[type] = 1
}
}
return type;
}
window.Events = {
_latched: {}
,_switched: {}
,_events: {}
,_arguments: {}
,getEvents: function(key){
var events = this._events[key];
return (typeof key === 'string') ? events?events:[] : Object.keys(this._events);
}
,addCompoundEvent: function (events, type, callback) {
type = removeLatched.call(this,type);
var self = this;
events = events.map(function (event) {
event = removeLatched.call(self, event);
self.addEvent(event, fireCheck);
return event;
});
function fireCheck () {
var length = events.length;
while(length--){
if(!self._switched[events[length]]) return;
}
self.fireEvent(type+':latched');
}
if(callback){
this.addEvent(type, callback );
}
}
,addEvent: function(/* Sting */ type, /* Function */ callback){
if(Array.isArray(type)) {
return this.addCompoundEvent.apply(this, arguments);
}
var type = removeLatched.call(this,type)
,events = this._events[type] = this._events[type] || [];
if(typeof callback !== 'function') {
throw new TypeError('`#addEvent`\'s second argument must be a function');
}
(this._latched[type]) ? callback.apply(this,this._arguments[type]) : events.push(callback);
return this;
}
,addEvents: function(/* Object */ events){
for(var key in events){
if(events.hasOwnProperty(key)){
this.addEvent(key,events[key]);
}
}
return this;
}
,fireEvent: function(/* String */ type) {
var type = removeLatched.call(this,type)
,isLatched = this._latched[type]
,events = this._events[type]
,length = events ? events.length : 0
,args = Array.prototype.slice.call(arguments,1)
,i = 0;
this._switched[type] = 1;
if(events && length) {
for (; i < length; i++) {
if (i in events) {
try{
events[i].apply(this,args);
} catch (e) {
throw new Error('Event Error - '+type+':: '+ e);
}
}
}
}
if(isLatched){
this._arguments[type] = args;
delete events;
}
return this;
}
};
}());
define(['mediator/mixin'], function(EventsMixin){
function Mediator (){
this._events = {};
this._latched = {};
this._arguments = {};
this._switched = {};
}
Mediator.prototype = EventsMixin;
return Mediator;
});
define(function () {
var REGEX = /:(latch(ed$)?)/i;
function removeLatched(type){
if(type.indexOf(':')){
if(REGEX.test(type)){
type = type.replace(REGEX,'');
this._latched[type] = 1
}
}
return type;
}
var EventMethods = {
_latched: {}
,_switched: {}
,_events: {}
,_arguments: {}
,getEvents: function(key){
var events = this._events[key];
return (typeof key === 'string') ? events?events:[] : Object.keys(this._events);
}
,addCompoundEvent: function (events, type, callback) {
type = removeLatched.call(this,type);
var self = this;
events = events.map(function (event) {
event = removeLatched.call(self, event);
self.addEvent(event, fireCheck);
return event;
});
function fireCheck () {
var length = events.length;
while(length--){
if(!self._switched[events[length]]) return;
}
self.fireEvent(type+':latched');
}
if(callback){
this.addEvent(type, callback );
}
}
,addEvent: function(/* Sting */ type, /* Function */ callback){
if(Array.isArray(type)) {
return this.addCompoundEvent.apply(this, arguments);
}
var type = removeLatched.call(this,type)
,events = this._events[type] = this._events[type] || [];
if(typeof callback !== 'function') {
throw new TypeError('`#addEvent`\'s second argument must be a function');
}
if(events.indexOf(callback) === -1) {
(this._latched[type]) ? callback.apply(this,this._arguments[type]) : events.push(callback);
}
return this;
}
,addEvents: function(/* Object */ events){
for(var key in events){
if(events.hasOwnProperty(key)){
this.addEvent(key,events[key]);
}
}
return this;
}
,fireEvent: function(/* String */ type) {
var type = removeLatched.call(this,type)
,isLatched = this._latched[type]
,events = this._events[type]
,length = events ? events.length : 0
,args = Array.prototype.slice.call(arguments,1)
,i = 0;
this._switched[type] = 1;
if(events && length) {
for (; i < length; i++) {
if (i in events) {
try{
events[i].apply(this,args);
} catch (e) {
throw new Error('Event Error - '+type+':: '+ e);
}
}
}
}
if(isLatched){
this._arguments[type] = args;
delete events;
}
return this;
}
,hasFired: function (key) {
return this._switched[key]?true:false;
}
};
return EventMethods;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment