event-pubsub-browser-es5.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. window.EventPubSub=function EventPubSub() {
  3. this._events_={};
  4. this.publish=this.trigger=this.emit=emit;
  5. this.subscribe=this.on=on;
  6. this.once=once;
  7. this.unSubscribe=this.off=off;
  8. this.emit$=emit$;
  9. function on(type,handler,once){
  10. if(!handler){
  11. throw new ReferenceError('handler not defined.');
  12. }
  13. if(!this._events_[type]){
  14. this._events_[type]=[];
  15. }
  16. if(once){
  17. handler._once_ = once;
  18. }
  19. this._events_[type].push(handler);
  20. return this;
  21. }
  22. function once(type,handler){
  23. return this.on(type, handler, true);
  24. }
  25. function off(type,handler){
  26. if(!this._events_[type]){
  27. return this;
  28. }
  29. if(!handler){
  30. throw new ReferenceError('handler not defined. if you wish to remove all handlers from the event please pass "*" as the handler');
  31. }
  32. if(handler=='*'){
  33. delete this._events_[type];
  34. return this;
  35. }
  36. var handlers=this._events_[type];
  37. while(handlers.includes(handler)){
  38. handlers.splice(
  39. handlers.indexOf(handler),
  40. 1
  41. );
  42. }
  43. if(handlers.length<1){
  44. delete this._events_[type];
  45. }
  46. return this;
  47. }
  48. function emit(type){
  49. this.emit$.apply(this, arguments);
  50. if(!this._events_[type]){
  51. return this;
  52. }
  53. arguments.splice=Array.prototype.splice;
  54. arguments.splice(0,1);
  55. var handlers=this._events_[type];
  56. var onceHandled=[];
  57. for(var i in handlers){
  58. var handler=handlers[i];
  59. handler.apply(this, arguments);
  60. if(handler._once_){
  61. onceHandled.push(handler);
  62. }
  63. }
  64. for(var i in onceHandled){
  65. this.off(
  66. type,
  67. onceHandled[i]
  68. );
  69. }
  70. return this;
  71. }
  72. function emit$(type, args){
  73. if(!this._events_['*']){
  74. return this;
  75. }
  76. var catchAll=this._events_['*'];
  77. args.shift=Array.prototype.shift;
  78. args.shift(type);
  79. for(var handler of catchAll){
  80. handler.apply(this, args);
  81. }
  82. return this;
  83. }
  84. return this;
  85. }
  86. if (!Array.prototype.includes) {
  87. Array.prototype.includes = function(searchElement /*, fromIndex*/) {
  88. 'use strict';
  89. if (this == null) {
  90. throw new TypeError('Array.prototype.includes called on null or undefined');
  91. }
  92. var O = Object(this);
  93. var len = parseInt(O.length, 10) || 0;
  94. if (len === 0) {
  95. return false;
  96. }
  97. var n = parseInt(arguments[1], 10) || 0;
  98. var k;
  99. if (n >= 0) {
  100. k = n;
  101. } else {
  102. k = len + n;
  103. if (k < 0) {k = 0;}
  104. }
  105. var currentElement;
  106. while (k < len) {
  107. currentElement = O[k];
  108. if (searchElement === currentElement ||
  109. (searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
  110. return true;
  111. }
  112. k++;
  113. }
  114. return false;
  115. };
  116. }