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