SocketIOSession.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. type HoppSIORequest = {
  12. endpoint: string
  13. path: string
  14. version: string
  15. }
  16. type HoppSIOSession = {
  17. request: HoppSIORequest
  18. connectingState: boolean
  19. connectionState: boolean
  20. log: HoppRealtimeLog
  21. socket: SocketIO | null
  22. }
  23. const defaultSIORequest: HoppSIORequest = {
  24. endpoint: "wss://hoppscotch-socketio.herokuapp.com",
  25. path: "/socket.io",
  26. version: "v4",
  27. }
  28. const defaultSIOSession: HoppSIOSession = {
  29. request: defaultSIORequest,
  30. connectionState: false,
  31. connectingState: false,
  32. socket: null,
  33. log: [],
  34. }
  35. const dispatchers = defineDispatchers({
  36. setRequest(
  37. _: HoppSIOSession,
  38. { newRequest }: { newRequest: HoppSIORequest }
  39. ) {
  40. return {
  41. request: newRequest,
  42. }
  43. },
  44. setEndpoint(curr: HoppSIOSession, { newEndpoint }: { newEndpoint: string }) {
  45. return {
  46. request: {
  47. ...curr.request,
  48. endpoint: newEndpoint,
  49. },
  50. }
  51. },
  52. setPath(curr: HoppSIOSession, { newPath }: { newPath: string }) {
  53. return {
  54. request: {
  55. ...curr.request,
  56. path: newPath,
  57. },
  58. }
  59. },
  60. setVersion(curr: HoppSIOSession, { newVersion }: { newVersion: string }) {
  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. setConnectionState(_: HoppSIOSession, { state }: { state: boolean }) {
  74. return {
  75. connectionState: state,
  76. }
  77. },
  78. setConnectingState(_: HoppSIOSession, { state }: { state: boolean }) {
  79. return {
  80. connectingState: state,
  81. }
  82. },
  83. setLog(_: HoppSIOSession, { log }: { log: HoppRealtimeLog }) {
  84. return {
  85. log,
  86. }
  87. },
  88. addLogLine(curr: HoppSIOSession, { line }: { line: HoppRealtimeLogLine }) {
  89. return {
  90. log: [...curr.log, line],
  91. }
  92. },
  93. })
  94. const SIOSessionStore = new DispatchingStore(defaultSIOSession, dispatchers)
  95. export function setSIORequest(newRequest?: HoppSIORequest) {
  96. SIOSessionStore.dispatch({
  97. dispatcher: "setRequest",
  98. payload: {
  99. newRequest: newRequest ?? defaultSIORequest,
  100. },
  101. })
  102. }
  103. export function setSIOEndpoint(newEndpoint: string) {
  104. SIOSessionStore.dispatch({
  105. dispatcher: "setEndpoint",
  106. payload: {
  107. newEndpoint,
  108. },
  109. })
  110. }
  111. export function setSIOVersion(newVersion: string) {
  112. SIOSessionStore.dispatch({
  113. dispatcher: "setVersion",
  114. payload: {
  115. newVersion,
  116. },
  117. })
  118. }
  119. export function setSIOPath(newPath: string) {
  120. SIOSessionStore.dispatch({
  121. dispatcher: "setPath",
  122. payload: {
  123. newPath,
  124. },
  125. })
  126. }
  127. export function setSIOSocket(socket: SocketIO) {
  128. SIOSessionStore.dispatch({
  129. dispatcher: "setSocket",
  130. payload: {
  131. socket,
  132. },
  133. })
  134. }
  135. export function setSIOConnectionState(state: boolean) {
  136. SIOSessionStore.dispatch({
  137. dispatcher: "setConnectionState",
  138. payload: {
  139. state,
  140. },
  141. })
  142. }
  143. export function setSIOConnectingState(state: boolean) {
  144. SIOSessionStore.dispatch({
  145. dispatcher: "setConnectingState",
  146. payload: {
  147. state,
  148. },
  149. })
  150. }
  151. export function setSIOLog(log: HoppRealtimeLog) {
  152. SIOSessionStore.dispatch({
  153. dispatcher: "setLog",
  154. payload: {
  155. log,
  156. },
  157. })
  158. }
  159. export function addSIOLogLine(line: HoppRealtimeLogLine) {
  160. SIOSessionStore.dispatch({
  161. dispatcher: "addLogLine",
  162. payload: {
  163. line,
  164. },
  165. })
  166. }
  167. export const SIORequest$ = SIOSessionStore.subject$.pipe(
  168. pluck("request"),
  169. distinctUntilChanged()
  170. )
  171. export const SIOEndpoint$ = SIOSessionStore.subject$.pipe(
  172. pluck("request", "endpoint"),
  173. distinctUntilChanged()
  174. )
  175. export const SIOVersion$ = SIOSessionStore.subject$.pipe(
  176. pluck("request", "version"),
  177. distinctUntilChanged()
  178. )
  179. export const SIOPath$ = SIOSessionStore.subject$.pipe(
  180. pluck("request", "path"),
  181. distinctUntilChanged()
  182. )
  183. export const SIOConnectingState$ = SIOSessionStore.subject$.pipe(
  184. pluck("connectingState"),
  185. distinctUntilChanged()
  186. )
  187. export const SIOConnectionState$ = SIOSessionStore.subject$.pipe(
  188. pluck("connectionState"),
  189. distinctUntilChanged()
  190. )
  191. export const SIOSocket$ = SIOSessionStore.subject$.pipe(
  192. pluck("socket"),
  193. distinctUntilChanged()
  194. )
  195. export const SIOLog$ = SIOSessionStore.subject$.pipe(
  196. pluck("log"),
  197. distinctUntilChanged()
  198. )