Sha256: 1ffdfd419cecff8250fc5ceb2689beee85533ebb8d8dcdb2924457817d951bec

Contents?: true

Size: 1.77 KB

Versions: 53

Compression:

Stored size: 1.77 KB

Contents

// event_emitter.js v0.0.8
// https://github.com/shokai/event_emitter.js
// (c) 2013 Sho Hashimoto <hashimoto@shokai.org>
// The MIT License
var EventEmitter = function(){
  var self = this;
  this.apply = function(target, prefix){
    if(!prefix) prefix = "";
    for(var func in self){
      if(self.hasOwnProperty(func) && func !== "apply"){
        target[prefix+func] = this[func];
      }
    }
  };
  this.__events = new Array();
  this.on = function(type, listener, opts){
    if(typeof listener !== "function") return;
    var event_id = self.__events.length > 0 ? 1 + self.__events[self.__events.length-1].id : 0
    var params = {
      id: event_id,
      type: type,
      listener: listener
    };
    for(i in opts){
      if(!params[i]) params[i] = opts[i];
    };
    self.__events.push(params);
    return event_id;
  };

  this.once = function(type, listener){
    self.on(type, listener, {once: true});
  };

  this.emit = function(type, data){
    for(var i = 0; i < self.__events.length; i++){
      var e = self.__events[i];
      switch(e.type){
      case type:
        e.listener(data);
        if(e.once) e.type = null;
        break
      case "*":
        e.listener(type, data);
        if(e.once) e.type = null;
        break
      }
    }
    self.removeListener();
  };

  this.removeListener = function(id_or_type){
    for(var i = self.__events.length-1; i >= 0; i--){
      var e = self.__events[i];
      switch(typeof id_or_type){
      case "number":
        if(e.id === id_or_type) self.__events.splice(i,1);
        break
      case "string":
      case "object":
        if(e.type === id_or_type) self.__events.splice(i,1);
        break
      }
    }
  };

};

if(typeof module !== "undefined" && typeof module.exports !== "undefined"){
  module.exports = EventEmitter;
}

Version data entries

53 entries across 53 versions & 3 rubygems

Version Path
sinatra-cometio-0.6.0 lib/js/event_emitter.js
sinatra-websocketio-0.4.1 lib/js/event_emitter.js
sinatra-rocketio-0.3.3 lib/js/event_emitter.js
sinatra-rocketio-0.3.2 lib/js/event_emitter.js
sinatra-websocketio-0.4.0 lib/js/event_emitter.js
sinatra-websocketio-0.3.9 lib/js/event_emitter.js
sinatra-rocketio-0.3.1 lib/js/event_emitter.js
sinatra-rocketio-0.3.0 lib/js/event_emitter.js
sinatra-websocketio-0.3.7 lib/js/event_emitter.js
sinatra-websocketio-0.3.6 lib/js/event_emitter.js
sinatra-websocketio-0.3.5 lib/js/event_emitter.js
sinatra-cometio-0.5.9 lib/js/event_emitter.js
sinatra-websocketio-0.3.4 lib/js/event_emitter.js
sinatra-websocketio-0.3.3 lib/js/event_emitter.js
sinatra-websocketio-0.3.2 lib/js/event_emitter.js
sinatra-cometio-0.5.8 lib/js/event_emitter.js
sinatra-rocketio-0.2.6 lib/js/event_emitter.js
sinatra-websocketio-0.3.1 lib/js/event_emitter.js
sinatra-cometio-0.5.7 lib/js/event_emitter.js
sinatra-rocketio-0.2.5 lib/js/event_emitter.js