SSESession.ts 3.3 KB

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