annotate clock.ur @ 11:ccd0a169e827

Clock corrects for skew, so that it matches server time (if an RPC round-trips fast enough)
author Adam Chlipala <adam@chlipala.net>
date Sun, 24 Jul 2011 14:51:16 -0400
parents 0337f88f2efc
children 2947170fcfd6
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@11 28 set t.Skew (diffInSeconds server local));
adam@10 29 spawn (loop ())
adam@10 30 end
adam@10 31
adam@10 32 fun signal t = Basis.signal t.Source