Skip to content

Commit c534542

Browse files
committed
Renamed scheduler to process_system
1 parent 7d6ef1c commit c534542

File tree

8 files changed

+79
-79
lines changed

8 files changed

+79
-79
lines changed

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Experiment to reproduce erlang style processes in browser. The api follows the one from Erlang. All are found on the `Scheduler` class
1+
Experiment to reproduce erlang style processes in browser. The api follows the one from Erlang. All are found on the `ProcessSystem` class
22

33

44
### Running Examples
@@ -13,35 +13,35 @@ to each other.
1313

1414
#### Usage
1515

16-
* First, import the Scheduler create a new instance of one.
16+
* First, import the ProcessSystem create a new instance of one.
1717
```javascript
18-
import { Scheduler } from "processes";
19-
let scheduler = new Scheduler();
18+
import { ProcessSystem } from "processes";
19+
let system = new ProcessSystem();
2020
```
2121

22-
* Now you can spawn processes using the scheduler.
22+
* Now you can spawn processes using the system.
2323

2424
A process will switch to other processes when yield is used and will run until it completes.
2525

2626
```javascript
27-
var pid1 = scheduler.spawn(function*(){
27+
var pid1 = system.spawn(function*(){
2828
while(true){
2929

30-
yield scheduler.receive(function(value){
30+
yield system.receive(function(value){
3131
return console.log(value);
3232
});
3333

34-
scheduler.send(pid2, "message from 1");
34+
system.send(pid2, "message from 1");
3535
}
3636
});
3737

38-
scheduler.register("Sally", pid1);
38+
system.register("Sally", pid1);
3939

40-
var pid2 = scheduler.spawn(function*(){
40+
var pid2 = system.spawn(function*(){
4141
while(true){
42-
scheduler.send("Sally", "message from 2");
42+
system.send("Sally", "message from 2");
4343

44-
yield scheduler.receive(function(value){
44+
yield system.receive(function(value){
4545
return console.log(value);
4646
});
4747
}
@@ -51,7 +51,7 @@ to each other.
5151

5252
### API
5353

54-
* Scheduler
54+
* ProcessSystem
5555
* `spawn(fun*) : pid` - Starts a process represented by the given generator function
5656
* `spawn(module, fun, args) : pid` - Starts a process using the generator function from the specified module
5757
* `link(pid) : void` - links the current process with the process from the given pid
@@ -76,7 +76,7 @@ to each other.
7676
* `erase(key)` - Removes the key and the associated value from the current process`s dictionary
7777
* `erase()` - Removes all entries from the current process's dictionary
7878

79-
* `Scheduler.run(fun, args, context = null)` - A static generator function used to wrap a normal function or generator. If fun is a function, it returns the value, if it's a generator, then it delegates yielding to the generator.
79+
* `ProcessSystem.run(fun, args, context = null)` - A static generator function used to wrap a normal function or generator. If fun is a function, it returns the value, if it's a generator, then it delegates yielding to the generator.
8080

8181
* GenServer
8282
* `start(module, args)` - Starts a GenServer with the given module and args
@@ -94,8 +94,8 @@ to each other.
9494
An example of a Stack using a GenServer
9595

9696
```javascript
97-
import { Scheduler, GenServer } from "processes";
98-
self.scheduler = self.scheduler || new Scheduler();
97+
import { ProcessSystem, GenServer } from "processes";
98+
self.system = self.system || new ProcessSystem();
9999

100100
const Stack = {
101101
init: function(args){
@@ -112,16 +112,16 @@ const Stack = {
112112
}
113113
}
114114

115-
self.scheduler.spawn(function*(){
116-
const [ok, pid] = yield* Scheduler.run(GenServer.start, [Stack, ["hello"]]);
115+
self.system.spawn(function*(){
116+
const [ok, pid] = yield* ProcessSystem.run(GenServer.start, [Stack, ["hello"]]);
117117

118-
let a = yield* Scheduler.run(GenServer.call, [pid, "pop"]);
118+
let a = yield* ProcessSystem.run(GenServer.call, [pid, "pop"]);
119119
console.log(a); // "hello"
120120

121-
let b = yield* Scheduler.run(GenServer.cast, [pid, ["push", "world"]]);
121+
let b = yield* ProcessSystem.run(GenServer.cast, [pid, ["push", "world"]]);
122122
console.log(b); // Symbol.for("ok")
123123

124-
let c = yield* Scheduler.run(GenServer.call, [pid, "pop"]);
124+
let c = yield* ProcessSystem.run(GenServer.call, [pid, "pop"]);
125125
console.log(c); // "world"
126126
});
127127
```

examples/gen_server.js

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

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

66
const Stack = {
77
init: function(args){
@@ -18,15 +18,15 @@ const Stack = {
1818
}
1919
}
2020

21-
self.scheduler.spawn(function*(){
22-
const [ok, pid] = yield* Scheduler.run(GenServer.start, [Stack, ["hello"]]);
21+
self.system.spawn(function*(){
22+
const [ok, pid] = yield* ProcessSystem.run(GenServer.start, [Stack, ["hello"]]);
2323

24-
let a = yield* Scheduler.run(GenServer.call, [pid, "pop"]);
24+
let a = yield* ProcessSystem.run(GenServer.call, [pid, "pop"]);
2525
console.log(a);
2626

27-
let b = yield* Scheduler.run(GenServer.cast, [pid, ["push", "world"]]);
27+
let b = yield* ProcessSystem.run(GenServer.cast, [pid, ["push", "world"]]);
2828
console.log(b);
2929

30-
let c = yield* Scheduler.run(GenServer.call, [pid, "pop"]);
30+
let c = yield* ProcessSystem.run(GenServer.call, [pid, "pop"]);
3131
console.log(c);
3232
});

examples/send_receive.js

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

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

66

7-
var pid1 = scheduler.spawn(function*(){
7+
var pid1 = system.spawn(function*(){
88
while(true){
99

10-
yield scheduler.receive(function(value){
10+
yield system.receive(function(value){
1111
return console.log(value);
1212
});
1313

14-
scheduler.send(pid2, "message from 1");
14+
system.send(pid2, "message from 1");
1515
}
1616
});
1717

18-
scheduler.register("Sally", pid1);
18+
system.register("Sally", pid1);
1919

2020

21-
var pid2 = scheduler.spawn(function*(){
21+
var pid2 = system.spawn(function*(){
2222
while(true){
2323

24-
scheduler.send("Sally", "message from 2");
24+
system.send("Sally", "message from 2");
2525

26-
yield scheduler.receive(function(value){
26+
yield system.receive(function(value){
2727
return console.log(value);
2828
});
2929
}

src/processes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export { default as Scheduler } from "./processes/scheduler";
1+
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";

src/processes/otp/gen_event.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
function start(options = []){
2-
return [Symbol.for("ok"), self.scheduler.spawn(start_process())];
2+
return [Symbol.for("ok"), self.system.spawn(start_process())];
33
}
44

55
function start_link(options = []){
6-
return [Symbol.for("ok"), self.scheduler.spawn_link(start_process())];
6+
return [Symbol.for("ok"), self.system.spawn_link(start_process())];
77
}
88

99
function start_process(){
1010
return function*(){
1111
while(true){
12-
yield self.scheduler.receive(function(args){
12+
yield self.system.receive(function(args){
1313
switch(args[0]){
1414
case "add_handler":
1515
break;

src/processes/otp/gen_server.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
function start(module, args){
2-
return [Symbol.for("ok"), self.scheduler.spawn(start_process(module, args))];
2+
return [Symbol.for("ok"), self.system.spawn(start_process(module, args))];
33
}
44

55
function start_link(module, args){
6-
return [Symbol.for("ok"), self.scheduler.spawn_link(start_process(module, args))];
6+
return [Symbol.for("ok"), self.system.spawn_link(start_process(module, args))];
77
}
88

99
function start_process(module, args){
1010
return function*(){
1111
let [ok, state] = module.init.apply(null, [args]);
12-
yield self.scheduler.put("state", state);
12+
yield self.system.put("state", state);
1313

1414
try{
1515
while(true){
16-
yield self.scheduler.receive(function(args){
16+
yield self.system.receive(function(args){
1717
let command = args[0];
1818

1919
switch(command){
2020
case "call":
2121
var request = args[1];
2222
var sender = args[2];
2323

24-
var [reply, response, new_state] = module.handle_call(request, sender, self.scheduler.get("state"));
25-
self.scheduler.put("state", new_state);
24+
var [reply, response, new_state] = module.handle_call(request, sender, self.system.get("state"));
25+
self.system.put("state", new_state);
2626

27-
self.scheduler.send(sender, response);
27+
self.system.send(sender, response);
2828
break;
2929

3030
case "cast":
3131
var request = args[1];
3232
var sender = args[2];
3333

34-
var [reply, new_state] = module.handle_cast(request, self.scheduler.get("state"));
34+
var [reply, new_state] = module.handle_cast(request, self.system.get("state"));
3535

36-
self.scheduler.put("state", new_state);
37-
self.scheduler.send(args[2], Symbol.for("ok"));
36+
self.system.put("state", new_state);
37+
self.system.send(args[2], Symbol.for("ok"));
3838

3939
break;
4040

@@ -52,23 +52,23 @@ function start_process(module, args){
5252
}
5353

5454
function* call(server, request){
55-
self.scheduler.send(server, ["call", request, self.scheduler.pid()]);
55+
self.system.send(server, ["call", request, self.system.pid()]);
5656

57-
return yield self.scheduler.receive(function(args){
57+
return yield self.system.receive(function(args){
5858
return args;
5959
});
6060
}
6161

6262
function* cast(server, request){
63-
self.scheduler.send(server, ["cast", request, self.scheduler.pid()]);
63+
self.system.send(server, ["cast", request, self.system.pid()]);
6464

65-
return yield self.scheduler.receive(function(args){
65+
return yield self.system.receive(function(args){
6666
return args;
6767
});
6868
}
6969

70-
function stop(server, request){
71-
self.scheduler.send(server, ["stop"]);
70+
function stop(server){
71+
self.system.send(server, ["stop"]);
7272
}
7373

7474
export default { start, start_link, call, cast, stop };

src/processes/process.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* @flow */
44
import Mailbox from "./mailbox";
5-
import Scheduler from "./scheduler";
5+
import ProcessSystem from "./process_system";
66
import States from "./states";
77

88
const NOMSG = Symbol();
@@ -24,17 +24,17 @@ class Process {
2424
mailbox: Mailbox;
2525
func: Function;
2626
args: Array;
27-
scheduler: Scheduler;
27+
system: ProcessSystem;
2828
status: Symbol;
2929
dict: Object;
3030
flags: Object;
3131

32-
constructor(pid: Number, func: Function, args: Array, mailbox: Mailbox, scheduler: Scheduler){
32+
constructor(pid: Number, func: Function, args: Array, mailbox: Mailbox, system: ProcessSystem){
3333
this.pid = pid;
3434
this.func = func;
3535
this.args = args;
3636
this.mailbox = mailbox;
37-
this.scheduler = scheduler;
37+
this.system = system;
3838
this.status = States.STOPPED;
3939
this.dict = {};
4040
this.flags = {};
@@ -44,8 +44,8 @@ class Process {
4444
const function_scope = this;
4545
let machine = this.main();
4646

47-
this.scheduler.queue(function() {
48-
function_scope.scheduler.set_current(function_scope.pid);
47+
this.system.queue(function() {
48+
function_scope.system.set_current(function_scope.pid);
4949
function_scope.run(machine, machine.next());
5050
}, this.pid);
5151
}
@@ -60,7 +60,7 @@ class Process {
6060
retval = e;
6161
}
6262

63-
this.scheduler.exit(retval);
63+
this.system.exit(retval);
6464
}
6565

6666
process_flag(flag, value){
@@ -76,7 +76,7 @@ class Process {
7676
console.error(reason);
7777
}
7878

79-
this.scheduler.remove_proc(this.pid, reason);
79+
this.system.remove_proc(this.pid, reason);
8080
}
8181

8282
receive(fun){
@@ -103,17 +103,17 @@ class Process {
103103

104104
if(is_sleep(value)){
105105

106-
this.scheduler.delay(function() {
107-
function_scope.scheduler.set_current(function_scope.pid);
106+
this.system.delay(function() {
107+
function_scope.system.set_current(function_scope.pid);
108108
function_scope.run(machine, machine.next());
109109
}, value[1]);
110110

111111
}else if(is_receive(value) && receive_timed_out(value)){
112112

113113
let result = value[3]();
114114

115-
this.scheduler.queue(function() {
116-
function_scope.scheduler.set_current(function_scope.pid);
115+
this.system.queue(function() {
116+
function_scope.system.set_current(function_scope.pid);
117117
function_scope.run(machine, machine.next(result));
118118
});
119119

@@ -122,20 +122,20 @@ class Process {
122122
let result = function_scope.receive(value[1]);
123123

124124
if(result === NOMSG){
125-
this.scheduler.suspend(function() {
126-
function_scope.scheduler.set_current(function_scope.pid);
125+
this.system.suspend(function() {
126+
function_scope.system.set_current(function_scope.pid);
127127
function_scope.run(machine, step);
128128
});
129129
}else{
130-
this.scheduler.queue(function() {
131-
function_scope.scheduler.set_current(function_scope.pid);
130+
this.system.queue(function() {
131+
function_scope.system.set_current(function_scope.pid);
132132
function_scope.run(machine, machine.next(result));
133133
});
134134
}
135135

136136
}else{
137-
this.scheduler.queue(function() {
138-
function_scope.scheduler.set_current(function_scope.pid);
137+
this.system.queue(function() {
138+
function_scope.system.set_current(function_scope.pid);
139139
function_scope.run(machine, machine.next(value));
140140
});
141141
}

0 commit comments

Comments
 (0)