view clock.ur @ 14:0827320b0f04

Write calendarCtl in terms of a source with a listener.
author Karn Kallio <kkallio@eka>
date Fri, 05 Aug 2011 18:55:24 -0430
parents ccd0a169e827
children 2947170fcfd6
line wrap: on
line source
(** Reactive view of the current time *)

type t = {
     Source : source time, (* Server time as of last tick event *)
     Skew : source int,    (* How many seconds do we add to the local clock to match the server clock? *)
     Period : int          (* How many milliseconds between ticks? *)
}

fun create ms =
    tm <- now;
    t <- source tm;
    sk <- source 0;
    return {Source = t, Skew = sk, Period = ms}

fun start t =
    let
        fun loop () =
            sleep t.Period;
            tm <- now;
            sk <- get t.Skew;
            set t.Source (addSeconds tm sk);
            loop ()

        fun serverTime () = now
    in
        spawn (server <- rpc (serverTime ());
               local <- now;
               set t.Skew (diffInSeconds server local));
        spawn (loop ())
    end

fun signal t = Basis.signal t.Source