-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.html
194 lines (138 loc) · 4.38 KB
/
test.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<html>
<head>
<title>Test erjs.js</title>
<script src="er.js" type="application/javascript"></script>
</head>
<body>
Starting Er.js Concurrency Tests...
<script type="application/javascript">
function log(div, txt) { div.innerHTML += " " + txt; };
// Send a { Ping: <pid> } to id (pid or registered name)
// Waits for a { Pong: _ } before quitting.
function *ping_me(id) {
var div = document.createElement("div");
document.body.appendChild(div);
log(div, "Ping(" + Er.pid() + "): ");
if (!id) {
log(div, "Invalid id '" + id + "'. Quitting.");
Er.exit("Invalid id '" + id + "'.");
}
log(div, "Sending to '" + id + "'.");
Er.send(id, { Ping: Er.pid() });
log(div, "Awaiting Pong...");
yield Er.receive({ Pong: _ },
function(msg) {
log(div, "Pong from (" + msg.Pong + ").");
return;
});
log(div, "Quiting!");
};
// Register as "Foo", spawn a bunch of ping_mes, and log incoming
// messages (forever).
var foo = Er.spawn(function *() {
Er.register("Foo", Er.pid());
var div = document.createElement("div");
document.body.appendChild(div);
Er.spawn(ping_me, "Foo");
Er.spawn(ping_me, Er.pid());
while (true) {
log(div, "<br>Receiving(" + Er.pid() + ")... ");
yield Er.receive(
{ Hello: _, From: _ },
function(msg) {
log(div, "Hello=" + msg.Hello + ". From: " +
msg.From + ".");
Er.spawn(ping_me, Er.pid());
},
{ SmallTalk: _ },
function(msg) {
log(div, "SmallTalk=" + msg.SmallTalk);
},
{ Goodbye: _, Digits: String },
function(msg) {
log(div, "Goodbye. Digits=" + msg.Digits);
},
{ Goodbye: _ },
function(msg) {
log(div, "Goodbye. No phone number :-(");
},
{ Ping: _ },
function(msg) {
log(div, "Ping from (" + msg.Ping + ").");
Er.send(msg.Ping, { Pong: Er.pid() });
},
{ Signal: Er.Exit, From: _, Reason: _ },
function(msg) {
log(div, "Link exit. From=" + msg.From + ".\n" +
"Reason: " + msg.Reason);
});
}
log(div, "Quitting!");
});
// Send spaced out messages to foo, and exit abnormally
Er.spawn(function *() {
yield Er.sleep(1000);
Er.send(foo, { Hello: new Date(), From: Er.pid() });
yield Er.sleep(2000);
Er.send(foo, { SmallTalk: "How about that local sports team!" });
yield Er.sleep(2000);
Er.send(foo, { Goodbye: false });
Er.exit("Sorry, in a hurry!");
});
// Send messages to "Foo"/foo from the main process
Er.send("Foo", { Hello: new Date(), From: Er.pid() });
Er.send("Foo", { SmallTalk: "Nice weather today!" });
Er.send(foo, { Goodbye: true, Digits: "(415) 555-5555" });
// Listen for clicks anywhere on the page
Er.spawn(function *() {
var div = document.createElement("div");
document.body.appendChild(div);
log(div, "ClickMe(" + Er.pid() + "): Click anywhere on the page to test.");
while(true) {
ev = yield Er.DOM.receive(document, "click");
alert("Click handler got event: " + ev);
}
});
// Sleep forever
var sleeper = Er.spawn(function *() {
var div = document.createElement("div");
document.body.appendChild(div);
log(div, "Sleeper(" + Er.pid() + "): ");
while (true) {
log(div, "Zzz...");
yield Er.sleep(500);
}
});
// Linked process killer
var killer = Er.spawn(function *() {
Er.link(sleeper);
Er.link(foo);
yield Er.sleep(8000);
Er.exit("Bonsai!");
});
// Fetch index.html using synchronous Er.Ajax.
Er.spawn(function *() {
var div = document.createElement("div");
document.body.appendChild(div);
log(div, "Ajax(" + Er.pid() + "): Getting 'index.html'...");
var result = yield Er.Ajax.get("index.html");
log(div, "Done. Loading iframe...</br>");
var iframe = document.createElement("iframe");
iframe.width = 500;
div.appendChild(iframe);
var idoc = iframe.contentWindow.document;
idoc.open();
idoc.write(result);
idoc.close();
});
/* Future work... */
/*
Er.link_event("button1", "click");
yield Er.receive({ Signal: Er.Event, From: _, Event: "click" },
function (msg) {
alert("Got Click event from: " + msg.From);
});
*/
</script>
</body>
</html>