Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. open System
  2. open Hopac
  3. open Hopac.Alt.Infixes
  4. open Hopac.Job.Infixes
  5.  
  6. type Expiring = { Complete : Job<unit> }
  7.  
  8. [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
  9. module Expiring =
  10. let create name minTime windowTime =
  11. let complete = ivar()
  12. Job.delay <| fun _ ->
  13. [ complete |>>? fun _ -> printfn "%s: early" name; Choice1Of2 ()
  14. timeOutMillis minTime >>%? Choice2Of2 () ]
  15. |> Alt.choose
  16. >>= function
  17. | Choice1Of2 () -> Job.unit()
  18. | Choice2Of2 () ->
  19. (complete |>>? fun _ -> printfn "%s: in time" name) <|>?
  20. (timeOutMillis windowTime >>%? printfn "%s: late" name) :> _
  21. |> start
  22. { Complete = IVar.tryFill complete () }
  23.  
  24. let e1 = Expiring.create "e1" 1000 1000
  25. let e2 = Expiring.create "e2" 1000 1000
  26. let e3 = Expiring.create "e3" 1000 1000
  27.  
  28. printfn "Completing e1 early!"
  29. run <| e1.Complete
  30.  
  31. Async.RunSynchronously <| Async.Sleep 1500
  32. printfn "Completing e2 on time!"
  33. run <| e2.Complete
  34.  
  35. printfn "Waiting for e3 to time out..."
  36.  
  37. Console.ReadLine() |> ignore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement