Skip to content

Show Set and Map expanded views #490

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions shells/dev/target/Other.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</template>

<script>
// this computed property should be visible
// this computed property should be visible
// even if component has no 'computed' defined
const computedPropMixin = {
computed: {
Expand All @@ -21,12 +21,12 @@ export default {
mixins: [ computedPropMixin ],
data () {
let a = { c: function () {} }
a.a = a
a.intentionalRecursion = a
let b = []
b[0] = b
return {
a: a,
b: b
intentionalRecusionArray: b
}
},
components: {
Expand All @@ -42,7 +42,9 @@ export default {
e: undefined,
f: true,
g: 12345,
h: 'I am a really long string mostly just to see how the horizontal scrolling works.'
h: 'I am a really long string mostly just to see how the horizontal scrolling works.',
i: new Set([1, 2, 3, 4, new Set([5, 6, 7, 8])]),
j: new Map([[1, 2], [3, 4], [5, new Map([[6, 7,]])]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try to put a Set inside a Map and the other way around?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; I've added a more complex test case in db270b3.

}
}
}
Expand Down
23 changes: 17 additions & 6 deletions src/devtools/components/DataField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
INFINITY,
NAN,
isPlainObject,
isMap,
isSet,
sortByKey
} from 'src/util'

Expand Down Expand Up @@ -89,22 +91,27 @@ export default {
},
isExpandableType () {
const value = this.field.value
return Array.isArray(value) || isPlainObject(value)
return Array.isArray(value) || isPlainObject(value) ||
isSet(value) || isMap(value)
},
formattedValue () {
const value = this.field.value
if (value === null) {
return 'null'
} else if (value === UNDEFINED) {
} else if (value === UNDEFINED || value === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(See below, data shouldn't be revived on devtools side.)

return 'undefined'
} else if (value === NAN) {
} else if (value === NAN || Number.isNaN(value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Same)

return 'NaN'
} else if (value === INFINITY) {
} else if (value === INFINITY || value === Number.POSITIVE_INFINITY) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Same)

return 'Infinity'
} else if (Array.isArray(value)) {
return 'Array[' + value.length + ']'
} else if (isPlainObject(value)) {
return 'Object' + (Object.keys(value).length ? '' : ' (empty)')
return 'Object[' + Object.keys(value).length + ']'
} else if (isSet(value)) {
return 'Set[' + value.size + ']'
} else if (isMap(value)) {
return 'Map[' + value.size + ']'
} else if (this.valueType === 'native') {
return specialTypeRE.exec(value)[1]
} else if (typeof value === 'string') {
Expand All @@ -125,11 +132,15 @@ export default {
key: i,
value: item
}))
} else if (typeof value === 'object') {
} else if (isPlainObject(value)) {
value = sortByKey(Object.keys(value).map(key => ({
key,
value: value[key]
})))
} else if (isSet(value)) {
value = Array.from(value.values()).map(v => ({ key: '_', value: v }))
} else if (isMap(value)) {
value = Array.from(value.entries()).map(([k, v]) => ({ key: k, value: v }))
}
return value
},
Expand Down
2 changes: 1 addition & 1 deletion src/devtools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function initApp (shell) {
})

bridge.on('instance-details', details => {
store.commit('components/RECEIVE_INSTANCE_DETAILS', parse(details))
store.commit('components/RECEIVE_INSTANCE_DETAILS', parse(details, true))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't revive data on devtools side.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects, Arrays, Numbers, Strings, and null are already revived on master. Is there any reason we don't want to do this for Sets, Maps, INF, undefined, and NaN?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only revive JSON-compatible data (which means no 'reviving', just like a standard JSON.parse()) because we may need to send it back again (state edition and Vuex time traveling).

Copy link
Member

@Akryum Akryum Jan 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the upcoming State edition system expects JSON-compatible data to work (even if the user doesn't send any change).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense. I've moved the set reconstruction logic into the component, so the store should only contain JSON-compatible data now.

})

bridge.on('vuex:init', snapshot => {
Expand Down
22 changes: 22 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function inDoc (node) {
export const UNDEFINED = '__vue_devtool_undefined__'
export const INFINITY = '__vue_devtool_infinity__'
export const NAN = '__vue_devtool_nan__'
export const SET = '__vue_devtool_set__'
export const MAP = '__vue_devtool_map__'

export function stringify (data) {
return CircularJSON.stringify(data, replacer)
Expand All @@ -53,6 +55,10 @@ function replacer (key, val) {
} else if (val instanceof RegExp) {
// special handling of native type
return `[native RegExp ${val.toString()}]`
} else if (isSet(val)) {
return SET + stringify(Array.from(val))
} else if (isMap(val)) {
return MAP + stringify(Array.from(val))
} else {
return sanitize(val)
}
Expand All @@ -71,6 +77,10 @@ function reviver (key, val) {
return Infinity
} else if (val === NAN) {
return NaN
} else if (isString(val) && val.startsWith(SET)) {
return new Set(parse(val.substring(SET.length), true))
} else if (isString(val) && val.startsWith(MAP)) {
return new Map(parse(val.substring(MAP.length), true))
} else {
return val
}
Expand Down Expand Up @@ -103,6 +113,18 @@ export function isPlainObject (obj) {
return Object.prototype.toString.call(obj) === '[object Object]'
}

export function isMap (obj) {
return obj instanceof Map
}

export function isSet (obj) {
return obj instanceof Set
}

export function isString (obj) {
return obj instanceof String || typeof obj === 'string'
}

function isPrimitive (data) {
if (data == null) {
return true
Expand Down