-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathforked-actor.js
More file actions
33 lines (29 loc) · 862 Bytes
/
forked-actor.js
File metadata and controls
33 lines (29 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
var actors = require('comedy');
/**
* Actor definition class.
*/
class MyActor {
sayHello(to) {
// Reply with a message, containing self PID.
return `Hello to ${to} from ${process.pid}!`;
}
}
// Create an actor system.
var actorSystem = actors();
actorSystem
// Get a root actor reference.
.rootActor()
// Create a class-defined child actor, that is run in a separate process (forked mode).
.then(rootActor => rootActor.createChild(MyActor, { mode: 'forked' }))
// Send a message to our forked actor with a self process PID.
.then(myActor => myActor.sendAndReceive('sayHello', process.pid))
.then(reply => {
console.log(`Actor replied: ${reply}`);
})
// Log errors.
.catch(err => {
console.error(err);
})
// Destroy the system, killing all actor processes.
.finally(() => actorSystem.destroy());