From ff767799f5220350b8b392f33ba045b468889fbb Mon Sep 17 00:00:00 2001 From: Chris Holt Date: Tue, 17 Oct 2017 15:45:06 -0500 Subject: [PATCH] Refactor how wait works to not use recursion for #388 --- src/canopy/wait.fs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/canopy/wait.fs b/src/canopy/wait.fs index 166fad3a..c2ef02dc 100644 --- a/src/canopy/wait.fs +++ b/src/canopy/wait.fs @@ -7,26 +7,31 @@ let mutable waitSleep = 0.5 let waitResults timeout (f : unit -> 'a) = let sw = System.Diagnostics.Stopwatch.StartNew() - let rec innerwait timeout f = - let sleepAndWait () = System.Threading.Thread.Sleep(int (waitSleep * 1000.0)); innerwait timeout f - if sw.Elapsed.TotalSeconds >= timeout then raise <| WebDriverTimeoutException("Timed out!") - + let mutable finalResult : 'a = Unchecked.defaultof<'a> + let mutable keepGoing = true + while keepGoing do try + if sw.Elapsed.TotalSeconds >= timeout then raise <| WebDriverTimeoutException("Timed out!") + let result = f() match box result with | :? bool as b -> - if b then result - else sleepAndWait () + if b then + keepGoing <- false + finalResult <- result + else System.Threading.Thread.Sleep(int (waitSleep * 1000.0)) | _ as o -> - if o <> null then result - else sleepAndWait () + if o <> null then + keepGoing <- false + finalResult <- result + else System.Threading.Thread.Sleep(int (waitSleep * 1000.0)) with | :? WebDriverTimeoutException as ex -> reraise() | :? CanopyException as ce -> raise(ce) - | _ -> sleepAndWait () - - innerwait timeout f + | _ -> System.Threading.Thread.Sleep(int (waitSleep * 1000.0)) + finalResult + let wait timeout (f : unit -> bool) = waitResults timeout f |> ignore