Files: 26423b152859d2a3cccf24570ef55cf2770aee41 / index.js
982 bytesRaw
1 | const Kernel = require('./index.js') |
2 | |
3 | module.exports = class Hypervisor { |
4 | constructor (opts) { |
5 | this._opts = { |
6 | state: {}, |
7 | imports: [], |
8 | hypervisor: this |
9 | } |
10 | |
11 | Object.assign(this._opts, opts) |
12 | this._runningVMs = new Map() |
13 | } |
14 | |
15 | set (path, value) { |
16 | return this._opts.graph.set(this._opts.state, path, value) |
17 | } |
18 | |
19 | sendMessage (portObj, message) { |
20 | const vm = this.getInstaceFromPort(portObj) |
21 | vm.queue(message) |
22 | } |
23 | |
24 | getVMFromPort (port) { |
25 | const id = Kernel.id(port) |
26 | let kernel = this._vms.get(id) |
27 | if (!kernel) { |
28 | kernel = new Kernel(port) |
29 | kernel.on('idle', () => { |
30 | this._vms.delete(id) |
31 | }) |
32 | this._vms.set(id, kernel) |
33 | } |
34 | } |
35 | |
36 | // given a port, wait untill its source contract has reached the threshold |
37 | // tick count |
38 | async waitOnPort (port, ticks) { |
39 | let kernel = this.getVMFromPort(port) |
40 | const tickCount = await kernel.wait(ticks) |
41 | port.ticks = tickCount |
42 | return tickCount |
43 | } |
44 | } |
45 |
Built with git-ssb-web