diff tests/spawn.ur @ 694:7ea0df9e56b6

spawn
author Adam Chlipala <adamc@hcoop.net>
date Sat, 04 Apr 2009 14:55:36 -0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/spawn.ur	Sat Apr 04 14:55:36 2009 -0400
@@ -0,0 +1,24 @@
+table t : {A : int, Ch : channel string}
+
+fun listener n ch =
+    s <- recv ch;
+    alert (show n ^ ": " ^ s);
+    listener n ch
+
+fun speak id msg =
+    r <- oneRow (SELECT t.Ch FROM t WHERE t.A = {[id]});
+    send r.T.Ch msg
+
+fun main () : transaction page =
+    ch1 <- channel;
+    dml (INSERT INTO t (A, Ch) VALUES (1, {[ch1]}));
+    ch2 <- channel;
+    dml (INSERT INTO t (A, Ch) VALUES (2, {[ch2]}));
+
+    s1 <- source "";
+    s2 <- source "";
+
+    return <xml><body onload={spawn (listener 1 ch1); spawn (listener 2 ch2)}>
+      1: <ctextbox source={s1}/><button onclick={msg <- get s1; speak 1 msg}/><br/>
+      2: <ctextbox source={s2}/><button onclick={msg <- get s2; speak 2 msg}/>
+    </body></xml>