erlang-processes
v2.2.2
Published
Erlang style processes in JavaScript
Downloads
6
Readme
Experiment to reproduce Erlang style processes in browser. The api follows the one from Erlang. All are found on the ProcessSystem
class
Documentation
https://elixirscript.github.io/processes/
Demo
https://elixirscript.github.io/processes/demo/
Usage
First, import the ProcessSystem create a new instance of one.
const Processes = require('erlang-processes') let system = new Processes.default.ProcessSystem()
Now you can spawn processes using the system.
A process will switch to other processes when yield is used and will run until it completes.
var pid1 = system.spawn(function*() { while (true) { yield system.receive(function(value) { return console.log(value) }) system.send(pid2, 'message from 1') } }) system.register('Sally', pid1) var pid2 = system.spawn(function*() { while (true) { system.send('Sally', 'message from 2') yield system.receive(function(value) { return console.log(value) }) } })
API
ProcessSystem
spawn(fun*) : pid
- Starts a process represented by the given generator functionspawn(module, fun, args) : pid
- Starts a process using the generator function from the specified modulelink(pid) : void
- links the current process with the process from the given pidunlink(pid) : void
- unlinks the current process from the process from the given pidregister(name, pid) : void
- registers the given name to the pidwhereis(name) : pid
- returns the pid registered by the given name or null if not registeredunregister(pid) : void
- unregisters the names associated with the pidregistered() : Array
- returns the liast of names that are registeredpid()
: pid` - returns the current process's pidpidof(obj) : pid
- takes the input and tries to find the pid. Input can be apid
,Process
, or name the pid is associated withsend(pid, msg) : msg
- sends a message the the process represented by the pidreceive(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 calledsleep(duration)
- puts the current process to sleepexit(reason)
- terminates the current process with the given reason.exit(pid, reason)
- tells the process with the pid to exit with the given reasonerror(reason)
- terminates the current process with an errorprocess_flag(pid, flag, value)
- Sets flags on the given process.process_flag(flag, value)
- Sets flags on the current process.- Note: the only flag respected is the
Symbol.for("trap_exit")
flag. If value istrue
, then exit signals from linked processes are turned into messages and sent to the current processes mailbox. If value isfalse
, the exit is treated as normal and terminates the process. Setting it totrue
is useful for supervising processes.
- Note: the only flag respected is the
put(key, value)
- Adds a value to the current process's dictionaryget(key, default_value = null)
- Gets a value from the current process's dictionary or the default if key not in dictionaryget_process_dict()
- Gets the current process's dictionaryget_keys()
- Gets all the keys from the current process's dictionaryget_keys(value)
- Gets all the keys from the current process's dictionary with the given valueerase(key)
- Removes the key and the associated value from the current process's dictionaryerase()
- Removes all entries from the current process's dictionaryis_alive(pid)
- Returns if the given pid is alivemake_ref()
- Returns a unique referencelist()
- Returns a list of all the pidsmonitor(pid)
- Monitors the given processdemonitor(ref)
- Removes the monitor
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.