11import {
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'
66import override from './override'
77
88let 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
316257function 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
0 commit comments