events-emitter.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* eslint-disable no-underscore-dangle */
  2. export default {
  3. on(events, handler, priority) {
  4. const self = this;
  5. if (typeof handler !== 'function') return self;
  6. const method = priority ? 'unshift' : 'push';
  7. events.split(' ').forEach(event => {
  8. if (!self.eventsListeners[event]) self.eventsListeners[event] = [];
  9. self.eventsListeners[event][method](handler);
  10. });
  11. return self;
  12. },
  13. once(events, handler, priority) {
  14. const self = this;
  15. if (typeof handler !== 'function') return self;
  16. function onceHandler(...args) {
  17. self.off(events, onceHandler);
  18. if (onceHandler.__emitterProxy) {
  19. delete onceHandler.__emitterProxy;
  20. }
  21. handler.apply(self, args);
  22. }
  23. onceHandler.__emitterProxy = handler;
  24. return self.on(events, onceHandler, priority);
  25. },
  26. onAny(handler, priority) {
  27. const self = this;
  28. if (typeof handler !== 'function') return self;
  29. const method = priority ? 'unshift' : 'push';
  30. if (self.eventsAnyListeners.indexOf(handler) < 0) {
  31. self.eventsAnyListeners[method](handler);
  32. }
  33. return self;
  34. },
  35. offAny(handler) {
  36. const self = this;
  37. if (!self.eventsAnyListeners) return self;
  38. const index = self.eventsAnyListeners.indexOf(handler);
  39. if (index >= 0) {
  40. self.eventsAnyListeners.splice(index, 1);
  41. }
  42. return self;
  43. },
  44. off(events, handler) {
  45. const self = this;
  46. if (!self.eventsListeners) return self;
  47. events.split(' ').forEach(event => {
  48. // self.native.off(event, handler);
  49. if (typeof handler === 'undefined') {
  50. self.eventsListeners[event] = [];
  51. } else if (self.eventsListeners[event]) {
  52. self.eventsListeners[event].forEach((eventHandler, index) => {
  53. if (eventHandler === handler || eventHandler.__emitterProxy && eventHandler
  54. .__emitterProxy === handler) {
  55. self.eventsListeners[event].splice(index, 1);
  56. }
  57. });
  58. }
  59. });
  60. return self;
  61. },
  62. emit(...args) {
  63. const self = this;
  64. if (!self.eventsListeners) return self;
  65. let events;
  66. let data;
  67. let context;
  68. if (typeof args[0] === 'string' || Array.isArray(args[0])) {
  69. events = args[0];
  70. data = args.slice(1, args.length);
  71. context = self;
  72. } else {
  73. events = args[0].events;
  74. data = args[0].data;
  75. context = args[0].context || self;
  76. }
  77. data.unshift(context);
  78. const eventsArray = Array.isArray(events) ? events : events.split(' ');
  79. eventsArray.forEach(event => {
  80. // console.log(event)
  81. if (self.eventsAnyListeners && self.eventsAnyListeners.length) {
  82. self.eventsAnyListeners.forEach(eventHandler => {
  83. eventHandler.apply(context, [event, ...data]);
  84. });
  85. }
  86. if (self.eventsListeners && self.eventsListeners[event]) {
  87. self.eventsListeners[event].forEach(eventHandler => {
  88. eventHandler.apply(context, data);
  89. });
  90. }
  91. });
  92. return self;
  93. }
  94. };