Skip to content

Commit

Permalink
Display id of created timer (closes #29)
Browse files Browse the repository at this point in the history
  • Loading branch information
sullyj3 committed Aug 3, 2024
1 parent ba5b0c0 commit 265fd38
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Sand/Message.lean
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ inductive Command

-- responses to commands sent from server to client
inductive AddTimerResponse
| ok
| ok (createdId : TimerId)
deriving Repr, ToJson, FromJson

inductive ListResponse
Expand Down
4 changes: 2 additions & 2 deletions src/Sand/SandClient.lean
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ def handleCmd (server : Socket) (cmd : Command) : IO Unit := do
-- Handle response
match cmd with
| Command.addTimer timer => do
let .ok := resp
println! "Timer created for {timer.formatColonSeparated}."
let .ok id := resp
println! "Timer #{repr id.id} created for {timer.formatColonSeparated}."
| Command.cancelTimer timerId => match resp with
| .ok =>
println! "Timer #{repr timerId.id} cancelled."
Expand Down
7 changes: 4 additions & 3 deletions src/Sand/SandDaemon.lean
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def DaemonState.initial : IO DaemonState := do
timers := (← IO.Mutex.new ∅)
}

def addTimer (duration : Duration) : CmdHandlerT IO Unit := do
def addTimer (duration : Duration) : CmdHandlerT IO TimerId := do
let {clientConnectedTime, state, ..} ← read

let msg := s!"Starting timer for {duration.formatColonSeparated}"
Expand All @@ -160,13 +160,14 @@ def addTimer (duration : Duration) : CmdHandlerT IO Unit := do
let countdownTask ← (countdown id due).asTask .dedicated

state.timers.atomically <| modify (·.insert id (timer, .running countdownTask))
return id

def handleClientCmd (cmd : Command) : CmdHandlerT IO (ResponseFor cmd) := do
let {state, ..} ← read
match cmd with
| .addTimer duration => do
addTimer duration
return .ok
let id ← addTimer duration
return .ok id
| .cancelTimer which => removeTimer which
| .list => do
let timers ← state.timers.atomically get
Expand Down

0 comments on commit 265fd38

Please sign in to comment.