Replies: 1 comment 1 reply
-
Maybe you could try handling the Closed event in your This is modifying the type MainWindow() as this =
inherit HostWindow()
do
base.Title <- "Clock Example"
base.Height <- 200.0
base.MaxHeight <- 200.0
base.MinHeight <- 200.0
base.Width <- 200.0
base.MaxWidth <- 200.0
base.MinWidth <- 200.0
let timer (_state: Clock.State) =
let sub (dispatch: Clock.Msg -> unit) =
let invoke() =
DateTime.Now |> Clock.Msg.Tick |> dispatch
true
DispatcherTimer.Run(Func<bool>(invoke), TimeSpan.FromMilliseconds 1000.0) |> ignore
Cmd.ofSub sub
// Save state when host window is Closed
let onClosedSub (state: Clock.State) =
Cmd.ofSub (fun _ ->
this.Closed.Subscribe(fun e ->
printfn "Saving state: %A" state
)
|> ignore
)
Elmish.Program.mkSimple (fun () -> Clock.init) Clock.update Clock.view
|> Program.withHost this
|> Program.withSubscription timer
|> Program.withSubscription onClosedSub
|> Program.withConsoleTrace
|> Program.run
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to save on disk app state when the main window (or the whole app) is closed/closing. I was trying to pass HostWindow reference object (which has Closing/Closed events) into the Update function and save serialized model in file when the event is triggered. However this caused the whole sequence of intermediate state values from the start of the app to be saved in file (except the last one which I only need). If I'm baking this event handling in the View function the behavior is similar, just the last state value is included as well. If I'm saving state in ref parameter of Update function and handling the event from inside the MainWindow() class then the single previous state value before window closing is saved. Is there some idiomatic way to handle this kind of effects?
Beta Was this translation helpful? Give feedback.
All reactions