- Notifications
You must be signed in to change notification settings - Fork2
nucleartide/actor.js
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Elixir-style actors in JavaScript. Spawn "processes", then send and receive messages between them – just like you would in Elixir.
You can think of it asco
, but with message passing.
NOTE: Don't use this library. Currently, it doesn't use Web Workers or Node'schild_process
under the hood (and I lack the motivation to write a Web Workers /child_process
implementation), so your code isn't actually run in parallel. I mostly wrote this library as an experiment in Elixir-flavored API design.
You can probably find better Web Workers helper libs onnpm.
constpid=spawn(asyncfunction(){constmsg=awaitthis.receive(mail=>{if(mail==='hello')return'world'})console.log(msg)})pid.send('hello')
See theincluded examples for more use cases.
$ yarn add actorjs
Spawn a "process" that will execute the passed-in function.
constpid=spawn(asyncfunction(foo,bar){console.log("wee i'm in a process sorta")},'foo','bar')
Send a message to a PID.
pid.send(['ok','this is a message'])
Block until a received message matches the passed-inpattern
function.
Thepattern
function takes an arbitrarymessage
as input, and returns a result based on thatmessage
. By default, thepattern
function is theidentity function.
A result ofundefined
is not considered to be a match, and thusthis.receive()
will continue blocking.
constpid=spawn(asyncfunction(){letv=awaitthis.receive(msg=>{const[status,value]=msgif(status==='hello')returnvalueif(status==='world')return"won't match"})v=awaitthis.receive()})pid.send(['hello','yes this is dog'])pid.send('anything')
The return value ofspawn()
is a thenable.
spawn(asyncfunction(){// ...}).then(value=>{/* ... */}).catch(console.error)
MIT
About
Elixir-style actors in JavaScript