Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no getters displayed on root module + better getters inspector #1986

Merged
merged 4 commits into from Jun 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 40 additions & 3 deletions src/plugins/devtool.js
Expand Up @@ -59,7 +59,7 @@ export function addDevtools (app, store) {
makeLocalGetters(store, modulePath)
payload.state = formatStoreForInspectorState(
getStoreModule(store._modules, modulePath),
store._makeLocalGettersCache,
modulePath === 'root' ? store.getters : store._makeLocalGettersCache,
modulePath
)
}
Expand Down Expand Up @@ -228,16 +228,45 @@ function formatStoreForInspectorState (module, getters, path) {
}

if (gettersKeys.length) {
storeState.getters = gettersKeys.map((key) => ({
const tree = transformPathsToObjectTree(getters)
storeState.getters = Object.keys(tree).map((key) => ({
key: key.endsWith('/') ? extractNameFromPath(key) : key,
editable: false,
value: getters[key]
value: canThrow(() => tree[key])
}))
}

return storeState
}

function transformPathsToObjectTree (getters) {
const result = {}
Object.keys(getters).forEach(key => {
const path = key.split('/')
if (path.length > 1) {
let target = result
const leafKey = path.pop()
for (const p of path) {
if (!target[p]) {
target[p] = {
_custom: {
value: {},
display: p,
tooltip: 'Module',
abstract: true
}
}
}
target = target[p]._custom.value
}
target[leafKey] = canThrow(() => getters[key])
} else {
result[key] = canThrow(() => getters[key])
}
})
return result
}

function getStoreModule (moduleMap, path) {
const names = path.split('/').filter((n) => n)
return names.reduce(
Expand All @@ -251,3 +280,11 @@ function getStoreModule (moduleMap, path) {
path === 'root' ? moduleMap : moduleMap.root._children
)
}

function canThrow (cb) {
try {
return cb()
} catch (e) {
return e
}
}