diff --git a/src/Fabulous/Cmd.fs b/src/Fabulous/Cmd.fs index 2c01fb7ad..4a4b96956 100644 --- a/src/Fabulous/Cmd.fs +++ b/src/Fabulous/Cmd.fs @@ -190,23 +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 -> - if cts <> null then - cts.Cancel() - cts.Dispose() + lock funLock (fun () -> + if cts <> null then + cts.Cancel() + cts.Dispose() - cts <- new CancellationTokenSource() + cts <- new CancellationTokenSource() - Async.Start( - async { - do! Async.Sleep(timeout) - dispatch(fn value) + Async.Start( + async { + do! Async.Sleep(timeout) - cts.Dispose() - cts <- null - }, - cts.Token - ) ] + lock funLock (fun () -> + dispatch(fn value) + + if cts <> null then + cts.Dispose() + cts <- null) + }, + cts.Token + )) ]