8000 store.on(mutation) -> store.subscribe · poorprogrammer/vuejs.org@031ddb5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 031ddb5

Browse files
committed
store.on(mutation) -> store.subscribe
1 parent 645bb9c commit 031ddb5

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

examples/todomvc/vuex/plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { STORAGE_KEY } from './store'
22
import createLogger from '../../../src/plugins/logger'
33

44
const localStoragePlugin = store => {
5-
store.on('mutation', (mutation, { todos }) => {
5+
store.subscribe((mutation, { todos }) => {
66
localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
77
})
88
}

src/index.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Store {
3030
this._dispatching = false
3131
this._rootMutations = this._mutations = mutations
3232
this._modules = modules
33-
this._events = Object.create(null)
33+
this._subscribers = []
3434
// bind dispatch to self
3535
const dispatch = this.dispatch
3636
this.dispatch = (...args) => {
@@ -127,7 +127,7 @@ class Store {
127127
const mutation = isObjectStyleDispatch
128128
? payload
129129
: { type, payload }
130-
this.emit('mutation', mutation, state)
130+
this._subscribers.forEach(sub => sub(mutation, state))
131131
}
132132
} else {
133133
console.warn(`[vuex] Unknown mutation: ${type}`)
@@ -152,6 +152,23 @@ class Store {
152152
return this._vm.$watch(() => fn(this.state), cb, options)
153153
}
154154

155+
/**
156+
* Subscribe to state changes. Fires after every mutation.
157+
*/
158+
159+
subscribe (fn) {
160+
const subs = this._subscribers
161+
if (subs.indexOf(fn) < 0) {
162+
subs.push(fn)
163+
}
164+
return () => {
165+
const i = subs.indexOf(fn)
166+
if (i > -1) {
167+
subs.splice(i, 1)
168+
}
169+
}
170+
}
171+
155172
/**
156173
* Hot update mutations & modules.
157174
*
@@ -274,10 +291,6 @@ function install (_Vue) {
274291
return
275292
}
276293
Vue = _Vue
277-
// reuse Vue's event system
278-
;['on', 'off', 'once', 'emit'].forEach(e => {
279-
Store.prototype[e] = Store.prototype['$' + e] = Vue.prototype['$' + e]
280-
})
281294
override(Vue)
282295
}
283296

src/plugins/devtool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function devtoolPlugin (store) {
1111
store.replaceState(targetState)
1212
})
1313

14-
store.on('mutation', (mutation, state) => {
14+
store.subscribe((mutation, state) => {
1515
hook.emit('vuex:mutation', mutation, state)
1616
})
1717
}

src/plugins/logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default function createLogger ({
88
return store => {
99
let prevState = JSON.parse(JSON.stringify(store.state))
1010

11-
store.on('mutation', (mutation, state) => {
11+
store.subscribe((mutation, state) => {
1212
if (typeof console === 'undefined') {
1313
return
1414
}

test/unit/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ describe('Vuex', () => {
285285
plugins: [
286286
store => {
287287
initState = store.state
288-
store.on('mutation', (mut, state) => {
288+
store.subscribe((mut, state) => {
289289
expect(state).to.equal(store.state)
290290
mutations.push(mut)
291291
})
@@ -314,7 +314,7 @@ describe('Vuex', () => {
314314
plugins: [
315315
store => {
316316
initState = store.state
317-
store.on('mutation', (mut, state) => {
317+
store.subscribe((mut, state) => {
318318
expect(state).to.equal(store.state)
319319
mutations.push(mut)
320320
})

0 commit comments

Comments
 (0)
0