WebSocketSession.ts 5.7 KB

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