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

Add Cmd.debounce #1062

Merged
merged 3 commits into from
Jan 29, 2024
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
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"fantomas": {
"version": "5.2.0",
"version": "6.2.3",
"commands": [
"fantomas"
]
Expand Down
43 changes: 43 additions & 0 deletions src/Fabulous.Tests/CmdTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Fabulous.Tests

open Fabulous
open NUnit.Framework

type CmdTestsMsg = NewValue of int

module CmdTestsHelper =
let execute dispatch (cmd: Cmd<'msg>) =
for sub in cmd do
sub dispatch

[<TestFixture>]
type ``Cmd tests``() =
[<Test>]
member _.``Cmd.debounce only dispatch the last message``() =
async {
let mutable actualValue = None

let dispatch msg =
if actualValue.IsNone then
actualValue <- Some msg

let triggerCmd = Cmd.debounce 100 NewValue

triggerCmd 1 |> CmdTestsHelper.execute dispatch
do! Async.Sleep 50
triggerCmd 2 |> CmdTestsHelper.execute dispatch
do! Async.Sleep 75
triggerCmd 3 |> CmdTestsHelper.execute dispatch
do! Async.Sleep 125

Assert.AreEqual(Some(NewValue 3), actualValue)

actualValue <- None

triggerCmd 4 |> CmdTestsHelper.execute dispatch
do! Async.Sleep 75
triggerCmd 5 |> CmdTestsHelper.execute dispatch
do! Async.Sleep 125

Assert.AreEqual(Some(NewValue 5), actualValue)
}
1 change: 1 addition & 0 deletions src/Fabulous.Tests/Fabulous.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Compile Include="AttributesTests.fs" />
<Compile Include="ViewTests.fs" />
<Compile Include="ArrayTests.fs" />
<Compile Include="CmdTests.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
Expand Down
6 changes: 3 additions & 3 deletions src/Fabulous/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ type WidgetBuilder<'msg, 'marker> =
DebugName = $"{typeof<'marker>.Name}<{typeof<'msg>.Name}>"
#endif
ScalarAttributes =
match StackList.length &scalarAttributes with
| 0us -> ValueNone
| _ -> ValueSome(Array.sortInPlace (fun a -> a.Key) (StackList.toArray &scalarAttributes))
match StackList.length &scalarAttributes with
| 0us -> ValueNone
| _ -> ValueSome(Array.sortInPlace (fun a -> a.Key) (StackList.toArray &scalarAttributes))

WidgetAttributes = ValueOption.map (Array.sortInPlace(fun a -> a.Key)) widgetAttributes

Expand Down
24 changes: 24 additions & 0 deletions src/Fabulous/Cmd.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Fabulous

open System.Threading
open System.Threading.Tasks

/// Dispatch - feed new message into the processing loop
Expand Down Expand Up @@ -104,3 +105,26 @@ module Cmd =
dispatch(failure ex)
}
|> ignore ]

/// Command to issue a message if no other message has been issued within the specified timeout
let debounce (timeout: int) (fn: 'value -> 'msg) : 'value -> Cmd<'msg> =
let mutable cts: CancellationTokenSource = null

fun (value: 'value) ->
[ fun dispatch ->
if cts <> null then
cts.Cancel()
cts.Dispose()

cts <- new CancellationTokenSource()

Async.Start(
async {
do! Async.Sleep(timeout)
dispatch(fn value)

cts.Dispose()
cts <- null
},
cts.Token
) ]
Loading