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

feat: setup devtools integration #1949

Merged
merged 18 commits into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat(devtools): actions timeline layer
  • Loading branch information
Akryum committed Apr 20, 2021
commit f4766b75abff67735e03776021abbcaaa7a3e935
8 changes: 7 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{
"presets": ["@babel/preset-env"]
"presets": [
["@babel/preset-env", {
"exclude": [
"transform-regenerator"
]
}]
]
}
24 changes: 17 additions & 7 deletions examples/classic/shopping-cart/api/shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@ const _products = [
]

export default {
getProducts (cb) {
setTimeout(() => cb(_products), 100)
async getProducts () {
await wait(100)
return _products
},

buyProducts (products, cb, errorCb) {
setTimeout(() => {
async buyProducts (products) {
await wait(1000)
if (
// simulate random checkout failure.
(Math.random() > 0.5 || navigator.webdriver)
? cb()
: errorCb()
}, 100)
) {
return
} else {
throw new Error('Checkout error')
}
}
}

function wait (ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
20 changes: 10 additions & 10 deletions examples/classic/shopping-cart/store/modules/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ const getters = {

// actions
const actions = {
checkout ({ commit, state }, products) {
async checkout ({ commit, state }, products) {
const savedCartItems = [...state.items]
commit('setCheckoutStatus', null)
// empty cart
commit('setCartItems', { items: [] })
shop.buyProducts(
products,
() => commit('setCheckoutStatus', 'successful'),
() => {
commit('setCheckoutStatus', 'failed')
// rollback to the cart saved before sending the request
commit('setCartItems', { items: savedCartItems })
}
)
try {
await shop.buyProducts(products)
commit('setCheckoutStatus', 'successful')
} catch (e) {
console.error(e)
commit('setCheckoutStatus', 'failed')
// rollback to the cart saved before sending the request
commit('setCartItems', { items: savedCartItems })
}
},

addProductToCart ({ state, commit }, product) {
Expand Down
7 changes: 3 additions & 4 deletions examples/classic/shopping-cart/store/modules/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ const getters = {}

// actions
const actions = {
getAllProducts ({ commit }) {
shop.getProducts(products => {
commit('setProducts', products)
})
async getAllProducts ({ commit }) {
const products = await shop.getProducts()
commit('setProducts', products)
}
}

Expand Down
58 changes: 57 additions & 1 deletion src/plugins/devtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const LABEL_VUEX_BINDINGS = 'vuex bindings'
let isAlreadyInstalled

const MUTATIONS_LAYER_ID = 'vuex:mutations'
const ACTIONS_LAYER_ID = 'vuex:actions'
const INSPECTOR_ID = 'vuex'

export function addDevtools (app, store) {
Expand All @@ -22,7 +23,13 @@ export function addDevtools (app, store) {
if (!isAlreadyInstalled) {
api.addTimelineLayer({
id: MUTATIONS_LAYER_ID,
label: 'Vuex',
label: 'Vuex Mutations',
color: COLOR_LIME_500
})

api.addTimelineLayer({
id: ACTIONS_LAYER_ID,
label: 'Vuex Actions',
color: COLOR_LIME_500
})

Expand Down Expand Up @@ -82,6 +89,55 @@ export function addDevtools (app, store) {
}
})
})

store.subscribeAction({
before: (action, state) => {
const data = {}
if (action.payload) {
data.payload = action.payload
}
action.time = Date.now()
data.state = state

api.addTimelineEvent({
layerId: ACTIONS_LAYER_ID,
event: {
time: action.time,
title: action.type,
groupId: action.id,
subtitle: 'start',
data
}
})
},
after: (action, state) => {
const data = {}
const duration = Date.now() - action.time
data.duration = {
_custom: {
type: 'duration',
display: `${duration}ms`,
tooltip: 'Action duration',
value: duration
}
}
if (action.payload) {
data.payload = action.payload
}
data.state = state

api.addTimelineEvent({
layerId: ACTIONS_LAYER_ID,
event: {
time: Date.now(),
title: action.type,
groupId: action.id,
subtitle: 'end',
data
}
})
}
})
}
)
}
Expand Down
7 changes: 6 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Store {
// store internal state
this._committing = false
this._actions = Object.create(null)
this._actionId = 0
this._actionSubscribers = []
this._mutations = Object.create(null)
this._wrappedGetters = Object.create(null)
Expand Down Expand Up @@ -122,7 +123,11 @@ export class Store {
payload
} = unifyObjectStyle(_type, _payload)

const action = { type, payload }
const action = {
id: this._actionId++,
type,
payload
}
const entry = this._actions[type]
if (!entry) {
if (__DEV__) {
Expand Down