annotate clock.ur @ 29:93140c5cc972

Clean up dependencies and examples; add Style module
author Adam Chlipala <adam@chlipala.net>
date Sat, 12 May 2012 10:03:44 -0400
parents 2947170fcfd6
children
rev   line source
adam@10 1 (** Reactive view of the current time *)
adam@10 2
adam@11 3 type t = {
adam@11 4 Source : source time, (* Server time as of last tick event *)
adam@11 5 Skew : source int, (* How many seconds do we add to the local clock to match the server clock? *)
adam@11 6 Period : int (* How many milliseconds between ticks? *)
adam@11 7 }
adam@10 8
adam@10 9 fun create ms =
adam@10 10 tm <- now;
adam@10 11 t <- source tm;
adam@11 12 sk <- source 0;
adam@11 13 return {Source = t, Skew = sk, Period = ms}
adam@10 14
adam@10 15 fun start t =
adam@10 16 let
adam@10 17 fun loop () =
adam@10 18 sleep t.Period;
adam@10 19 tm <- now;
adam@11 20 sk <- get t.Skew;
adam@11 21 set t.Source (addSeconds tm sk);
adam@10 22 loop ()
adam@11 23
adam@11 24 fun serverTime () = now
adam@10 25 in
adam@11 26 spawn (server <- rpc (serverTime ());
adam@11 27 local <- now;
adam@17 28 set t.Skew (diffInSeconds local server));
adam@10 29 spawn (loop ())
adam@10 30 end
adam@10 31
adam@10 32 fun signal t = Basis.signal t.Source