Skip to content

Commit

Permalink
Use unique indexes for documents
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Aug 23, 2017
1 parent cfb5fcf commit c170640
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,12 @@ function connect () {
flush()
})

bridge.on('change-target', (target) => {
setDocumentTarget(getAllTargets()[target])
scan()
bridge.on('change-target', (id) => {
const target = getAllTargets().find(t => t.id === id)
if (target) {
setDocumentTarget(target.doc)
scan()
}
})
bridge.on('refresh', scan)
bridge.on('enter-instance', id => highlight(instanceMap.get(id)))
Expand Down
28 changes: 24 additions & 4 deletions src/backend/target-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,32 @@ export function getDocumentTarget () {
return target
}

let id = 0
const documents = new WeakMap()

function getId (doc) {
console.log('Getting', doc)
if (!documents.has(doc)) {
console.log('Added with', id)
documents.set(doc, id++)
}
console.log(`generated id ${documents.get(doc)}`, doc)
return documents.get(doc)
}

function packTarget (doc) {
return {
doc,
id: getId(doc)
}
}

export function getAllTargets () {
const targets = [document]
return targets.concat(findTargetsInElement(document))
const targets = [packTarget(document)]
return targets.concat(findTargetsInElement(targets[0]))
}

function findTargetsInElement (el) {
const iframes = Array.prototype.map.call(el.getElementsByTagName('iframe'), i => i.contentDocument)
function findTargetsInElement ({ doc }) {
const iframes = Array.prototype.map.call(doc.getElementsByTagName('iframe'), i => i.contentDocument).map(packTarget)
return iframes.concat(...iframes.map(findTargetsInElement))
}
5 changes: 3 additions & 2 deletions src/devtools/components/TargetSelector.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<template>
<div class="target-selector">
<select v-if="targets.length" @click="refreshTargets" v-model="currentTarget" @change="setTarget">
<option v-for="(target, i) in targets" :value="i"
>{{ target.location.pathname }}</option>
<option v-for="target in targets" :value="target.id"
>({{ target.id }}) {{ target.doc.location.pathname }}</option>
</select>
<button @click="currentTarget = null">reset</button>
<button @click="refreshTargets">refresh</button>
</div>
</template>

Expand Down

0 comments on commit c170640

Please sign in to comment.