Skip to content

Commit 18bf813

Browse files
committed
Began adding an Application module
1 parent 20db88e commit 18bf813

File tree

7 files changed

+52
-31
lines changed

7 files changed

+52
-31
lines changed

examples/send_receive.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
"use strict";
22

3-
import { ProcessSystem, GenServer } from "../src/processes";
3+
import { ProcessSystem, GenServer, Application } from "../src/processes";
44
self.system = self.system || new ProcessSystem();
55

6+
let myApp = {
7+
start: function(type, args){
8+
let pid = self.system.spawn(function*(){
9+
var pid1 = self.system.spawn_link(function*(){
10+
while(true){
611

7-
var pid1 = system.spawn(function*(){
8-
while(true){
12+
yield self.system.receive(function(value){
13+
return console.log(value);
14+
});
915

10-
yield system.receive(function(value){
11-
return console.log(value);
16+
self.system.send(pid2, "message from 1");
17+
}
1218
});
1319

14-
system.send(pid2, "message from 1");
15-
}
16-
});
20+
self.system.register("Sally", pid1);
1721

18-
system.register("Sally", pid1);
1922

23+
var pid2 = self.system.spawn_link(function*(){
24+
while(true){
25+
26+
self.system.send("Sally", "message from 2");
2027

21-
var pid2 = system.spawn(function*(){
22-
while(true){
23-
24-
system.send("Sally", "message from 2");
28+
yield self.system.receive(function(value){
29+
return console.log(value);
30+
});
31+
}
32+
});
2533

26-
yield system.receive(function(value){
27-
return console.log(value);
34+
yield self.system.receive(function(value){
35+
return Symbol.for("no_match");
36+
});
2837
});
38+
39+
return [Symbol.for("ok"), pid];
2940
}
30-
});
41+
};
42+
43+
let pid = Application.start(myApp);

src/processes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { default as ProcessSystem } from "./processes/process_system";
22
export { default as GenServer } from "./processes/otp/gen_server";
33
export { default as GenEvent } from "./processes/otp/gen_event";
4+
export { default as Application } from "./processes/otp/application";

src/processes/otp/application.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function start(app, type = Symbol.for("temporary")){
2+
return app.start(type, []);
3+
}
4+
5+
export default { start };

src/processes/pid.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class PID {
1010
}
1111

1212
toString(){
13-
return "PID#<0." + id + ".0>";
13+
return "PID#<0." + this.id + ".0>";
1414
}
1515
}
1616

src/processes/process.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import Mailbox from "./mailbox";
55
import ProcessSystem from "./process_system";
66
import States from "./states";
77

8-
const NOMSG = Symbol();
9-
108
function is_sleep(value){
119
return Array.isArray(value) && value[0] === States.SLEEP;
1210
}
@@ -80,13 +78,16 @@ class Process {
8078
}
8179

8280
receive(fun){
83-
let value = NOMSG;
81+
let value = States.NOMATCH;
8482
let messages = this.mailbox.get();
8583

8684
for(let i = 0; i < messages.length; i++){
8785
try{
8886
value = fun(messages[i]);
89-
this.mailbox.removeAt(i);
87+
if(value !== States.NOMATCH){
88+
this.mailbox.removeAt(i);
89+
break;
90+
}
9091
}catch(e){
9192
this.exit(e);
9293
}
@@ -121,7 +122,7 @@ class Process {
121122

122123
let result = function_scope.receive(value[1]);
123124

124-
if(result === NOMSG){
125+
if(result === States.NOMATCH){
125126
this.system.suspend(function() {
126127
function_scope.system.set_current(function_scope.pid);
127128
function_scope.run(machine, step);

src/processes/process_system.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ class ProcessSystem {
2121
this.suspended = new Map();
2222

2323
let process_system_scope = this;
24-
this.main_process = this.spawn(function*(){
24+
this.main_process_pid = this.spawn(function*(){
2525
while(true){
2626
yield process_system_scope.sleep(10000);
2727
}
2828
});
29+
this.set_current(this.main_process_pid);
2930
}
3031

3132
static * run(fun, args, context = null){
@@ -65,13 +66,13 @@ class ProcessSystem {
6566
}
6667

6768
link(pid){
68-
this.links.get(this.current_process.pid).add(pid);
69-
this.links.get(pid).add(this.current_process.pid);
69+
this.links.get(this.pid()).add(pid);
70+
this.links.get(pid).add(this.pid());
7071
}
7172

7273
unlink(pid){
73-
this.links.get(this.current_process.pid).delete(pid);
74-
this.links.get(pid).delete(this.current_process.pid);
74+
this.links.get(this.pid()).delete(pid);
75+
this.links.get(pid).delete(this.pid());
7576
}
7677

7778
set_current(id){
@@ -104,9 +105,8 @@ class ProcessSystem {
104105
this.unregister(pid);
105106
this.scheduler.removePid(pid);
106107

107-
if(this.links.get(pid)){
108-
for (let linkpid in this.links.get(pid).entries()) {
109-
linkpid = Number(linkpid);
108+
if(this.links.has(pid)){
109+
for (let linkpid of this.links.get(pid)) {
110110
this.exit(linkpid, exitreason);
111111
this.links.get(linkpid).delete(pid);
112112
}

src/processes/states.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export default {
1010
SUSPENDED: Symbol.for("suspended"),
1111
STOPPED: Symbol.for("stopped"),
1212
SLEEP: Symbol.for("sleep"),
13-
EXIT: Symbol.for("exit")
13+
EXIT: Symbol.for("exit"),
14+
NOMATCH: Symbol.for("no_match")
1415
}

0 commit comments

Comments
 (0)