|
| 1 | +[](https://travis-ci.org/elixirscript/processes) |
| 2 | + |
1 | 3 | Experiment to reproduce Erlang style processes in browser. The api follows the one from Erlang. All are found on the `ProcessSystem` class |
2 | 4 |
|
3 | 5 | #### Usage |
4 | 6 |
|
5 | | -* First, import the ProcessSystem create a new instance of one. |
| 7 | +- First, import the ProcessSystem create a new instance of one. |
| 8 | + |
6 | 9 | ```javascript |
7 | | - const Processes = require("erlang-processes"); |
8 | | - let system = new Processes.default.ProcessSystem(); |
| 10 | + const Processes = require('erlang-processes') |
| 11 | + let system = new Processes.default.ProcessSystem() |
9 | 12 | ``` |
10 | 13 |
|
11 | | -* Now you can spawn processes using the system. |
12 | | - |
13 | | - A process will switch to other processes when yield is used and will run until it completes. |
14 | | - |
15 | | - ```javascript |
16 | | - var pid1 = system.spawn(function*(){ |
17 | | - while(true){ |
18 | | - |
19 | | - yield system.receive(function(value){ |
20 | | - return console.log(value); |
21 | | - }); |
22 | | - |
23 | | - system.send(pid2, "message from 1"); |
24 | | - } |
25 | | - }); |
| 14 | +- Now you can spawn processes using the system. |
26 | 15 |
|
27 | | - system.register("Sally", pid1); |
| 16 | + A process will switch to other processes when yield is used and will run until it completes. |
28 | 17 |
|
29 | | - var pid2 = system.spawn(function*(){ |
30 | | - while(true){ |
31 | | - system.send("Sally", "message from 2"); |
32 | | - |
33 | | - yield system.receive(function(value){ |
34 | | - return console.log(value); |
35 | | - }); |
36 | | - } |
37 | | - }); |
38 | | - |
39 | | - ``` |
| 18 | + ```javascript |
| 19 | + var pid1 = system.spawn(function*() { |
| 20 | + while (true) { |
| 21 | + yield system.receive(function(value) { |
| 22 | + return console.log(value) |
| 23 | + }) |
| 24 | + |
| 25 | + system.send(pid2, 'message from 1') |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + system.register('Sally', pid1) |
| 30 | + |
| 31 | + var pid2 = system.spawn(function*() { |
| 32 | + while (true) { |
| 33 | + system.send('Sally', 'message from 2') |
| 34 | + |
| 35 | + yield system.receive(function(value) { |
| 36 | + return console.log(value) |
| 37 | + }) |
| 38 | + } |
| 39 | + }) |
| 40 | + ``` |
40 | 41 |
|
41 | 42 | ### API |
42 | 43 |
|
43 | | -* ProcessSystem |
44 | | - * `spawn(fun*) : pid` - Starts a process represented by the given generator function |
45 | | - * `spawn(module, fun, args) : pid` - Starts a process using the generator function from the specified module |
46 | | - * `link(pid) : void` - links the current process with the process from the given pid |
47 | | - * `unlink(pid) : void` - unlinks the current process from the process from the given pid |
48 | | - * `register(name, pid) : void` - registers the given name to the pid |
49 | | - * `whereis(name) : pid` - returns the pid registered by the given name or null if not registered |
50 | | - * `unregister(pid) : void` - unregisters the names associated with the pid |
51 | | - * `registered() : Array` - returns the liast of names that are registered |
52 | | - * `pid()` : pid` - returns the current process's pid |
53 | | - * `pidof(obj) : pid` - takes the input and tries to find the pid. Input can be a `pid`, `Process`, or name the pid is associated with |
54 | | - * `send(pid, msg) : msg` - sends a message the the process represented by the pid |
55 | | - * `receive(fun, timeout = 0, timeoutFn = () => true)` - Tells the current process to receive a message that the function can handle. If no match then the process is put in the suspended state until a message arrives or the timeout is reached. If the timeout is reached and no msg matches, then the timeoutFn is called |
56 | | - * `sleep(duration)` - puts the current process to sleep |
57 | | - * `exit(reason)` - terminates the current process with the given reason. |
58 | | - * `exit(pid, reason)` - tells the process with the pid to exit with the given reason |
59 | | - * `error(reason)` - terminates the current process with an error |
60 | | - * `process_flag(pid, flag, value)` - Sets flags on the given process. |
61 | | - * `process_flag(flag, value)` - Sets flags on the current process. |
62 | | - * Note: the only flag respected is the `Symbol.for("trap_exit")` flag. If value is `true`, then exit signals from linked processes are turned into messages and sent to the current processes mailbox. If value is `false`, the exit is treated as normal and terminates the process. Setting it to `true` is useful for supervising processes. |
63 | | - * `put(key, value)` - Adds a value to the current process's dictionary |
64 | | - * `get(key, default_value = null)` - Gets a value from the current process's dictionary or the default if key not in dictionary |
65 | | - * `get_process_dict()` - Gets the current process's dictionary |
66 | | - * `get_keys()` - Gets all the keys from the current process's dictionary |
67 | | - * `get_keys(value)` - Gets all the keys from the current process's dictionary with the given value |
68 | | - * `erase(key)` - Removes the key and the associated value from the current process's dictionary |
69 | | - * `erase()` - Removes all entries from the current process's dictionary |
70 | | - * `is_alive(pid)` - Returns if the given pid is alive |
71 | | - * `make_ref()` - Returns a unique reference |
72 | | - * `list()` - Returns a list of all the pids |
73 | | - * `monitor(pid)` - Monitors the given process |
74 | | - * `demonitor(ref)` - Removes the monitor |
75 | | - |
76 | | -* `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. |
| 44 | +- ProcessSystem |
| 45 | + |
| 46 | + - `spawn(fun*) : pid` - Starts a process represented by the given generator function |
| 47 | + - `spawn(module, fun, args) : pid` - Starts a process using the generator function from the specified module |
| 48 | + - `link(pid) : void` - links the current process with the process from the given pid |
| 49 | + - `unlink(pid) : void` - unlinks the current process from the process from the given pid |
| 50 | + - `register(name, pid) : void` - registers the given name to the pid |
| 51 | + - `whereis(name) : pid` - returns the pid registered by the given name or null if not registered |
| 52 | + - `unregister(pid) : void` - unregisters the names associated with the pid |
| 53 | + - `registered() : Array` - returns the liast of names that are registered |
| 54 | + - `pid()` : pid` - returns the current process's pid |
| 55 | + - `pidof(obj) : pid` - takes the input and tries to find the pid. Input can be a `pid`, `Process`, or name the pid is associated with |
| 56 | + - `send(pid, msg) : msg` - sends a message the the process represented by the pid |
| 57 | + - `receive(fun, timeout = 0, timeoutFn = () => true)` - Tells the current process to receive a message that the function can handle. If no match then the process is put in the suspended state until a message arrives or the timeout is reached. If the timeout is reached and no msg matches, then the timeoutFn is called |
| 58 | + - `sleep(duration)` - puts the current process to sleep |
| 59 | + - `exit(reason)` - terminates the current process with the given reason. |
| 60 | + - `exit(pid, reason)` - tells the process with the pid to exit with the given reason |
| 61 | + - `error(reason)` - terminates the current process with an error |
| 62 | + - `process_flag(pid, flag, value)` - Sets flags on the given process. |
| 63 | + - `process_flag(flag, value)` - Sets flags on the current process. |
| 64 | + - Note: the only flag respected is the `Symbol.for("trap_exit")` flag. If value is `true`, then exit signals from linked processes are turned into messages and sent to the current processes mailbox. If value is `false`, the exit is treated as normal and terminates the process. Setting it to `true` is useful for supervising processes. |
| 65 | + - `put(key, value)` - Adds a value to the current process's dictionary |
| 66 | + - `get(key, default_value = null)` - Gets a value from the current process's dictionary or the default if key not in dictionary |
| 67 | + - `get_process_dict()` - Gets the current process's dictionary |
| 68 | + - `get_keys()` - Gets all the keys from the current process's dictionary |
| 69 | + - `get_keys(value)` - Gets all the keys from the current process's dictionary with the given value |
| 70 | + - `erase(key)` - Removes the key and the associated value from the current process's dictionary |
| 71 | + - `erase()` - Removes all entries from the current process's dictionary |
| 72 | + - `is_alive(pid)` - Returns if the given pid is alive |
| 73 | + - `make_ref()` - Returns a unique reference |
| 74 | + - `list()` - Returns a list of all the pids |
| 75 | + - `monitor(pid)` - Monitors the given process |
| 76 | + - `demonitor(ref)` - Removes the monitor |
| 77 | + |
| 78 | +- `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. |
77 | 79 |
|
78 | 80 | ## References |
79 | 81 |
|
80 | | -* [Er.js](https://github.com/orph/erjs) |
81 | | -* [Erlang Processes](http://erlang.org/doc/reference_manual/processes.html) |
82 | | -* [ES6 Generators Deliver Go Style Concurrency](http://swannodette.github.io/2013/08/24/es6-generators-and-csp) |
83 | | -* [Red and Green Callbacks](http://joearms.github.io/2013/04/02/Red-and-Green-Callbacks.html) |
84 | | - |
| 82 | +- [Er.js](https://github.com/orph/erjs) |
| 83 | +- [Erlang Processes](http://erlang.org/doc/reference_manual/processes.html) |
| 84 | +- [ES6 Generators Deliver Go Style Concurrency](http://swannodette.github.io/2013/08/24/es6-generators-and-csp) |
| 85 | +- [Red and Green Callbacks](http://joearms.github.io/2013/04/02/Red-and-Green-Callbacks.html) |
0 commit comments