Files: fc3efb6d1a8e5ef2e4cbd0017fa9649b221b8793 / actor.js
2649 bytesRaw
1 | module.exports = class Actor { |
2 | /** |
3 | * the Actor manages the varous message passing functions and provides |
4 | * an interface for the containers to use |
5 | * @param {Object} opts |
6 | * @param {Object} opts.id - the UUID of the Actor |
7 | * @param {Object} opts.state - the state of the container |
8 | * @param {Object} opts.hypervisor - the instance of the hypervisor |
9 | * @param {Object} opts.container - the container constuctor and argments |
10 | */ |
11 | constructor (opts) { |
12 | Object.assign(this, opts) |
13 | |
14 | this.inbox = [] |
15 | this.ticks = 0 |
16 | this.running = false |
17 | this.container = new this.Container(this) |
18 | } |
19 | |
20 | serializeMetaData () { |
21 | return Actor.serializeMetaData(this.type, this.nonce) |
22 | } |
23 | |
24 | getFuncRef (name) { |
25 | return { |
26 | name, |
27 | destId: this.id |
28 | } |
29 | } |
30 | |
31 | /** |
32 | * Runs the shutdown routine for the actor |
33 | */ |
34 | async shutdown () { |
35 | await this.state.done() |
36 | this.state.root['/'][3] = this.serializeMetaData() |
37 | } |
38 | |
39 | /** |
40 | * Runs the startup routine for the actor |
41 | */ |
42 | startup () { |
43 | return this.container.onStartup() |
44 | } |
45 | |
46 | /** |
47 | * run the Actor with a given message |
48 | * @param {object} message - the message to run |
49 | * @param {String} method - which method to run |
50 | * @returns {Promise} |
51 | */ |
52 | async runMessage (message) { |
53 | if (message._fromTicks > this.ticks) { |
54 | this.ticks = message._fromTicks |
55 | } |
56 | try { |
57 | this.currentMessage = message |
58 | await this.container.onMessage(message) |
59 | } catch (e) { |
60 | message.emit('execution:error', e) |
61 | } |
62 | message.emit('done', this) |
63 | } |
64 | |
65 | /** |
66 | * updates the number of ticks that the actor has run |
67 | * @param {Number} count - the number of ticks to add |
68 | */ |
69 | incrementTicks (count) { |
70 | this.ticks += count |
71 | } |
72 | |
73 | /** |
74 | * creates an actor |
75 | * @param {Integer} type - the type id for the container |
76 | * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor |
77 | */ |
78 | createActor (type, code) { |
79 | const id = this._generateNextId() |
80 | return this.hypervisor.createActor(type, code, id) |
81 | } |
82 | |
83 | _generateNextId () { |
84 | const id = { |
85 | nonce: this.nonce, |
86 | parent: this.id |
87 | } |
88 | |
89 | this.nonce++ |
90 | return id |
91 | } |
92 | |
93 | /** |
94 | * sends a message to a given port |
95 | * @param {Object} portRef - the port |
96 | * @param {Message} message - the message |
97 | */ |
98 | send (message) { |
99 | message._fromTicks = this.ticks |
100 | message._fromId = this.id |
101 | |
102 | this.hypervisor.scheduler.queue([message]) |
103 | } |
104 | |
105 | static serializeMetaData (type, nonce = 0) { |
106 | return [type, nonce] |
107 | } |
108 | |
109 | static deserializeMetaData ([type, nonce]) { |
110 | return { |
111 | nonce, |
112 | type |
113 | } |
114 | } |
115 | } |
116 |
Built with git-ssb-web