Skip to content

Commit bb1569c

Browse files
author
Guillaume Chau
committed
refactor(storage): use chrome.storage, fixes #979
1 parent 4e80915 commit bb1569c

File tree

10 files changed

+255
-196
lines changed

10 files changed

+255
-196
lines changed

shells/chrome/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"http://*/*",
3535
"https://*/*",
3636
"file:///*",
37-
"contextMenus"
37+
"contextMenus",
38+
"storage"
3839
],
3940
"content_scripts": [
4041
{

src/backend/index.js

Lines changed: 102 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { findRelatedComponent } from './utils'
1010
import { stringify, classify, camelize, set, has, parse, getComponentName, getCustomRefDetails } from '../util'
1111
import ComponentSelector from './component-selector'
1212
import SharedData, { init as initSharedData } from 'src/shared-data'
13+
import { init as initStorage } from 'src/storage'
1314
import { isBrowser, target } from 'src/devtools/env'
1415

1516
// hook should have been injected before this executes.
@@ -47,130 +48,132 @@ export function initBackend (_bridge) {
4748
}
4849

4950
function connect (Vue) {
50-
initSharedData({
51-
bridge,
52-
Vue
53-
})
51+
initStorage().then(() => {
52+
initSharedData({
53+
bridge,
54+
Vue
55+
})
5456

55-
hook.currentTab = 'components'
56-
bridge.on('switch-tab', tab => {
57-
hook.currentTab = tab
58-
if (tab === 'components') {
59-
flush()
60-
}
61-
})
57+
hook.currentTab = 'components'
58+
bridge.on('switch-tab', tab => {
59+
hook.currentTab = tab
60+
if (tab === 'components') {
61+
flush()
62+
}
63+
})
6264

63-
// the backend may get injected to the same page multiple times
64-
// if the user closes and reopens the devtools.
65-
// make sure there's only one flush listener.
66-
hook.off('flush')
67-
hook.on('flush', () => {
68-
if (hook.currentTab === 'components') {
69-
flush()
70-
}
71-
})
65+
// the backend may get injected to the same page multiple times
66+
// if the user closes and reopens the devtools.
67+
// make sure there's only one flush listener.
68+
hook.off('flush')
69+
hook.on('flush', () => {
70+
if (hook.currentTab === 'components') {
71+
flush()
72+
}
73+
})
7274

73-
bridge.on('select-instance', id => {
74-
currentInspectedId = id
75-
const instance = findInstanceOrVnode(id)
76-
if (!instance) return
77-
if (!/:functional:/.test(id)) bindToConsole(instance)
78-
flush()
79-
bridge.send('instance-selected')
80-
})
75+
bridge.on('select-instance', id => {
76+
currentInspectedId = id
77+
const instance = findInstanceOrVnode(id)
78+
if (!instance) return
79+
if (!/:functional:/.test(id)) bindToConsole(instance)
80+
flush()
81+
bridge.send('instance-selected')
82+
})
8183

82-
bridge.on('scroll-to-instance', id => {
83-
const instance = findInstanceOrVnode(id)
84-
if (instance) {
85-
scrollIntoView(instance)
86-
highlight(instance)
87-
}
88-
})
84+
bridge.on('scroll-to-instance', id => {
85+
const instance = findInstanceOrVnode(id)
86+
if (instance) {
87+
scrollIntoView(instance)
88+
highlight(instance)
89+
}
90+
})
8991

90-
bridge.on('filter-instances', _filter => {
91-
filter = _filter.toLowerCase()
92-
flush()
93-
})
92+
bridge.on('filter-instances', _filter => {
93+
filter = _filter.toLowerCase()
94+
flush()
95+
})
9496

95-
bridge.on('refresh', scan)
97+
bridge.on('refresh', scan)
9698

97-
bridge.on('enter-instance', id => {
98-
const instance = findInstanceOrVnode(id)
99-
if (instance) highlight(instance)
100-
})
99+
bridge.on('enter-instance', id => {
100+
const instance = findInstanceOrVnode(id)
101+
if (instance) highlight(instance)
102+
})
101103

102-
bridge.on('leave-instance', unHighlight)
104+
bridge.on('leave-instance', unHighlight)
103105

104-
// eslint-disable-next-line no-new
105-
new ComponentSelector(bridge, instanceMap)
106+
// eslint-disable-next-line no-new
107+
new ComponentSelector(bridge, instanceMap)
106108

107-
// Get the instance id that is targeted by context menu
108-
bridge.on('get-context-menu-target', () => {
109-
const instance = target.__VUE_DEVTOOLS_CONTEXT_MENU_TARGET__
109+
// Get the instance id that is targeted by context menu
110+
bridge.on('get-context-menu-target', () => {
111+
const instance = target.__VUE_DEVTOOLS_CONTEXT_MENU_TARGET__
110112

111-
target.__VUE_DEVTOOLS_CONTEXT_MENU_TARGET__ = null
112-
target.__VUE_DEVTOOLS_CONTEXT_MENU_HAS_TARGET__ = false
113+
target.__VUE_DEVTOOLS_CONTEXT_MENU_TARGET__ = null
114+
target.__VUE_DEVTOOLS_CONTEXT_MENU_HAS_TARGET__ = false
113115

114-
if (instance) {
115-
const id = instance.__VUE_DEVTOOLS_UID__
116-
if (id) {
117-
return bridge.send('inspect-instance', id)
116+
if (instance) {
117+
const id = instance.__VUE_DEVTOOLS_UID__
118+
if (id) {
119+
return bridge.send('inspect-instance', id)
120+
}
118121
}
119-
}
120-
121-
toast('No Vue component was found', 'warn')
122-
})
123122

124-
bridge.on('set-instance-data', args => {
125-
setStateValue(args)
126-
flush()
127-
})
123+
toast('No Vue component was found', 'warn')
124+
})
128125

129-
// vuex
130-
if (hook.store) {
131-
initVuexBackend(hook, bridge, hook.store.commit === undefined)
132-
} else {
133-
hook.once('vuex:init', store => {
134-
initVuexBackend(hook, bridge, store.commit === undefined)
126+
bridge.on('set-instance-data', args => {
127+
setStateValue(args)
128+
flush()
135129
})
136-
}
137130

138-
hook.once('router:init', () => {
139-
initRouterBackend(hook.Vue, bridge, rootInstances)
140-
})
131+
// vuex
132+
if (hook.store) {
133+
initVuexBackend(hook, bridge, hook.store.commit === undefined)
134+
} else {
135+
hook.once('vuex:init', store => {
136+
initVuexBackend(hook, bridge, store.commit === undefined)
137+
})
138+
}
141139

142-
// events
143-
initEventsBackend(Vue, bridge)
140+
hook.once('router:init', () => {
141+
initRouterBackend(hook.Vue, bridge, rootInstances)
142+
})
143+
144+
// events
145+
initEventsBackend(Vue, bridge)
144146

145-
target.__VUE_DEVTOOLS_INSPECT__ = inspectInstance
147+
target.__VUE_DEVTOOLS_INSPECT__ = inspectInstance
146148

147-
// User project devtools config
148-
if (target.hasOwnProperty('VUE_DEVTOOLS_CONFIG')) {
149-
const config = target.VUE_DEVTOOLS_CONFIG
149+
// User project devtools config
150+
if (target.hasOwnProperty('VUE_DEVTOOLS_CONFIG')) {
151+
const config = target.VUE_DEVTOOLS_CONFIG
150152

151-
// Open in editor
152-
if (config.hasOwnProperty('openInEditorHost')) {
153-
SharedData.openInEditorHost = config.openInEditorHost
153+
// Open in editor
154+
if (config.hasOwnProperty('openInEditorHost')) {
155+
SharedData.openInEditorHost = config.openInEditorHost
156+
}
154157
}
155-
}
156158

157-
bridge.log('backend ready.')
158-
bridge.send('ready', Vue.version)
159-
bridge.on('log-detected-vue', () => {
160-
console.log(
161-
`%c vue-devtools %c Detected Vue v${Vue.version} %c`,
162-
'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff',
163-
'background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff',
164-
'background:transparent'
165-
)
166-
})
159+
bridge.log('backend ready.')
160+
bridge.send('ready', Vue.version)
161+
bridge.on('log-detected-vue', () => {
162+
console.log(
163+
`%c vue-devtools %c Detected Vue v${Vue.version} %c`,
164+
'background:#35495e ; padding: 1px; border-radius: 3px 0 0 3px; color: #fff',
165+
'background:#41b883 ; padding: 1px; border-radius: 0 3px 3px 0; color: #fff',
166+
'background:transparent'
167+
)
168+
})
167169

168-
setTimeout(() => {
169-
scan()
170+
setTimeout(() => {
171+
scan()
170172

171-
// perf
172-
initPerfBackend(Vue, bridge, instanceMap)
173-
}, 0)
173+
// perf
174+
initPerfBackend(Vue, bridge, instanceMap)
175+
}, 0)
176+
})
174177
}
175178

176179
export function findInstanceOrVnode (id) {

src/devtools/index.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Vue from 'vue'
22
import App from './App.vue'
33
import router from './router'
4-
import store from './store'
4+
import { createStore } from './store'
55
import * as filters from './filters'
66
import './plugins'
77
import { parse } from '../util'
88
import { isChrome, initEnv } from './env'
99
import SharedData, { init as initSharedData, destroy as destroySharedData } from 'src/shared-data'
10+
import { init as initStorage } from 'src/storage'
1011
import VuexResolve from './views/vuex/resolve'
1112

1213
for (const key in filters) {
@@ -67,13 +68,15 @@ let app = null
6768
*/
6869

6970
export function initDevTools (shell) {
70-
initApp(shell)
71-
shell.onReload(() => {
72-
if (app) {
73-
app.$destroy()
74-
}
75-
bridge.removeAllListeners()
71+
initStorage().then(() => {
7672
initApp(shell)
73+
shell.onReload(() => {
74+
if (app) {
75+
app.$destroy()
76+
}
77+
bridge.removeAllListeners()
78+
initApp(shell)
79+
})
7780
})
7881
}
7982

@@ -106,6 +109,8 @@ function initApp (shell) {
106109
bridge.send('log-detected-vue')
107110
}
108111

112+
const store = createStore()
113+
109114
bridge.once('ready', version => {
110115
store.commit(
111116
'SHOW_MESSAGE',
@@ -197,7 +202,14 @@ function initApp (shell) {
197202

198203
bridge.on('inspect-instance', id => {
199204
ensurePaneShown(() => {
200-
inspectInstance(id)
205+
bridge.send('select-instance', id)
206+
router.push({ name: 'components' })
207+
const instance = store.state.components.instancesMap[id]
208+
instance && store.dispatch('components/toggleInstance', {
209+
instance,
210+
expanded: true,
211+
parent: true
212+
})
201213
})
202214
})
203215

@@ -240,17 +252,6 @@ function getContextMenuInstance () {
240252
bridge.send('get-context-menu-target')
241253
}
242254

243-
function inspectInstance (id) {
244-
bridge.send('select-instance', id)
245-
router.push({ name: 'components' })
246-
const instance = store.state.components.instancesMap[id]
247-
instance && store.dispatch('components/toggleInstance', {
248-
instance,
249-
expanded: true,
250-
parent: true
251-
})
252-
}
253-
254255
// Pane visibility management
255256

256257
function ensurePaneShown (cb) {

0 commit comments

Comments
 (0)