import { ServerWebsocketMessage } from "@garden-io/core/build/src/server/server" ; import { Events } from "@garden-io/core/build/src/events" ; import { Entities , SupportedEventName , supportedEventNames , TaskState , taskStates } from "../contexts/api" ; import produce from "immer" ; export type WsEventMessage = ServerWebsocketMessage & { type : "event" ; name : SupportedEventName ; payload : Events [ SupportedEventName ] ; } ; interface TaskStateChangeEventMessage { type : "event" ; name : TaskState ; payload : Events [ TaskState ] ; } /**
 * Type guard to check whether websocket message is a type supported by the Dashboard
 */ export function isSupportedEvent ( msg ) { return msg . type === "event" && supportedEventNames . has ( ( msg as WsEventMessage ) . name ) ; } /**
 * Type guard to check whether the websocket event is for a task state change that is handled
 * by the Dashboard.
 */ export function isTaskStateChangeEvent ( msg ) { return taskStates . includes ( msg . name ) ; } export function processWebSocketMessage ( store , message ) { return produce ( store , ( draft ) => { if ( isTaskStateChangeEvent ( message ) ) { const taskState = message . name ; const payload = message . payload ; const entityName = payload . name ; draft . project . taskGraphProcessing = true ; switch ( payload . type ) { case "publish" : break ; case "deploy" : draft . services [ entityName ] = { ... draft . services [ entityName ] , taskState , } ; break ; case "build" : draft . modules [ entityName ] = { ... store . modules [ entityName ] , taskState , } ; break ; case "run" : draft . tasks [ entityName ] = { ... store . tasks [ entityName ] , taskState , } ; break ; case "test" : draft . tests [ entityName ] = { ... store . tests [ entityName ] , taskState , } ; break ; } } if ( message . name === "taskGraphComplete" ) { draft . project . taskGraphProcessing = false ; } } ) ; }