Skip to content

Commit

Permalink
Move from recusrive calls in wait to a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lefthandedgoat committed Oct 19, 2017
1 parent 98039e5 commit 01f081b
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/Canopy.Mobile/Wait.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,29 @@ let mutable waitSleep = 0.5
//Provide a function that returns a bool or object and if its true or not null then stop waiting
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 -> reraise()
| :? CanopyException as ce -> raise(ce)
| _ -> sleepAndWait ()

innerwait timeout f
| _ -> System.Threading.Thread.Sleep(int (waitSleep * 1000.0))

finalResult
//The most common form of waiting, wait for something to be true but ignore results
let wait timeout (f : unit -> bool) = waitResults timeout f |> ignore
let wait timeout (f : unit -> bool) = waitResults timeout f |> ignore

0 comments on commit 01f081b

Please sign in to comment.