Files: b4f2286715e5d94ee25ada50027f1690079cca1b / port.js
1086 bytesRaw
1 | const Cache = require('imperative-trie') |
2 | const common = require('./common') |
3 | |
4 | module.exports = class Port { |
5 | constructor (state, constructor) { |
6 | this.state = state |
7 | this.Kernel = constructor |
8 | this.cache = new Cache() |
9 | } |
10 | |
11 | async send (name, message) { |
12 | if (name === common.PARENT) { |
13 | message.from.push(this.state.name) |
14 | } else { |
15 | message.from.push(common.PARENT) |
16 | } |
17 | |
18 | const dest = await this.get(name) |
19 | return dest.recieve(message) |
20 | } |
21 | |
22 | async get (name) { |
23 | const vertex = name === common.PARENT ? this.cache.parent : this.cache.get(name) |
24 | |
25 | if (vertex) { |
26 | return vertex.value |
27 | } else { |
28 | const destState = await ( |
29 | name === common.PARENT |
30 | ? this.state.getParent() |
31 | : this.state.get([name])) |
32 | |
33 | const kernel = new this.Kernel({ |
34 | state: destState |
35 | }) |
36 | |
37 | const cache = new Cache(kernel) |
38 | kernel.ports.cache = cache |
39 | if (name === common.PARENT) { |
40 | cache.set(this.state.name, this.cache) |
41 | } else { |
42 | this.cache.set(name, cache) |
43 | } |
44 | return kernel |
45 | } |
46 | } |
47 | } |
48 |
Built with git-ssb-web