The Classic Approach

Let’s briefly recap how we would implement this using the classic approach.

States and Message types

The PureScript types now include data in both states and messages:

data State
  = DoorOpen
  | DoorClosed
  | DoorLocked { storedPin :: String }

data Msg
  = Close
  | Open
  | Lock { newPin :: String }
  | Unlock { enteredPin :: String }

πŸ—Ž test/Examples/Classic/DoorPin.purs L10-L27

The update function

Accordingly the update function now needs to handle state and message data:

update :: State -> Msg -> State
update state msg = case state, msg of
  DoorOpen, Close -> DoorClosed
  DoorClosed, Open -> DoorOpen
  DoorClosed, Lock { newPin } -> DoorLocked { storedPin: newPin }
  DoorLocked { storedPin }, Unlock { enteredPin } ->
    if storedPin == enteredPin then
      DoorClosed
    else
      DoorLocked { storedPin }
  _, _ -> state

πŸ—Ž test/Examples/Classic/DoorPin.purs L29-L39

↑ Back to top