events.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS101: Remove unnecessary use of Array.from
  6. * DS102: Remove unnecessary code created because of implicit returns
  7. * DS104: Avoid inline assignments
  8. * DS207: Consider shorter variations of null checks
  9. * DS208: Avoid top-level this
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
  11. */
  12. this.Events = {
  13. on(event, callback) {
  14. if (event.indexOf(" ") >= 0) {
  15. for (var name of Array.from(event.split(" "))) {
  16. this.on(name, callback);
  17. }
  18. } else {
  19. let base;
  20. ((base =
  21. this._callbacks != null ? this._callbacks : (this._callbacks = {}))[
  22. event
  23. ] != null
  24. ? base[event]
  25. : (base[event] = [])
  26. ).push(callback);
  27. }
  28. return this;
  29. },
  30. off(event, callback) {
  31. let callbacks, index;
  32. if (event.indexOf(" ") >= 0) {
  33. for (var name of Array.from(event.split(" "))) {
  34. this.off(name, callback);
  35. }
  36. } else if (
  37. (callbacks =
  38. this._callbacks != null ? this._callbacks[event] : undefined) &&
  39. (index = callbacks.indexOf(callback)) >= 0
  40. ) {
  41. callbacks.splice(index, 1);
  42. if (!callbacks.length) {
  43. delete this._callbacks[event];
  44. }
  45. }
  46. return this;
  47. },
  48. trigger(event, ...args) {
  49. let callbacks;
  50. this.eventInProgress = { name: event, args };
  51. if (
  52. (callbacks = this._callbacks != null ? this._callbacks[event] : undefined)
  53. ) {
  54. for (var callback of Array.from(callbacks.slice(0))) {
  55. if (typeof callback === "function") {
  56. callback(...Array.from(args || []));
  57. }
  58. }
  59. }
  60. this.eventInProgress = null;
  61. if (event !== "all") {
  62. this.trigger("all", event, ...Array.from(args));
  63. }
  64. return this;
  65. },
  66. removeEvent(event) {
  67. if (this._callbacks != null) {
  68. for (var name of Array.from(event.split(" "))) {
  69. delete this._callbacks[name];
  70. }
  71. }
  72. return this;
  73. },
  74. };