diff --git a/jvm/src/main/scala/cs214/webapp/server/StateMachine.scala b/jvm/src/main/scala/cs214/webapp/server/StateMachine.scala index fb6f308c732de6ea6e1eeae30fe740389f67c780..cbff790546e68fa025a730e09061138e5e931861 100644 --- a/jvm/src/main/scala/cs214/webapp/server/StateMachine.scala +++ b/jvm/src/main/scala/cs214/webapp/server/StateMachine.scala @@ -74,23 +74,15 @@ abstract class ClockDrivenStateMachine[Event, State, View] */ val clockDrivenWire: AppWire[Event, View] - override object wire extends AppWire[Either[Tick, Event], View]: - override object eventFormat extends WireFormat[Either[Tick, Event]]: - override def decode(json: ujson.Value): Try[Either[Tick, Event]] = - Try: - val isClockEvent = - json.objOpt - .flatMap(_.get(ClockDrivenStateMachine.CLOCK_EVENT_HEADER)) - .exists(_.bool) + override object wire extends ClockDrivenWire(clockDrivenWire) - if isClockEvent then Left(TickEventFormat.decode(json.obj.get("tick").get).get) - else Right(clockDrivenWire.eventFormat.decode(json).get) +private class ClockDrivenWire[Event, View](wire: AppWire[Event, View]) extends AppWire[Either[Tick, Event], View]: + override object eventFormat extends WireFormat[Either[Tick, Event]]: + override def decode(json: ujson.Value): Try[Either[Tick, Event]] = Try: + Right(wire.eventFormat.decode(json).get) - override def encode(t: Either[Tick, Event]): ujson.Value = - throw IllegalStateException("This shouldn't be used. Please use the ScalApp's wire instead.") + override def encode(t: Either[Tick, Event]): ujson.Value = + require(t.isRight, "Tick events should not be sent over the network.") + wire.eventFormat.encode(t.toOption.get) - override val viewFormat: WireFormat[View] = clockDrivenWire.viewFormat - -object ClockDrivenStateMachine: - val CLOCK_EVENT_HEADER = "X-Clock" - val CLOCK_EVENT_TICK_HEADER = "tick" + override val viewFormat: WireFormat[View] = wire.viewFormat