Skip to content

Commit baf0b21

Browse files
committed
middlewares -> plugins
1 parent db8b44f commit baf0b21

File tree

12 files changed

+74
-247
lines changed

12 files changed

+74
-247
lines changed

examples/chat/vuex/store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Vue from 'vue'
22
import Vuex from '../../../src'
33
import mutations from './mutations'
4-
import createLogger from '../../../src/middlewares/logger'
4+
import createLogger from '../../../src/plugins/logger'
55

66
Vue.use(Vuex)
77

@@ -33,7 +33,7 @@ export default new Vuex.Store({
3333
}
3434
},
3535
mutations,
36-
middlewares: process.env.NODE_ENV !== 'production'
36+
plugins: process.env.NODE_ENV !== 'production'
3737
? [createLogger()]
3838
: []
3939
})

examples/counter/store.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const state = {
1212
// mutations are operations that actually mutates the state.
1313
// each mutation handler gets the entire state tree as the
1414
// first argument, followed by additional payload arguments.
15-
// mutations must be synchronous and can be recorded by middlewares
15+
// mutations must be synchronous and can be recorded by plugins
1616
// for debugging purposes.
1717
const mutations = {
1818
INCREMENT (state) {
@@ -27,10 +27,6 @@ const mutations = {
2727
// and the mutations. Because the actions and mutations are just
2828
// functions that do not depend on the instance itself, they can
2929
// be easily tested or even hot-reloaded (see counter-hot example).
30-
//
31-
// You can also provide middlewares, which is just an array of
32-
// objects containing some hooks to be called at initialization
33-
// and after each mutation.
3430
export default new Vuex.Store({
3531
state,
3632
mutations

examples/shopping-cart/vuex/store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Vue from 'vue'
22
import Vuex from '../../../src'
33
import cart from './modules/cart'
44
import products from './modules/products'
5-
import createLogger from '../../../src/middlewares/logger'
5+
import createLogger from '../../../src/plugins/logger'
66

77
Vue.use(Vuex)
88
Vue.config.debug = true
@@ -15,5 +15,5 @@ export default new Vuex.Store({
1515
products
1616
},
1717
strict: debug,
18-
middlewares: debug ? [createLogger()] : []
18+
plugins: debug ? [createLogger()] : []
1919
})

examples/todomvc/vuex/middlewares.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/todomvc/vuex/plugins.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { STORAGE_KEY } from './store'
2+
import createLogger from '../../../src/plugins/logger'
3+
4+
const localStoragePlugin = store => {
5+
store.on('mutation', (mutation, { todos }) => {
6+
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
7+
})
8+
}
9+
10+
export default process.env.NODE_ENV !== 'production'
11+
? [createLogger(), localStoragePlugin]
12+
: [localStoragePlugin]

examples/todomvc/vuex/store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Vue from 'vue'
22
import Vuex from '../../../src'
3-
import middlewares from './middlewares'
3+
import plugins from './plugins'
44

55
Vue.use(Vuex)
66

@@ -49,5 +49,5 @@ const mutations = {
4949
export default new Vuex.Store({
5050
state,
5151
mutations,
52-
middlewares
52+
plugins
5353
})

src/index.js

Lines changed: 24 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
2-
mergeObjects, deepClone, isObject,
2+
mergeObjects, isObject,
33
getNestedState, getWatcher
44
} from './util'
5-
import devtoolMiddleware from './middlewares/devtool'
5+
import devtoolPlugin from './plugins/devtool'
66
import override from './override'
77

88
let Vue
@@ -15,21 +15,22 @@ class Store {
1515
* - {Object} state
1616
* - {Object} actions
1717
* - {Object} mutations
18-
* - {Array} middlewares
18+
* - {Array} plugins
1919
* - {Boolean} strict
2020
*/
2121

2222
constructor ({
2323
state = {},
2424
mutations = {},
2525
modules = {},
26-
middlewares = [],
26+
plugins = [],
2727
strict = false
2828
} = {}) {
2929
this._getterCacheId = 'vuex_store_' + uid++
3030
this._dispatching = false
3131
this._rootMutations = this._mutations = mutations
3232
this._modules = modules
33+
this._events = Object.create(null)
3334
// bind dispatch to self
3435
const dispatch = this.dispatch
3536
this.dispatch = (...args) => {
@@ -53,11 +54,13 @@ class Store {
5354
Vue.config.silent = silent
5455
this._setupModuleState(state, modules)
5556
this._setupModuleMutations(modules)
56-
this._setupMiddlewares(middlewares, state)
5757
// add extra warnings in strict mode
5858
if (strict) {
5959
this._setupMutationCheck()
6060
}
61+
// apply plugins
62+
devtoolPlugin(this)
63+
plugins.forEach(plugin => plugin(this))
6164
}
6265

6366
/**
@@ -91,25 +94,28 @@ class Store {
9194
if (type.silent) silent = true
9295
type = type.type
9396
}
94-
const mutation = this._mutations[type]
97+
const handler = this._mutations[type]
9598
const state = this.state
96-
if (mutation) {
99+
if (handler) {
97100
this._dispatching = true
98101
// apply the mutation
99-
if (Array.isArray(mutation)) {
100-
mutation.forEach(m => {
102+
if (Array.isArray(handler)) {
103+
handler.forEach(h => {
101104
isObjectStyleDispatch
102-
? m(state, payload)
103-
: m(state, ...payload)
105+
? h(state, payload)
106+
: h(state, ...payload)
104107
})
105108
} else {
106109
isObjectStyleDispatch
107-
? mutation(state, payload)
108-
: mutation(state, ...payload)
110+
? handler(state, payload)
111+
: handler(state, ...payload)
109112
}
110113
this._dispatching = false
111114
if (!silent) {
112-
this._applyMiddlewares(type, payload, isObjectStyleDispatch)
115+
const mutation = isObjectStyleDispatch
116+
? payload
117+
: { type, payload }
118+
this.emit('mutation', mutation, state)
113119
}
114120
} else {
115121
console.warn(`[vuex] Unknown mutation: ${type}`)
@@ -246,71 +252,6 @@ class Store {
246252
}, { deep: true, sync: true })
247253
/* eslint-enable no-new */
248254
}
249-
250-
/**
251-
* Setup the middlewares. The devtools middleware is always
252-
* included, since it does nothing if no devtool is detected.
253-
*
254-
* A middleware can demand the state it receives to be
255-
* "snapshots", i.e. deep clones of the actual state tree.
256-
*
257-
* @param {Array} middlewares
258-
* @param {Object} state
259-
*/
260-
261-
_setupMiddlewares (middlewares, state) {
262-
this._middlewares = [devtoolMiddleware].concat(middlewares)
263-
this._needSnapshots = middlewares.some(m => m.snapshot)
264-
if (this._needSnapshots) {
265-
console.log(
266-
'[vuex] One or more of your middlewares are taking state snapshots ' +
267-
'for each mutation. Make sure to use them only during development.'
268-
)
269-
}
270-
const initialSnapshot = this._prevSnapshot = this._needSnapshots
271-
? deepClone(state)
272-
: null
273-
// call init hooks
274-
this._middlewares.forEach(m => {
275-
if (m.onInit) {
276-
m.onInit(m.snapshot ? initialSnapshot : state, this)
277-
}
278-
})
279-
}
280-
281-
/**
282-
* Apply the middlewares on a given mutation.
283-
*
284-
* @param {String} type
285-
* @param {Array} payload
286-
* @param {Boolean} isObjectStyleDispatch
287-
*/
288-
289-
_applyMiddlewares (type, payload, isObjectStyleDispatch) {
290-
const state = this.state
291-
const prevSnapshot = this._prevSnapshot
292-
let snapshot, clonedPayload
293-
if (this._needSnapshots) {
294-
snapshot = this._prevSnapshot = deepClone(state)
295-
clonedPayload = deepClone(payload)
296-
}
297-
this._middlewares.forEach(m => {
298-
if (m.onMutation) {
299-
const mutation = isObjectStyleDispatch
300-
? m.snapshot
301-
? clonedPayload
302-
: payload
303-
: m.snapshot
304-
? { type, payload: clonedPayload }
305-
: { type, payload }
306-
if (m.snapshot) {
307-
m.onMutation(mutation, snapshot, prevSnapshot, this)
308-
} else {
309-
m.onMutation(mutation, state, this)
310-
}
311-
}
312-
})
313-
}
314255
}
315256

316257
function install (_Vue) {
@@ -321,6 +262,10 @@ function install (_Vue) {
321262
return
322263
}
323264
Vue = _Vue
265+
// reuse Vue's event system
266+
;['on', 'off', 'once', 'emit'].forEach(e => {
267+
Store.prototype[e] = Store.prototype['$' + e] = Vue.prototype['$' + e]
268+
})
324269
override(Vue)
325270
}
326271

src/middlewares/devtool.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/middlewares/logger.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/plugins/devtool.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const hook =
2+
typeof window !== 'undefined' &&
3+
window.__VUE_DEVTOOLS_GLOBAL_HOOK__
4+
5+
export default function devtoolPlugin (store) {
6+
if (!hook) return
7+
8+
hook.emit('vuex:init', store)
9+
10+
hook.on('vuex:travel-to-state', targetState => {
11+
store._dispatching = true
12+
store._vm.state = targetState
13+
store._dispatching = false
14+
})
15+
16+
store.on('mutation', (mutation, state) => {
17+
hook.emit('vuex:mutation', mutation, state)
18+
})
19+
}

0 commit comments

Comments
 (0)