Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Nov 19, 2017. It is now read-only.
/actor.jsPublic archive

Elixir-style actors in JavaScript

NotificationsYou must be signed in to change notification settings

nucleartide/actor.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.


Example

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.

Install

$ yarn add actorjs

API

spawn(fn | asyncFn, ...args) => PID

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')

PID#send(msg)

Send a message to a PID.

pid.send(['ok','this is a message'])

this.receive(pattern) => Promise

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')

PID#then(value =>),PID#catch(err =>)

The return value ofspawn() is a thenable.

spawn(asyncfunction(){// ...}).then(value=>{/* ... */}).catch(console.error)

License

MIT


[8]ページ先頭

©2009-2025 Movatter.jp