"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/event-kit/lib/emitter.js" (7 Feb 2017, 3704 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 (function() {
    2   var Disposable, Emitter;
    3 
    4   Disposable = require('./disposable');
    5 
    6   module.exports = Emitter = (function() {
    7     Emitter.exceptionHandlers = [];
    8 
    9     Emitter.onEventHandlerException = function(exceptionHandler) {
   10       if (this.exceptionHandlers.length === 0) {
   11         this.dispatch = this.exceptionHandlingDispatch;
   12       }
   13       this.exceptionHandlers.push(exceptionHandler);
   14       return new Disposable((function(_this) {
   15         return function() {
   16           _this.exceptionHandlers.splice(_this.exceptionHandlers.indexOf(exceptionHandler), 1);
   17           if (_this.exceptionHandlers.length === 0) {
   18             return _this.dispatch = _this.simpleDispatch;
   19           }
   20         };
   21       })(this));
   22     };
   23 
   24     Emitter.simpleDispatch = function(handler, value) {
   25       return handler(value);
   26     };
   27 
   28     Emitter.exceptionHandlingDispatch = function(handler, value) {
   29       var exception, exceptionHandler, _i, _len, _ref, _results;
   30       try {
   31         return handler(value);
   32       } catch (_error) {
   33         exception = _error;
   34         _ref = this.exceptionHandlers;
   35         _results = [];
   36         for (_i = 0, _len = _ref.length; _i < _len; _i++) {
   37           exceptionHandler = _ref[_i];
   38           _results.push(exceptionHandler(exception));
   39         }
   40         return _results;
   41       }
   42     };
   43 
   44     Emitter.dispatch = Emitter.simpleDispatch;
   45 
   46     Emitter.prototype.disposed = false;
   47 
   48 
   49     /*
   50     Section: Construction and Destruction
   51      */
   52 
   53     function Emitter() {
   54       this.clear();
   55     }
   56 
   57     Emitter.prototype.clear = function() {
   58       return this.handlersByEventName = {};
   59     };
   60 
   61     Emitter.prototype.dispose = function() {
   62       this.handlersByEventName = null;
   63       return this.disposed = true;
   64     };
   65 
   66 
   67     /*
   68     Section: Event Subscription
   69      */
   70 
   71     Emitter.prototype.on = function(eventName, handler, unshift) {
   72       var currentHandlers;
   73       if (unshift == null) {
   74         unshift = false;
   75       }
   76       if (this.disposed) {
   77         throw new Error("Emitter has been disposed");
   78       }
   79       if (typeof handler !== 'function') {
   80         throw new Error("Handler must be a function");
   81       }
   82       if (currentHandlers = this.handlersByEventName[eventName]) {
   83         if (unshift) {
   84           this.handlersByEventName[eventName] = [handler].concat(currentHandlers);
   85         } else {
   86           this.handlersByEventName[eventName] = currentHandlers.concat(handler);
   87         }
   88       } else {
   89         this.handlersByEventName[eventName] = [handler];
   90       }
   91       return new Disposable(this.off.bind(this, eventName, handler));
   92     };
   93 
   94     Emitter.prototype.preempt = function(eventName, handler) {
   95       return this.on(eventName, handler, true);
   96     };
   97 
   98     Emitter.prototype.off = function(eventName, handlerToRemove) {
   99       var handler, newHandlers, oldHandlers, _i, _len;
  100       if (this.disposed) {
  101         return;
  102       }
  103       if (oldHandlers = this.handlersByEventName[eventName]) {
  104         newHandlers = [];
  105         for (_i = 0, _len = oldHandlers.length; _i < _len; _i++) {
  106           handler = oldHandlers[_i];
  107           if (handler !== handlerToRemove) {
  108             newHandlers.push(handler);
  109           }
  110         }
  111         this.handlersByEventName[eventName] = newHandlers;
  112       }
  113     };
  114 
  115 
  116     /*
  117     Section: Event Emission
  118      */
  119 
  120     Emitter.prototype.emit = function(eventName, value) {
  121       var handler, handlers, _i, _len, _ref;
  122       if (handlers = (_ref = this.handlersByEventName) != null ? _ref[eventName] : void 0) {
  123         for (_i = 0, _len = handlers.length; _i < _len; _i++) {
  124           handler = handlers[_i];
  125           this.constructor.dispatch(handler, value);
  126         }
  127       }
  128     };
  129 
  130     return Emitter;
  131 
  132   })();
  133 
  134 }).call(this);