Skip to content

Commit

Permalink
Change to lock object
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothé Larivière committed Feb 14, 2024
1 parent 746a80e commit 1045e03
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/Fabulous/Cmd.fs
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,28 @@ module Cmd =

/// 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 funLock = obj()
let mutable cts: CancellationTokenSource = null

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

cts <- new CancellationTokenSource()

Async.Start(
async {
do! Async.Sleep(timeout)
lock fn (fun () ->
dispatch(fn value)

if cts <> null then
cts.Dispose()
cts <- null
)
},
cts.Token
) ) ]
lock funLock (fun () ->
if cts <> null then
cts.Cancel()
cts.Dispose()

cts <- new CancellationTokenSource()

Async.Start(
async {
do! Async.Sleep(timeout)

lock funLock (fun () ->
dispatch(fn value)

if cts <> null then
cts.Dispose()
cts <- null)
},
cts.Token
)) ]

0 comments on commit 1045e03

Please sign in to comment.