You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Experiment to reproduce Erlang style processes in browser. The api follows the one from Erlang. All are found on the `ProcessSystem` class
2
2
3
-
One example is an implementation of a GenServer. The other example is 2 processes talking
4
-
to each other.
5
-
6
3
#### Usage
7
4
8
5
* First, import the ProcessSystem create a new instance of one.
@@ -49,8 +46,9 @@ to each other.
49
46
* `link(pid) : void` - links the current process with the process from the given pid
50
47
* `unlink(pid) : void` - unlinks the current process from the process from the given pid
51
48
* `register(name, pid) : void` - registers the given name to the pid
52
-
* `registered(name) : pid` - returns the pid registered by the given name or null if not registered
49
+
* `whereis(name) : pid` - returns the pid registered by the given name or null if not registered
53
50
* `unregister(pid) : void` - unregisters the names associated with the pid
51
+
* `registered() : Array` - returns the liast of names that are registered
54
52
* `pid()` : pid` - returns the current process's pid
55
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
56
54
* `send(pid, msg) : msg` - sends a message the the process represented by the pid
@@ -59,61 +57,18 @@ to each other.
59
57
* `exit(reason)` - terminates the current process with the given reason.
60
58
* `exit(pid, reason)` - tells the process with the pid to exit with the given reason
61
59
* `error(reason)` - terminates the current process with an error
60
+
* `process_flag(pid, flag, value)` - Sets flags on the given process.
62
61
* `process_flag(flag, value)` - Sets flags on the current process.
63
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.
64
63
* `put(key, value)` - Adds a value to the current process's dictionary
65
-
* `get(key)` - Gets a value from the current process's dictionary
66
-
* `get()` - Gets 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
67
66
* `get_keys()` - Gets all the keys from the current process's dictionary
68
-
* `erase(key)` - Removes the key and the associated value 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
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
70
73
71
74
* `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.
72
-
73
-
* GenServer
74
-
* `start(module, args)` - Starts a GenServer with the given module and args
75
-
* `start_link(module, args)` - Starts a GenServer with the given module and args
76
-
* `call* (server, action)` - Sends the GenServer a action and waits for it to respond with a value.
77
-
* `cast* (server, action)` - Sends the GenServer a action to update a value.
78
-
* `stop (server)` - Stops the GenServer.
79
-
* **Note**: Genserver expects a module the has the following functions:
80
-
* `init(args)` - Must return an array containing a symbol and the initial state
81
-
* `handle_call(action, from, state)` - Called when `GenServer.call` is called. This function is given the action, the pid of the calling process, and the current state. Must return `[reply, return_value, new_state]` where reply is a symbol ,usually `Symbol.for("reply"), the value to return to the process, and lastly, the new state of the GenServer.
82
-
* `handle_cast(action, state)` - Called when `GenServer.cast` is called. his function is given the action, and the current state. Must return `[reply, return_value, new_state]` where reply is a symbol ,usually `Symbol.for("noreply")`, and lastly, the new state of the GenServer.
83
-
84
-
#### GenServer Example
85
-
86
-
An example of a Stack using a GenServer
87
-
88
-
```javascript
89
-
const Processes = require("erlang-processes");
90
-
self.system = self.system || new Processes.default.ProcessSystem();
0 commit comments