From 1045e0388b8f85bc6f8bd78e5ef3f570ac0af054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9=20Larivi=C3=A8re?= Date: Wed, 14 Feb 2024 16:34:03 +0100 Subject: [PATCH] Change to lock object --- src/Fabulous/Cmd.fs | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/Fabulous/Cmd.fs b/src/Fabulous/Cmd.fs index 0cadd9c87..4a4b96956 100644 --- a/src/Fabulous/Cmd.fs +++ b/src/Fabulous/Cmd.fs @@ -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 + )) ]