2024-10-06 09:32:56 +01:00
|
|
|
structure UpdateThread =
|
|
|
|
|
struct
|
|
|
|
|
open CML
|
|
|
|
|
open MailboxType
|
2025-08-04 06:23:52 +01:00
|
|
|
open InputMsg
|
2024-10-06 09:32:56 +01:00
|
|
|
|
|
|
|
|
fun sendMsg (msg, drawMailbox) =
|
|
|
|
|
case msg of DRAW msg => Mailbox.send (drawMailbox, msg)
|
|
|
|
|
|
|
|
|
|
fun sendMsgs (msgList, drawMailbox) =
|
|
|
|
|
case msgList of
|
|
|
|
|
hd :: tl =>
|
|
|
|
|
let val _ = sendMsg (hd, drawMailbox)
|
|
|
|
|
in sendMsgs (tl, drawMailbox)
|
|
|
|
|
end
|
|
|
|
|
| [] => ()
|
|
|
|
|
|
2025-08-04 06:23:52 +01:00
|
|
|
val textCommands = ref ""
|
|
|
|
|
|
|
|
|
|
fun addTextCommand inputMsg =
|
|
|
|
|
case inputMsg of
|
|
|
|
|
CHAR_EVENT chr =>
|
|
|
|
|
let
|
|
|
|
|
val chr = CharVector.fromList [chr]
|
|
|
|
|
val newInput = !textCommands ^ chr
|
|
|
|
|
in
|
|
|
|
|
textCommands := newInput
|
|
|
|
|
end
|
|
|
|
|
| _ => ()
|
|
|
|
|
|
|
|
|
|
fun handleException e =
|
|
|
|
|
let
|
|
|
|
|
(* print stack trace for debugging purposes,
|
|
|
|
|
* and then raise another exception to exit the program *)
|
|
|
|
|
val stackTrace = MLton.Exn.history e
|
|
|
|
|
val stackTrace = String.concatWith "\n" stackTrace
|
|
|
|
|
val () = print "ERROR:\n"
|
|
|
|
|
val () = print (stackTrace ^ "\n\n")
|
|
|
|
|
|
|
|
|
|
val history = !textCommands ^ "\n\n"
|
|
|
|
|
val () = print ("HISTORY: " ^ history)
|
|
|
|
|
|
|
|
|
|
val textOutput = stackTrace ^ "\n" ^ history
|
|
|
|
|
|
|
|
|
|
val io = TextIO.openAppend "exceptions.log"
|
|
|
|
|
val () = TextIO.output (io, textOutput)
|
|
|
|
|
val () = TextIO.closeOut io
|
|
|
|
|
in
|
|
|
|
|
raise Empty
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-06 09:32:56 +01:00
|
|
|
fun loop (app: AppType.app_type, inputMailbox, drawMailbox) =
|
|
|
|
|
let
|
|
|
|
|
val inputMsg = Mailbox.recv inputMailbox
|
2025-08-04 06:23:52 +01:00
|
|
|
val () = addTextCommand inputMsg
|
|
|
|
|
|
|
|
|
|
val app = AppUpdate.update (app, inputMsg) handle e => handleException e
|
2025-08-03 13:14:28 +01:00
|
|
|
|
2024-12-17 10:40:06 +00:00
|
|
|
val () = sendMsgs (#msgs app, drawMailbox)
|
2024-10-06 09:32:56 +01:00
|
|
|
in
|
|
|
|
|
loop (app, inputMailbox, drawMailbox)
|
|
|
|
|
end
|
|
|
|
|
end
|