Skip to content

Commit 0927bd3

Browse files
committed
Refactored example code into examples folder each with their own web page
1 parent 97db51c commit 0927bd3

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

examples/gen_server.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
</head>
6+
<body>
7+
Open Console
8+
9+
<script src="/jspm_packages/system.js"></script>
10+
<script src="/config.js"></script>
11+
<script>
12+
System.import('examples/gen_server.js');
13+
</script>
14+
</body>
15+
</html>

examples/gen_server.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"use strict";
2+
3+
import { Scheduler, GenServer } from "../src/processes.js";
4+
self.scheduler = self.scheduler || new Scheduler();
5+
6+
const Stack = {
7+
init: function(args){
8+
return [Symbol.for("ok"), args];
9+
},
10+
11+
handle_call: function(action, pid, state){
12+
return [Symbol.for("reply"), state[0], state.slice(1)];
13+
},
14+
15+
16+
handle_cast: function(action, state){
17+
return [Symbol.for("noreply"), [action[1]].concat(state)];
18+
}
19+
}
20+
21+
self.scheduler.spawn(function*(){
22+
const [ok, pid] = yield* Scheduler.run(GenServer.start, [Stack, ["hello"]]);
23+
24+
let a = yield* Scheduler.run(GenServer.call, [pid, "pop"]);
25+
console.log(a);
26+
27+
let b = yield* Scheduler.run(GenServer.cast, [pid, ["push", "world"]]);
28+
console.log(b);
29+
30+
let c = yield* Scheduler.run(GenServer.call, [pid, "pop"]);
31+
console.log(c);
32+
});

examples/send_receive.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title></title>
5+
</head>
6+
<body>
7+
Open Console
8+
9+
<script src="/jspm_packages/system.js"></script>
10+
<script src="/config.js"></script>
11+
<script>
12+
System.import('examples/send_receive.js');
13+
</script>
14+
</body>
15+
</html>

examples/send_receive.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
import { Scheduler, GenServer } from "../src/processes.js";
4+
self.scheduler = self.scheduler || new Scheduler();
5+
6+
7+
var pid1 = scheduler.spawn(function*(){
8+
while(true){
9+
10+
yield scheduler.receive(function(value){
11+
return console.log(value);
12+
});
13+
14+
scheduler.send(pid2, "message from 1");
15+
}
16+
});
17+
18+
scheduler.register("Sally", pid1);
19+
20+
21+
var pid2 = scheduler.spawn(function*(){
22+
while(true){
23+
24+
scheduler.send("Sally", "message from 2");
25+
26+
yield scheduler.receive(function(value){
27+
return console.log(value);
28+
});
29+
}
30+
});

0 commit comments

Comments
 (0)