Skip to content

Commit

Permalink
Merge pull request #1068 from fabulous-dev/debounce-lock
Browse files Browse the repository at this point in the history
Lock access to fn in Cmd.debounce to avoid null crash
  • Loading branch information
TimLariviere committed Feb 19, 2024
2 parents 1beefea + 1045e03 commit 065ef67
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/Fabulous/Cmd.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)) ]

0 comments on commit 065ef67

Please sign in to comment.