Skip to content

Commit

Permalink
Refactor how wait works to not use recursion for #388
Browse files Browse the repository at this point in the history
  • Loading branch information
lefthandedgoat committed Oct 17, 2017
1 parent 6c2be1f commit ff76779
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/canopy/wait.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit ff76779

Please sign in to comment.