diff changePoller.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
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/changePoller.ur	Fri Aug 05 18:55:24 2011 -0430
@@ -0,0 +1,30 @@
+con changePoller t = {Val : source t,
+                      Last : source t}
+
+fun create [t ::: Type] (_ : eq t) (i : t)  =
+    v <- source i;
+    l <- source i;
+
+    return {Val = v,
+            Last = l}
+
+fun addChangeListener [t ::: Type] (_ : eq t) f pollInterval cp =
+    let
+        fun go () =
+            sleep pollInterval;
+            sample <- get cp.Val;
+            lst <- get cp.Last;
+            if sample = lst then
+                go ()
+            else
+                f sample;
+                set cp.Last sample;
+                go ()
+    in
+        go ()
+    end
+
+fun ctl [t ::: Type] cp = cp.Val
+
+fun value [t ::: Type] cp = signal (cp.Val)
+