import { MachineRunner, State, StateOpaque } from '@actyx/machine-runner' import { StateFactory } from '@actyx/machine-runner/lib/design/state.js' import { useEffect, useState } from 'react' import { AuctionP, AuctionT, FirstBidT, InitialP, InitialT, RideP, RideT } from './machines.js' import { PrintState } from './UIMachineCommon.js' import { UIAuctionP, UIInitialP, UIRideP } from './UIMachinePassenger.js' import { UIAuctionT, UIFirstBidT, UIInitialT, UIRideT } from './UIMachineTaxi.js' export const UIMachine = ({ machine, name }: { name: string; machine: MachineRunner }) => { const [state, setState] = useState(machine.get()) useEffect(() => { let active = true ;(async () => { for await (const state of machine) { if (!active) { break } setState(state) } })() return () => { active = false } }, [machine.id]) if (state.is(AuctionP)) { const { bids } = state.payload // just to demonstrate that `state.is()` works console.log(bids) } type Unknown = T type NoAny = T extends Unknown ? U : never function noAny(t: T): NoAny { return t } const sAuction = noAny(state.as(AuctionP)) type StateAsNoAny = StateIsh> return (
{state.as(InitialP, (state) => ( ))} {state.as(AuctionP, (state) => ( ))} {state.as(RideP, (state) => ( ))} {state.as(InitialT, (state) => ( ))} {state.as(FirstBidT, (state) => ( ))} {state.as(AuctionT, (state) => ( ))} {state.as(RideT, (state) => ( ))}
) }