tinyemitter.js (2471B)
1 (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.TinyEmitter = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ 2 function E () { 3 // Keep this empty so it's easier to inherit from 4 // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) 5 } 6 7 E.prototype = { 8 on: function (name, callback, ctx) { 9 var e = this.e || (this.e = {}); 10 11 (e[name] || (e[name] = [])).push({ 12 fn: callback, 13 ctx: ctx 14 }); 15 16 return this; 17 }, 18 19 once: function (name, callback, ctx) { 20 var self = this; 21 function listener () { 22 self.off(name, listener); 23 callback.apply(ctx, arguments); 24 }; 25 26 listener._ = callback 27 return this.on(name, listener, ctx); 28 }, 29 30 emit: function (name) { 31 var data = [].slice.call(arguments, 1); 32 var evtArr = ((this.e || (this.e = {}))[name] || []).slice(); 33 var i = 0; 34 var len = evtArr.length; 35 36 for (i; i < len; i++) { 37 evtArr[i].fn.apply(evtArr[i].ctx, data); 38 } 39 40 return this; 41 }, 42 43 off: function (name, callback) { 44 var e = this.e || (this.e = {}); 45 var evts = e[name]; 46 var liveEvents = []; 47 48 if (evts && callback) { 49 for (var i = 0, len = evts.length; i < len; i++) { 50 if (evts[i].fn !== callback && evts[i].fn._ !== callback) 51 liveEvents.push(evts[i]); 52 } 53 } 54 55 // Remove event from queue to prevent memory leak 56 // Suggested by https://github.com/lazd 57 // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 58 59 (liveEvents.length) 60 ? e[name] = liveEvents 61 : delete e[name]; 62 63 return this; 64 } 65 }; 66 67 module.exports = E; 68 module.exports.TinyEmitter = E; 69 70 },{}]},{},[1])(1) 71 });