Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open System
- open Hopac
- open Hopac.Alt.Infixes
- open Hopac.Job.Infixes
- type Expiring = { Complete : Job<unit> }
- [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
- module Expiring =
- let create name minTime windowTime =
- let complete = ivar()
- Job.delay <| fun _ ->
- [ complete |>>? fun _ -> printfn "%s: early" name; Choice1Of2 ()
- timeOutMillis minTime >>%? Choice2Of2 () ]
- |> Alt.choose
- >>= function
- | Choice1Of2 () -> Job.unit()
- | Choice2Of2 () ->
- (complete |>>? fun _ -> printfn "%s: in time" name) <|>?
- (timeOutMillis windowTime >>%? printfn "%s: late" name) :> _
- |> start
- { Complete = IVar.tryFill complete () }
- let e1 = Expiring.create "e1" 1000 1000
- let e2 = Expiring.create "e2" 1000 1000
- let e3 = Expiring.create "e3" 1000 1000
- printfn "Completing e1 early!"
- run <| e1.Complete
- Async.RunSynchronously <| Async.Sleep 1500
- printfn "Completing e2 on time!"
- run <| e2.Complete
- printfn "Waiting for e3 to time out..."
- Console.ReadLine() |> ignore
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement