SocketIOSession.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { pluck, distinctUntilChanged } from "rxjs/operators"
  2. import { Socket as SocketV2 } from "socket.io-client-v2"
  3. import { Socket as SocketV3 } from "socket.io-client-v3"
  4. import { Socket as SocketV4 } from "socket.io-client-v4"
  5. import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
  6. import {
  7. HoppRealtimeLog,
  8. HoppRealtimeLogLine,
  9. } from "~/helpers/types/HoppRealtimeLog"
  10. type SocketIO = SocketV2 | SocketV3 | SocketV4
  11. export type SIOClientVersion = "v4" | "v3" | "v2"
  12. type HoppSIORequest = {
  13. endpoint: string
  14. path: string
  15. version: SIOClientVersion
  16. }
  17. type HoppSIOSession = {
  18. request: HoppSIORequest
  19. log: HoppRealtimeLog
  20. socket: SocketIO | null
  21. }
  22. const defaultSIORequest: HoppSIORequest = {
  23. endpoint: "wss://echo-socketio.hoppscotch.io",
  24. path: "/socket.io",
  25. version: "v4",
  26. }
  27. const defaultSIOSession: HoppSIOSession = {
  28. request: defaultSIORequest,
  29. socket: null,
  30. log: [],
  31. }
  32. const dispatchers = defineDispatchers({
  33. setRequest(
  34. _: HoppSIOSession,
  35. { newRequest }: { newRequest: HoppSIORequest }
  36. ) {
  37. return {
  38. request: newRequest,
  39. }
  40. },
  41. setEndpoint(curr: HoppSIOSession, { newEndpoint }: { newEndpoint: string }) {
  42. return {
  43. request: {
  44. ...curr.request,
  45. endpoint: newEndpoint,
  46. },
  47. }
  48. },
  49. setPath(curr: HoppSIOSession, { newPath }: { newPath: string }) {
  50. return {
  51. request: {
  52. ...curr.request,
  53. path: newPath,
  54. },
  55. }
  56. },
  57. setVersion(
  58. curr: HoppSIOSession,
  59. { newVersion }: { newVersion: SIOClientVersion }
  60. ) {
  61. return {
  62. request: {
  63. ...curr.request,
  64. version: newVersion,
  65. },
  66. }
  67. },
  68. setSocket(_: HoppSIOSession, { socket }: { socket: SocketIO }) {
  69. return {
  70. socket,
  71. }
  72. },
  73. setLog(_: HoppSIOSession, { log }: { log: HoppRealtimeLog }) {
  74. return {
  75. log,
  76. }
  77. },
  78. addLogLine(curr: HoppSIOSession, { line }: { line: HoppRealtimeLogLine }) {
  79. return {
  80. log: [...curr.log, line],
  81. }
  82. },
  83. })
  84. const SIOSessionStore = new DispatchingStore(defaultSIOSession, dispatchers)
  85. export function setSIORequest(newRequest?: HoppSIORequest) {
  86. SIOSessionStore.dispatch({
  87. dispatcher: "setRequest",
  88. payload: {
  89. newRequest: newRequest ?? defaultSIORequest,
  90. },
  91. })
  92. }
  93. export function setSIOEndpoint(newEndpoint: string) {
  94. SIOSessionStore.dispatch({
  95. dispatcher: "setEndpoint",
  96. payload: {
  97. newEndpoint,
  98. },
  99. })
  100. }
  101. export function setSIOVersion(newVersion: string) {
  102. SIOSessionStore.dispatch({
  103. dispatcher: "setVersion",
  104. payload: {
  105. newVersion,
  106. },
  107. })
  108. }
  109. export function setSIOPath(newPath: string) {
  110. SIOSessionStore.dispatch({
  111. dispatcher: "setPath",
  112. payload: {
  113. newPath,
  114. },
  115. })
  116. }
  117. export function setSIOSocket(socket: SocketIO) {
  118. SIOSessionStore.dispatch({
  119. dispatcher: "setSocket",
  120. payload: {
  121. socket,
  122. },
  123. })
  124. }
  125. export function setSIOLog(log: HoppRealtimeLog) {
  126. SIOSessionStore.dispatch({
  127. dispatcher: "setLog",
  128. payload: {
  129. log,
  130. },
  131. })
  132. }
  133. export function addSIOLogLine(line: HoppRealtimeLogLine) {
  134. SIOSessionStore.dispatch({
  135. dispatcher: "addLogLine",
  136. payload: {
  137. line,
  138. },
  139. })
  140. }
  141. export const SIORequest$ = SIOSessionStore.subject$.pipe(
  142. pluck("request"),
  143. distinctUntilChanged()
  144. )
  145. export const SIOEndpoint$ = SIOSessionStore.subject$.pipe(
  146. pluck("request", "endpoint"),
  147. distinctUntilChanged()
  148. )
  149. export const SIOVersion$ = SIOSessionStore.subject$.pipe(
  150. pluck("request", "version"),
  151. distinctUntilChanged()
  152. )
  153. export const SIOPath$ = SIOSessionStore.subject$.pipe(
  154. pluck("request", "path"),
  155. distinctUntilChanged()
  156. )
  157. export const SIOConnectionState$ = SIOSessionStore.subject$.pipe(
  158. pluck("connectionState"),
  159. distinctUntilChanged()
  160. )
  161. export const SIOSocket$ = SIOSessionStore.subject$.pipe(
  162. pluck("socket"),
  163. distinctUntilChanged()
  164. )
  165. export const SIOLog$ = SIOSessionStore.subject$.pipe(
  166. pluck("log"),
  167. distinctUntilChanged()
  168. )