You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

67 lines
1.1 KiB

  1. define([
  2. ], function (
  3. ) {
  4. let events = {
  5. events: {},
  6. queue: [],
  7. on: function (eventName, callback) {
  8. let list = this.events[eventName] || (this.events[eventName] = []);
  9. list.push(callback);
  10. for (let i = 0; i < this.queue.length; i++) {
  11. let q = this.queue[i];
  12. if (q.event !== eventName)
  13. continue;
  14. this.queue.splice(i, 1);
  15. i--;
  16. q.args.splice(0, 0, eventName);
  17. this.emit.apply(this, q.args);
  18. }
  19. return callback;
  20. },
  21. clearQueue: function () {
  22. this.queue.length = 0;
  23. },
  24. off: function (eventName, callback) {
  25. let list = this.events[eventName] || [];
  26. let lLen = list.length;
  27. for (let i = 0; i < lLen; i++) {
  28. if (list[i] === callback) {
  29. list.splice(i, 1);
  30. i--;
  31. lLen--;
  32. }
  33. }
  34. if (lLen === 0)
  35. delete this.events[eventName];
  36. },
  37. emit: function (eventName) {
  38. let args = [].slice.call(arguments, 1);
  39. let list = this.events[eventName];
  40. if (!list) {
  41. this.queue.push({
  42. event: eventName,
  43. args: args
  44. });
  45. return;
  46. }
  47. list.forEach(l => l.apply(null, args));
  48. }
  49. };
  50. if (window.addons)
  51. window.addons.init(events);
  52. return events;
  53. });