JSFiddle - React, Tailwind, and code Playground

by Chase Wilson

JavaScript

(function(global,ns){
  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 = {
     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);
      events = events.map(removeLatched.bind(this));
      var self = this;
      var fireCheck =  function () {
        var length = events.length;
        console.log(events);
        while(length--){
          if(!self._switched[events[length]]) return;
        }
        self.fireEvent(type+':latched');
      };
        
      events.forEach(function (event) {
        event = removeLatched.call(self, event);
        self.addEvent(event, fireCheck);
      });

      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(Events.errors.callback_must_be_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]
      ...