Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that latest model is always applied in threaded case #577

Merged
merged 3 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 4.0.0-beta-52
* Fixed a bug in the threaded dispatch case that could result in an out-of-date model being updated.
* Skip intermediate updates to view model when they queue up in the threaded case.

#### 4.0.0-beta-50
marner2 marked this conversation as resolved.
Show resolved Hide resolved
* Upgraded to Elmish v4
* **BREAKING:** Changed syntax of `WpfProgram.withSubscription` to now take named list of `Subscribe<'msg> = Dispatch<'msg> -> IDisposable` (note the `IDisposable` return). This function now gets called every time `'model` updates (previously it was only called on startup). This allows starting and stopping of subscriptions from this function by the given string list identifier.
Expand Down
2 changes: 1 addition & 1 deletion src/Elmish.WPF/Elmish.WPF.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageProjectUrl>https://github.com/elmish/Elmish.WPF</PackageProjectUrl>
<PackageTags>WPF F# fsharp Elmish Elm</PackageTags>
<PackageIcon>elmish-wpf-logo-128x128.png</PackageIcon>
<Version>4.0.0-beta-50</Version>
<Version>4.0.0-beta-52</Version>
<PackageReleaseNotes>https://github.com/elmish/Elmish.WPF/blob/master/RELEASE_NOTES.md</PackageReleaseNotes>
<!--Turn on warnings for unused values (arguments and let bindings) -->
<OtherFlags>$(OtherFlags) --warnon:1182</OtherFlags>
Expand Down
17 changes: 15 additions & 2 deletions src/Elmish.WPF/WpfProgram.fs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ module WpfProgram =

// Core Elmish calls this from `dispatch`, which means this is always called from `elmishDispatcher`
// (which is UI thread in single-threaded case)
let mutable pendingModel = ValueNone
let setUiState model _syncDispatch =
match viewModel with
| None -> // no view model yet, so create one
Expand All @@ -189,11 +190,23 @@ module WpfProgram =
| Some vm -> // view model exists, so update
match threader with
| Threaded_UIDispatch uiWaiter -> // We are in the specific dispatch call from the UI thread (see `synchronizedUiDispatch` in `dispatchFromViewModel`)
uiWaiter.SetResult(fun () -> program.UpdateViewModel (vm, model)) // execute `UpdateViewModel` on UI thread
uiWaiter.SetResult(fun () -> program.UpdateViewModel (vm, model); pendingModel <- ValueNone) // execute `UpdateViewModel` on UI thread
| Threaded_PendingUIDispatch _ -> // We are in a non-UI dispatch that updated the model before the UI got its update in, but after the user interacted
() // Skip updating the UI since the screen is frozen anyways, and `program.UpdateViewModel` is fully transitive
| Threaded_NoUIDispatch -> // We are in a non-UI dispatch with no pending user interactions known
element.Dispatcher.InvokeAsync(fun () -> program.UpdateViewModel (vm, model)) |> ignore // Schedule update normally
let scheduleJob () =
pendingModel <- ValueSome model

let executeJob () =
match pendingModel with
| ValueSome m ->
program.UpdateViewModel (vm, m)
pendingModel <- ValueNone
| ValueNone ->
bindingsLogger.LogDebug("Job was empty - No update done.")

element.Dispatcher.InvokeAsync(scheduleJob, Threading.DispatcherPriority.Normal) |> ignore // Schedule update
element.Dispatcher.InvokeAsync(executeJob, Threading.DispatcherPriority.Background) |> ignore // Execute Update
| SingleThreaded -> // If we aren't using different threads, always process normally
element.Dispatcher.Invoke(fun () -> program.UpdateViewModel (vm, model))

Expand Down
Loading