Skip to content
Snippets Groups Projects
Commit a85dbad5 authored by Clément Pit-Claudel's avatar Clément Pit-Claudel
Browse files

server: Remove Tick event encoder

parent e80cb764
No related branches found
No related tags found
No related merge requests found
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment