import { assign, send } from "xstate"; const FSM: any = { id: "chatApp", initial: "setup", states: { setup: { invoke: { src: "setupMachine", data: (ctx: any) => ctx, onError: "error", onDone: [ { cond: (ctx: any, evt: any) => { return !evt.data; }, target: "error", }, { target: "chatting", actions: assign((ctx: any, evt: any) => ({ ...ctx, ...evt.data })), }, ], }, }, chatting: { on: { START_CHAT: { actions: send("START", { to: "chatMachine" }), }, RESET_CHAT: { target: "setup", actions: "onResetContext", }, }, invoke: { id: "chatMachine", src: "chatMachine", data: (ctx: any) => ctx, onDone: "close", onError: "error", }, }, close: { type: "final" }, error: { after: { 10000: { cond: (ctx: any) => { return ctx.retryCount < 10; }, actions: assign({ retryCount: (ctx: any) => ctx.retryCount + 1 }), target: "setup", }, }, }, }, }; export default FSM;