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.
 
 
 

74 lines
1.3 KiB

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