WebSocketSession.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { pluck, distinctUntilChanged } from "rxjs/operators"
  2. import DispatchingStore, { defineDispatchers } from "./DispatchingStore"
  3. import {
  4. HoppRealtimeLog,
  5. HoppRealtimeLogLine,
  6. } from "~/helpers/types/HoppRealtimeLog"
  7. import { WSConnection } from "~/helpers/realtime/WSConnection"
  8. export type HoppWSProtocol = {
  9. value: string
  10. active: boolean
  11. }
  12. type HoppWSRequest = {
  13. endpoint: string
  14. protocols: HoppWSProtocol[]
  15. }
  16. export type HoppWSSession = {
  17. request: HoppWSRequest
  18. log: HoppRealtimeLog
  19. socket: WSConnection
  20. }
  21. const defaultWSRequest: HoppWSRequest = {
  22. endpoint: "wss://echo-websocket.hoppscotch.io",
  23. protocols: [],
  24. }
  25. const defaultWSSession: HoppWSSession = {
  26. request: defaultWSRequest,
  27. socket: new WSConnection(),
  28. log: [],
  29. }
  30. const dispatchers = defineDispatchers({
  31. setRequest(_: HoppWSSession, { newRequest }: { newRequest: HoppWSRequest }) {
  32. return {
  33. request: newRequest,
  34. }
  35. },
  36. setEndpoint(curr: HoppWSSession, { newEndpoint }: { newEndpoint: string }) {
  37. return {
  38. request: {
  39. protocols: curr.request.protocols,
  40. endpoint: newEndpoint,
  41. },
  42. }
  43. },
  44. setProtocols(
  45. curr: HoppWSSession,
  46. { protocols }: { protocols: HoppWSProtocol[] }
  47. ) {
  48. return {
  49. request: {
  50. protocols,
  51. endpoint: curr.request.endpoint,
  52. },
  53. }
  54. },
  55. addProtocol(curr: HoppWSSession, { protocol }: { protocol: HoppWSProtocol }) {
  56. return {
  57. request: {
  58. endpoint: curr.request.endpoint,
  59. protocols: [...curr.request.protocols, protocol],
  60. },
  61. }
  62. },
  63. deleteProtocol(curr: HoppWSSession, { index }: { index: number }) {
  64. return {
  65. request: {
  66. endpoint: curr.request.endpoint,
  67. protocols: curr.request.protocols.filter((_, idx) => index !== idx),
  68. },
  69. }
  70. },
  71. deleteAllProtocols(curr: HoppWSSession) {
  72. return {
  73. request: {
  74. endpoint: curr.request.endpoint,
  75. protocols: [],
  76. },
  77. }
  78. },
  79. updateProtocol(
  80. curr: HoppWSSession,
  81. {
  82. index,
  83. updatedProtocol,
  84. }: { index: number; updatedProtocol: HoppWSProtocol }
  85. ) {
  86. return {
  87. request: {
  88. endpoint: curr.request.endpoint,
  89. protocols: curr.request.protocols.map((proto, idx) => {
  90. return index === idx ? updatedProtocol : proto
  91. }),
  92. },
  93. }
  94. },
  95. setSocket(_: HoppWSSession, { socket }: { socket: WSConnection }) {
  96. return {
  97. socket,
  98. }
  99. },
  100. setLog(_: HoppWSSession, { log }: { log: HoppRealtimeLog }) {
  101. return {
  102. log,
  103. }
  104. },
  105. addLogLine(curr: HoppWSSession, { line }: { line: HoppRealtimeLogLine }) {
  106. return {
  107. log: [...curr.log, line],
  108. }
  109. },
  110. })
  111. const WSSessionStore = new DispatchingStore(defaultWSSession, dispatchers)
  112. export function setWSRequest(newRequest?: HoppWSRequest) {
  113. WSSessionStore.dispatch({
  114. dispatcher: "setRequest",
  115. payload: {
  116. newRequest: newRequest ?? defaultWSRequest,
  117. },
  118. })
  119. }
  120. export function setWSEndpoint(newEndpoint: string) {
  121. WSSessionStore.dispatch({
  122. dispatcher: "setEndpoint",
  123. payload: {
  124. newEndpoint,
  125. },
  126. })
  127. }
  128. export function setWSProtocols(protocols: HoppWSProtocol[]) {
  129. WSSessionStore.dispatch({
  130. dispatcher: "setProtocols",
  131. payload: {
  132. protocols,
  133. },
  134. })
  135. }
  136. export function addWSProtocol(protocol: HoppWSProtocol) {
  137. WSSessionStore.dispatch({
  138. dispatcher: "addProtocol",
  139. payload: {
  140. protocol,
  141. },
  142. })
  143. }
  144. export function deleteWSProtocol(index: number) {
  145. WSSessionStore.dispatch({
  146. dispatcher: "deleteProtocol",
  147. payload: {
  148. index,
  149. },
  150. })
  151. }
  152. export function deleteAllWSProtocols() {
  153. WSSessionStore.dispatch({
  154. dispatcher: "deleteAllProtocols",
  155. payload: {},
  156. })
  157. }
  158. export function updateWSProtocol(
  159. index: number,
  160. updatedProtocol: HoppWSProtocol
  161. ) {
  162. WSSessionStore.dispatch({
  163. dispatcher: "updateProtocol",
  164. payload: {
  165. index,
  166. updatedProtocol,
  167. },
  168. })
  169. }
  170. export function setWSSocket(socket: WSConnection) {
  171. WSSessionStore.dispatch({
  172. dispatcher: "setSocket",
  173. payload: {
  174. socket,
  175. },
  176. })
  177. }
  178. export function setWSLog(log: HoppRealtimeLog) {
  179. WSSessionStore.dispatch({
  180. dispatcher: "setLog",
  181. payload: {
  182. log,
  183. },
  184. })
  185. }
  186. export function addWSLogLine(line: HoppRealtimeLogLine) {
  187. WSSessionStore.dispatch({
  188. dispatcher: "addLogLine",
  189. payload: {
  190. line,
  191. },
  192. })
  193. }
  194. export const WSRequest$ = WSSessionStore.subject$.pipe(
  195. pluck("request"),
  196. distinctUntilChanged()
  197. )
  198. export const WSEndpoint$ = WSSessionStore.subject$.pipe(
  199. pluck("request", "endpoint"),
  200. distinctUntilChanged()
  201. )
  202. export const WSProtocols$ = WSSessionStore.subject$.pipe(
  203. pluck("request", "protocols"),
  204. distinctUntilChanged()
  205. )
  206. export const WSConnectingState$ = WSSessionStore.subject$.pipe(
  207. pluck("connectingState"),
  208. distinctUntilChanged()
  209. )
  210. export const WSConnectionState$ = WSSessionStore.subject$.pipe(
  211. pluck("connectionState"),
  212. distinctUntilChanged()
  213. )
  214. export const WSSocket$ = WSSessionStore.subject$.pipe(
  215. pluck("socket"),
  216. distinctUntilChanged()
  217. )
  218. export const WSLog$ = WSSessionStore.subject$.pipe(
  219. pluck("log"),
  220. distinctUntilChanged()
  221. )