SSESession.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 HoppSSERequest = {
  8. endpoint: string
  9. eventType: string
  10. }
  11. type HoppSSESession = {
  12. request: HoppSSERequest
  13. connectingState: boolean
  14. connectionState: boolean
  15. log: HoppRealtimeLog
  16. socket: EventSource | null
  17. }
  18. const defaultSSERequest: HoppSSERequest = {
  19. endpoint: "https://express-eventsource.herokuapp.com/events",
  20. eventType: "data",
  21. }
  22. const defaultSSESession: HoppSSESession = {
  23. request: defaultSSERequest,
  24. connectionState: false,
  25. connectingState: false,
  26. socket: null,
  27. log: [],
  28. }
  29. const dispatchers = defineDispatchers({
  30. setRequest(
  31. _: HoppSSESession,
  32. { newRequest }: { newRequest: HoppSSERequest }
  33. ) {
  34. return {
  35. request: newRequest,
  36. }
  37. },
  38. setEndpoint(curr: HoppSSESession, { newEndpoint }: { newEndpoint: string }) {
  39. return {
  40. request: {
  41. eventType: curr.request.eventType,
  42. endpoint: newEndpoint,
  43. },
  44. }
  45. },
  46. setEventType(curr: HoppSSESession, { newType }: { newType: string }) {
  47. return {
  48. request: {
  49. endpoint: curr.request.endpoint,
  50. eventType: newType,
  51. },
  52. }
  53. },
  54. setSocket(_: HoppSSESession, { socket }: { socket: EventSource }) {
  55. return {
  56. socket,
  57. }
  58. },
  59. setConnectionState(_: HoppSSESession, { state }: { state: boolean }) {
  60. return {
  61. connectionState: state,
  62. }
  63. },
  64. setConnectingState(_: HoppSSESession, { state }: { state: boolean }) {
  65. return {
  66. connectingState: state,
  67. }
  68. },
  69. setLog(_: HoppSSESession, { log }: { log: HoppRealtimeLog }) {
  70. return {
  71. log,
  72. }
  73. },
  74. addLogLine(curr: HoppSSESession, { line }: { line: HoppRealtimeLogLine }) {
  75. return {
  76. log: [...curr.log, line],
  77. }
  78. },
  79. })
  80. const SSESessionStore = new DispatchingStore(defaultSSESession, dispatchers)
  81. export function setSSERequest(newRequest?: HoppSSERequest) {
  82. SSESessionStore.dispatch({
  83. dispatcher: "setRequest",
  84. payload: {
  85. newRequest: newRequest ?? defaultSSERequest,
  86. },
  87. })
  88. }
  89. export function setSSEEndpoint(newEndpoint: string) {
  90. SSESessionStore.dispatch({
  91. dispatcher: "setEndpoint",
  92. payload: {
  93. newEndpoint,
  94. },
  95. })
  96. }
  97. export function setSSEEventType(newType: string) {
  98. SSESessionStore.dispatch({
  99. dispatcher: "setEventType",
  100. payload: {
  101. newType,
  102. },
  103. })
  104. }
  105. export function setSSESocket(socket: EventSource) {
  106. SSESessionStore.dispatch({
  107. dispatcher: "setSocket",
  108. payload: {
  109. socket,
  110. },
  111. })
  112. }
  113. export function setSSEConnectionState(state: boolean) {
  114. SSESessionStore.dispatch({
  115. dispatcher: "setConnectionState",
  116. payload: {
  117. state,
  118. },
  119. })
  120. }
  121. export function setSSEConnectingState(state: boolean) {
  122. SSESessionStore.dispatch({
  123. dispatcher: "setConnectingState",
  124. payload: {
  125. state,
  126. },
  127. })
  128. }
  129. export function setSSELog(log: HoppRealtimeLog) {
  130. SSESessionStore.dispatch({
  131. dispatcher: "setLog",
  132. payload: {
  133. log,
  134. },
  135. })
  136. }
  137. export function addSSELogLine(line: HoppRealtimeLogLine) {
  138. SSESessionStore.dispatch({
  139. dispatcher: "addLogLine",
  140. payload: {
  141. line,
  142. },
  143. })
  144. }
  145. export const SSERequest$ = SSESessionStore.subject$.pipe(
  146. pluck("request"),
  147. distinctUntilChanged()
  148. )
  149. export const SSEEndpoint$ = SSESessionStore.subject$.pipe(
  150. pluck("request", "endpoint"),
  151. distinctUntilChanged()
  152. )
  153. export const SSEEventType$ = SSESessionStore.subject$.pipe(
  154. pluck("request", "eventType"),
  155. distinctUntilChanged()
  156. )
  157. export const SSEConnectingState$ = SSESessionStore.subject$.pipe(
  158. pluck("connectingState"),
  159. distinctUntilChanged()
  160. )
  161. export const SSEConnectionState$ = SSESessionStore.subject$.pipe(
  162. pluck("connectionState"),
  163. distinctUntilChanged()
  164. )
  165. export const SSESocket$ = SSESessionStore.subject$.pipe(
  166. pluck("socket"),
  167. distinctUntilChanged()
  168. )
  169. export const SSELog$ = SSESessionStore.subject$.pipe(
  170. pluck("log"),
  171. distinctUntilChanged()
  172. )