|
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import { assign, AssignAction, sendTo, choose } from 'xstate'; |
9 | | -import { Context, Event, EventTypes, StandardFullNodeEvent } from '../types'; |
| 9 | +import { Context, Event, EventTypes, MonitoringEvent, StandardFullNodeEvent } from '../types'; |
10 | 10 | import { get } from 'lodash'; |
11 | 11 | import logger from '../logger'; |
12 | 12 | import { hashTxData } from '../utils'; |
@@ -211,53 +211,14 @@ export const getMonitoringRefFromContext = (context: Context) => { |
211 | 211 |
|
212 | 212 | const monitoringIsPresent = (context: Context) => context.monitoring !== null; |
213 | 213 |
|
214 | | -/* |
215 | | - * Notifies the monitoring actor that the WebSocket became connected. |
216 | | - */ |
217 | | -export const sendMonitoringConnected = choose([{ |
218 | | - cond: monitoringIsPresent, |
219 | | - actions: sendTo(getMonitoringRefFromContext, { type: EventTypes.MONITORING_EVENT, event: { type: 'CONNECTED' } }), |
220 | | -}]); |
221 | | - |
222 | | -/* |
223 | | - * Notifies the monitoring actor that the WebSocket disconnected. |
224 | | - */ |
225 | | -export const sendMonitoringDisconnected = choose([{ |
226 | | - cond: monitoringIsPresent, |
227 | | - actions: sendTo(getMonitoringRefFromContext, { type: EventTypes.MONITORING_EVENT, event: { type: 'DISCONNECTED' } }), |
228 | | -}]); |
229 | | - |
230 | | -/* |
231 | | - * Notifies the monitoring actor that a fullnode event was received (resets the idle timer). |
232 | | - */ |
233 | | -export const sendMonitoringEventReceived = choose([{ |
234 | | - cond: monitoringIsPresent, |
235 | | - actions: sendTo(getMonitoringRefFromContext, { type: EventTypes.MONITORING_EVENT, event: { type: 'EVENT_RECEIVED' } }), |
236 | | -}]); |
237 | | - |
238 | | -/* |
239 | | - * Notifies the monitoring actor that the machine is entering the RECONNECTING state. |
240 | | - */ |
241 | | -export const sendMonitoringReconnecting = choose([{ |
| 214 | +const makeMonitoringSender = (eventType: MonitoringEvent['type']) => choose([{ |
242 | 215 | cond: monitoringIsPresent, |
243 | | - actions: sendTo(getMonitoringRefFromContext, { type: EventTypes.MONITORING_EVENT, event: { type: 'RECONNECTING' } }), |
| 216 | + actions: sendTo(getMonitoringRefFromContext, { type: EventTypes.MONITORING_EVENT, event: { type: eventType } }), |
244 | 217 | }]); |
245 | 218 |
|
246 | | -/* |
247 | | - * Notifies the monitoring actor that a processing state was entered. |
248 | | - * The actor starts a stuck-detection timer; if PROCESSING_COMPLETED doesn't |
249 | | - * arrive within STUCK_PROCESSING_TIMEOUT_MS it fires a MAJOR alert. |
250 | | - */ |
251 | | -export const sendMonitoringProcessingStarted = choose([{ |
252 | | - cond: monitoringIsPresent, |
253 | | - actions: sendTo(getMonitoringRefFromContext, { type: EventTypes.MONITORING_EVENT, event: { type: 'PROCESSING_STARTED' } }), |
254 | | -}]); |
255 | | - |
256 | | -/* |
257 | | - * Notifies the monitoring actor that a processing state was exited normally, |
258 | | - * cancelling the stuck-detection timer. |
259 | | - */ |
260 | | -export const sendMonitoringProcessingCompleted = choose([{ |
261 | | - cond: monitoringIsPresent, |
262 | | - actions: sendTo(getMonitoringRefFromContext, { type: EventTypes.MONITORING_EVENT, event: { type: 'PROCESSING_COMPLETED' } }), |
263 | | -}]); |
| 219 | +export const sendMonitoringConnected = makeMonitoringSender('CONNECTED'); |
| 220 | +export const sendMonitoringDisconnected = makeMonitoringSender('DISCONNECTED'); |
| 221 | +export const sendMonitoringEventReceived = makeMonitoringSender('EVENT_RECEIVED'); |
| 222 | +export const sendMonitoringReconnecting = makeMonitoringSender('RECONNECTING'); |
| 223 | +export const sendMonitoringProcessingStarted = makeMonitoringSender('PROCESSING_STARTED'); |
| 224 | +export const sendMonitoringProcessingCompleted = makeMonitoringSender('PROCESSING_COMPLETED'); |
0 commit comments