CustomWebSocket.class.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import ListenerHandler from "@/classes/ListenerHandler.class";
  2. export default class CustomWebSocketMock {
  3. dispatcher: ListenerHandler;
  4. url: string;
  5. data: {
  6. dispatch?: {
  7. [key: string]: (...args: any[]) => any;
  8. };
  9. progress?: {
  10. [key: string]: (...args: any[]) => any;
  11. };
  12. on?: {
  13. [key: string]: any;
  14. };
  15. };
  16. onDisconnectCbs: {
  17. temp: any[];
  18. persist: any[];
  19. };
  20. executeDispatch: boolean;
  21. constructor(url) {
  22. this.dispatcher = new ListenerHandler();
  23. this.url = url;
  24. this.data = {
  25. dispatch: {},
  26. progress: {},
  27. on: {}
  28. };
  29. this.onDisconnectCbs = {
  30. temp: [],
  31. persist: []
  32. };
  33. this.executeDispatch = true;
  34. }
  35. on(target, cb, options?) {
  36. const onData = this.data.on && this.data.on[target];
  37. this.dispatcher.addEventListener(
  38. `on.${target}`,
  39. event => cb(event.detail() || onData),
  40. options
  41. );
  42. }
  43. dispatch(target, ...args) {
  44. const lastArg = args[args.length - 1];
  45. const _args = args.slice(0, -1);
  46. const dispatchData = () =>
  47. this.data.dispatch &&
  48. typeof this.data.dispatch[target] === "function"
  49. ? this.data.dispatch[target](..._args)
  50. : undefined;
  51. const progressData = () =>
  52. this.data.progress &&
  53. typeof this.data.progress[target] === "function"
  54. ? this.data.progress[target](..._args)
  55. : undefined;
  56. if (typeof lastArg === "function") {
  57. if (this.executeDispatch && dispatchData()) lastArg(dispatchData());
  58. else if (!this.executeDispatch)
  59. this.dispatcher.addEventListener(
  60. `dispatch.${target}`,
  61. event => lastArg(event.detail(..._args) || dispatchData()),
  62. false
  63. );
  64. } else if (typeof lastArg === "object") {
  65. if (this.executeDispatch) {
  66. if (progressData())
  67. progressData().forEach(data => {
  68. lastArg.onProgress(data);
  69. });
  70. if (dispatchData()) lastArg.cb(dispatchData());
  71. } else {
  72. this.dispatcher.addEventListener(
  73. `progress.${target}`,
  74. event => {
  75. if (event.detail(..._args))
  76. lastArg.onProgress(event.detail(..._args));
  77. else if (progressData())
  78. progressData().forEach(data => {
  79. lastArg.onProgress(data);
  80. });
  81. },
  82. false
  83. );
  84. this.dispatcher.addEventListener(
  85. `dispatch.${target}`,
  86. event =>
  87. lastArg.cb(event.detail(..._args) || dispatchData()),
  88. false
  89. );
  90. }
  91. }
  92. }
  93. // eslint-disable-next-line class-methods-use-this
  94. onConnect(cb) {
  95. cb();
  96. }
  97. onDisconnect(...args) {
  98. if (args[0] === true) this.onDisconnectCbs.persist.push(args[1]);
  99. else this.onDisconnectCbs.temp.push(args[0]);
  100. this.dispatcher.addEventListener(
  101. "socket.disconnect",
  102. () => {
  103. this.onDisconnectCbs.temp.forEach(cb => cb());
  104. this.onDisconnectCbs.persist.forEach(cb => cb());
  105. },
  106. false
  107. );
  108. }
  109. clearCallbacks() {
  110. this.onDisconnectCbs.temp = [];
  111. }
  112. // eslint-disable-next-line class-methods-use-this
  113. destroyModalListeners() {}
  114. trigger(type, target, data?) {
  115. this.dispatcher.dispatchEvent(
  116. new CustomEvent(`${type}.${target}`, {
  117. detail: (...args) => {
  118. if (typeof data === "function") return data(...args);
  119. if (typeof data === "undefined") return undefined;
  120. return JSON.parse(JSON.stringify(data));
  121. }
  122. })
  123. );
  124. }
  125. }