Advertisement
Guest User

Untitled

a guest
Jan 31st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.55 KB | None | 0 0
  1. type Timer = { incomingTasks: Ch<DateTime * Alt<unit> * IVar<Alt<unit>>> }
  2.  
  3. module Timer =
  4.     type Task = {
  5.         wakeTime:   DateTime
  6.         cancel:     Alt<unit>
  7.         fire:       IVar<unit>
  8.     }
  9.  
  10.     let takeFirst tasks =
  11.         match tasks with
  12.         | [] -> None, []
  13.         | xs -> xs |> List.head |> Some, xs |> List.tail
  14.  
  15.     let create () = { incomingTasks = Ch () }
  16.  
  17.     let start timer =
  18.         let rec timerLoop (currentTask, nextTasks) =
  19.             Alt.choose (seq {
  20.                 match currentTask with
  21.                 | Some task ->
  22.                     yield task.cancel ^->. (nextTasks |> takeFirst)
  23.                     let waitTime = task.wakeTime - DateTime.Now
  24.                     yield
  25.                         (if TimeSpan.Compare (waitTime, TimeSpan.Zero) <= 0 then
  26.                             Alt.always ()
  27.                         else
  28.                             (timeOut waitTime))
  29.                         ^=>. (task.fire *<= () >>- (fun _ -> printfn "event fired") >>-. (nextTasks |> takeFirst))
  30.                 | None -> ()
  31.  
  32.                 yield Ch.take timer.incomingTasks ^=> (fun (wakeTime, cancel, res) ->
  33.                     let fire = IVar ()
  34.                     let newTask = { wakeTime = wakeTime; cancel = cancel; fire = fire }
  35.                     res *<= (fire |> IVar.read) >>- fun _ ->
  36.                         currentTask |> Option.fold (fun l x -> x::l) (newTask::nextTasks)
  37.                         |> List.sortBy (fun task -> task.wakeTime)
  38.                         |> takeFirst)
  39.             })
  40.             >>= timerLoop
  41.         timerLoop (None, []) >>- (fun _ -> printfn "TIMER STARTED")
  42.  
  43.     let submit wakeTime cancel timer =
  44.         let res = IVar ()
  45.         timer.incomingTasks *<+ (wakeTime, cancel, res)
  46.         >>=. res
  47.  
  48.  
  49.  
  50. [<EntryPoint>]
  51. let main argv =
  52.    
  53.     // let stop = IVar<string> ()
  54.     // // let x = printDelay |> until stop 10 |> startIgnore
  55.     // let promiseJob = printDelay |> until stop 0 |> Promise.start >>= Promise.read >>- (fun r -> Console.WriteLine (r)) |> startIgnore
  56.     // // let r = promiseJob |> startIgnore
  57.     // Console.ReadLine () |> ignore
  58.     // stop *<= "asdf" |> run
  59.     // Console.ReadLine () |> ignore
  60.    
  61.     printfn "Start: %A" DateTime.Now
  62.     let timer = Timer.create ()
  63.     timer |> Timer.start |> startIgnore
  64.     let wakeTime = DateTime.Now + (TimeSpan.FromSeconds 1.)
  65.     timer |> Timer.submit wakeTime (Alt.never ()) >>= id >>- (fun _ -> printfn "EVENT: %A" DateTime.Now) |> startIgnore
  66.     Console.ReadLine () |> ignore
  67.  
  68.  
  69.     0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement