license import { Actor , lookup } from "actor-helpers/src/actor/Actor.js" ; declare global { interface ActorMessageType { state : Message ; } } export interface State { count : number ; } export enum MessageType { INCREMENT , DECREMENT } export interface IncrementMessage { type : MessageType . INCREMENT ; } export interface DecrementMessage { type : MessageType . DECREMENT ; } export type Message = IncrementMessage | DecrementMessage ; export default class StateActor extends Actor < Message > { private ui = lookup ( "ui" ) ; private state : State = { count : 0 } ; async onMessage ( msg ) { switch ( msg . type ) { case MessageType . INCREMENT : this . state . count += 1 ; break ; case MessageType . DECREMENT : this . state . count -= 1 ; if ( this . state . count <= 0 ) { this . state . count = 0 ; } break ; } this . ui . send ( { state : this . state } ) ; } }