git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit ea9456b0cd659d6f9a4f7a039e4207d594c292e4

Merge pull request #117 from primea/getInstanceByPath

added get by path
wanderer 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.jschanged
index.jschanged
tests/index.jschanged
exoInterface.jsView
@@ -132,14 +132,18 @@
132132 // set the port that the message came from
133133 message._fromPort = this.entryPort
134134 message._fromPortTicks = this.ticks
135135
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)
138138
139139 const waiter = this._waitingMap.get(portRef)
140140 if (waiter) {
141141 waiter.resolve(this.ticks)
142142 this._waitingMap.delete(portRef)
143143 }
144144 }
145+
146+ getContainer (portRef) {
147+ return this.hypervisor.getOrCreateInstance(portRef, this.entryPort)
148+ }
145149 }
index.jsView
@@ -12,8 +12,17 @@
1212 this._runningContainers = new Map()
1313 this._containerTypes = {}
1414 }
1515
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+
1625 /**
1726 * get a contrainer instance given its entry port and its mounting port
1827 * @param {Object} port the entry port for the container
1928 * @param {Object} parentPort the entry port of the parent container
tests/index.jsView
@@ -652,5 +652,27 @@
652652
653653 await root.send(port, new Message())
654654 root.send(port, new Message())
655655 })
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+ })
656678 })

Built with git-ssb-web