From 67905f7c67d3feaf440b58d682052ce8b276860c Mon Sep 17 00:00:00 2001 From: Julian Date: Mon, 23 Oct 2023 23:32:45 +0100 Subject: [PATCH] refactor and fix history bug --- src/main/scala/controller.scala | 19 ++++++++++--------- src/main/scala/uicomponents.scala | 6 +++--- src/main/scala/view.scala | 4 ++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/scala/controller.scala b/src/main/scala/controller.scala index c079417..8bd69d1 100644 --- a/src/main/scala/controller.scala +++ b/src/main/scala/controller.scala @@ -14,12 +14,13 @@ import rescala.default import miniscribe.DataBackend.HeroOption // ==== World events ==== -object ArmyEvents: +object armyEvents: val addForce = Evt[String]() // forceName val deleteForce = Evt[String]() // forceName val addWarband = Evt[(Force, String)]() // force, heroName + val all = addForce || deleteForce || addWarband -object NavigationEvents: +object navigationEvents: val forwardBackward = Evt[Unit]() // ====================== @@ -36,13 +37,13 @@ class Controller: else AppState() // app state can be changed through these events - private val addForceAct = ArmyEvents.addForce.act[AppState] { f => + private val addForceAct = armyEvents.addForce.act[AppState] { f => current.copy(forces = current.forces :+ Force(f, List())) } - private val delForceAct = ArmyEvents.deleteForce.act[AppState] { f => + private val delForceAct = armyEvents.deleteForce.act[AppState] { f => current.copy(forces = current.forces.filter(_._1 != f)) } - private val addWarbandAct = ArmyEvents.addWarband.act[AppState] { (f, h) => + private val addWarbandAct = armyEvents.addWarband.act[AppState] { (f, h) => current.copy(forces = current.forces.map { case g @ Force(name, warbands, _) if f == g => val newHero = Hero(model = Some(Model(name = h))) @@ -50,7 +51,7 @@ class Controller: case g => g }) } - private val forwBackwAct = NavigationEvents.forwardBackward.act[AppState] { + private val forwBackwAct = navigationEvents.forwardBackward.act[AppState] { _ => parseState() // whenever we detect a forward/backward event, simply parse state from proto } @@ -108,11 +109,11 @@ class Controller: // ===== Browser history API a.k.a. handle forward/backward events ======= // update history when AppState changes but not on forward/backward events private val lastEvent = - (state.changed.map(_ => "armyChange") || NavigationEvents.forwardBackward + (armyEvents.all.map(_ => "armyChange") || navigationEvents.forwardBackward .map(_ => "fb")) .latest() Signal { (lastEvent(), state()) }.observe { - case ("fb", _) => () + case ("fb", _) => () // don't do anything here case (_, state) => val p = Base64.getEncoder.encodeToString( state.toByteArray @@ -131,7 +132,7 @@ class Controller: dom.window.addEventListener( "popstate", { (e: PopStateEvent) => - NavigationEvents.forwardBackward.fire() + navigationEvents.forwardBackward.fire() } ) // ================================================================== diff --git a/src/main/scala/uicomponents.scala b/src/main/scala/uicomponents.scala index 54bdd4e..ebeedd4 100644 --- a/src/main/scala/uicomponents.scala +++ b/src/main/scala/uicomponents.scala @@ -23,7 +23,7 @@ object UIComponent: // toggle button presses toggleMenuEvt act2 (id => acc.updated(id, !acc(id))), // force removals - ArmyEvents.deleteForce act2 (forceName => acc.updated(forceName, false)) + armyEvents.deleteForce act2 (forceName => acc.updated(forceName, false)) ) } @@ -52,7 +52,7 @@ case class ForceComponent( a( "delete", onclick := { () => - miniscribe.ArmyEvents.deleteForce.fire(force.name) + miniscribe.armyEvents.deleteForce.fire(force.name) } ) ), @@ -68,7 +68,7 @@ case class ForceComponent( a( heroName, onclick := { () => - ArmyEvents.addWarband.fire((force, heroName)) + armyEvents.addWarband.fire((force, heroName)) } ) ) diff --git a/src/main/scala/view.scala b/src/main/scala/view.scala index 068dd13..f50f0e7 100644 --- a/src/main/scala/view.scala +++ b/src/main/scala/view.scala @@ -81,14 +81,14 @@ class View(controller: Controller): def addForceButton(army: String): TypedTag[Element] = val cb = Events.fromCallback[UIEvent](cb => a(army, onclick := cb)) - cb.event.observe(_ => miniscribe.ArmyEvents.addForce.fire(army)) + cb.event.observe(_ => miniscribe.armyEvents.addForce.fire(army)) cb.event.observe(_ => forcesMenu.toggle.fire("addForce")) return cb.data def removeForceButton(army: String): TypedTag[Element] = val cb = Events.fromCallback[UIEvent](cb => a("delete", onclick := cb)) - cb.event.observe(_ => miniscribe.ArmyEvents.deleteForce.fire(army)) + cb.event.observe(_ => miniscribe.armyEvents.deleteForce.fire(army)) return cb.data val forcesMenu = toggleMenu(