tinyemitter.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. E.prototype = {
  7. on: function (name, callback, ctx) {
  8. var e = this.e || (this.e = {});
  9. (e[name] || (e[name] = [])).push({
  10. fn: callback,
  11. ctx: ctx
  12. });
  13. return this;
  14. },
  15. once: function (name, callback, ctx) {
  16. var self = this;
  17. function listener () {
  18. self.off(name, listener);
  19. callback.apply(ctx, arguments);
  20. };
  21. listener._ = callback
  22. return this.on(name, listener, ctx);
  23. },
  24. emit: function (name) {
  25. var data = [].slice.call(arguments, 1);
  26. var evtArr = ((this.e || (this.e = {}))[name] || []).slice();
  27. var i = 0;
  28. var len = evtArr.length;
  29. for (i; i < len; i++) {
  30. evtArr[i].fn.apply(evtArr[i].ctx, data);
  31. }
  32. return this;
  33. },
  34. off: function (name, callback) {
  35. var e = this.e || (this.e = {});
  36. var evts = e[name];
  37. var liveEvents = [];
  38. if (evts && callback) {
  39. for (var i = 0, len = evts.length; i < len; i++) {
  40. if (evts[i].fn !== callback && evts[i].fn._ !== callback)
  41. liveEvents.push(evts[i]);
  42. }
  43. }
  44. // Remove event from queue to prevent memory leak
  45. // Suggested by https://github.com/lazd
  46. // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910
  47. (liveEvents.length)
  48. ? e[name] = liveEvents
  49. : delete e[name];
  50. return this;
  51. }
  52. };
  53. module.exports = E;
  54. module.exports.TinyEmitter = E;
  55. },{}]},{},[1])(1)
  56. });