Commit ea9456b0cd659d6f9a4f7a039e4207d594c292e4
Merge pull request #117 from primea/getInstanceByPath
added get by pathwanderer authored on 5/22/2017, 9:27:27 PM
GitHub committed on 5/22/2017, 9:27:27 PM
Parent: eb936c252664b13565410d09b4facc45b738e249
Parent: 07c0df717b109d015093957f1176a824fe29d25a
Files changed
exoInterface.js | changed |
index.js | changed |
tests/index.js | changed |
exoInterface.js | ||
---|---|---|
@@ -132,14 +132,18 @@ | ||
132 | 132 | // set the port that the message came from |
133 | 133 | message._fromPort = this.entryPort |
134 | 134 | message._fromPortTicks = this.ticks |
135 | 135 | |
136 | - const instance = await this.hypervisor.getOrCreateInstance(portRef, this.entryPort) | |
137 | - instance.queue(message) | |
136 | + const container = await this.getContainer(portRef) | |
137 | + container.queue(message) | |
138 | 138 | |
139 | 139 | const waiter = this._waitingMap.get(portRef) |
140 | 140 | if (waiter) { |
141 | 141 | waiter.resolve(this.ticks) |
142 | 142 | this._waitingMap.delete(portRef) |
143 | 143 | } |
144 | 144 | } |
145 | + | |
146 | + getContainer (portRef) { | |
147 | + return this.hypervisor.getOrCreateInstance(portRef, this.entryPort) | |
148 | + } | |
145 | 149 | } |
index.js | ||
---|---|---|
@@ -12,8 +12,17 @@ | ||
12 | 12 | this._runningContainers = new Map() |
13 | 13 | this._containerTypes = {} |
14 | 14 | } |
15 | 15 | |
16 | + async getByPath (root, path) { | |
17 | + path = path.split('/') | |
18 | + for (const name of path) { | |
19 | + const portRef = root.ports.get(name) | |
20 | + root = await this.getOrCreateInstance(portRef, root.entryPort) | |
21 | + } | |
22 | + return root | |
23 | + } | |
24 | + | |
16 | 25 | /** |
17 | 26 | * get a contrainer instance given its entry port and its mounting port |
18 | 27 | * @param {Object} port the entry port for the container |
19 | 28 | * @param {Object} parentPort the entry port of the parent container |
tests/index.js | ||
---|---|---|
@@ -652,5 +652,27 @@ | ||
652 | 652 | |
653 | 653 | await root.send(port, new Message()) |
654 | 654 | root.send(port, new Message()) |
655 | 655 | }) |
656 | + | |
657 | + tape('get container instance by path', async t => { | |
658 | + t.plan(1) | |
659 | + const hypervisor = new Hypervisor(node.dag) | |
660 | + hypervisor.registerContainer('base', BaseContainer) | |
661 | + | |
662 | + const root = await hypervisor.createInstance('base') | |
663 | + let port = root.ports.create('base') | |
664 | + root.ports.bind(port, 'first') | |
665 | + | |
666 | + const first = await root.getContainer(port) | |
667 | + port = first.ports.create('base') | |
668 | + first.ports.bind(port, 'second') | |
669 | + | |
670 | + const second = await first.getContainer(port) | |
671 | + port = second.ports.create('base') | |
672 | + second.ports.bind(port, 'third') | |
673 | + | |
674 | + const third = await second.getContainer(port) | |
675 | + const foundThird = await hypervisor.getByPath(root, 'first/second/third') | |
676 | + t.equals(third, foundThird, 'should find by path') | |
677 | + }) | |
656 | 678 | }) |
Built with git-ssb-web