|
@@ -35,6 +35,7 @@ export class Store {
|
|
// store internal state
|
|
// store internal state
|
|
this._committing = false
|
|
this._committing = false
|
|
this._actions = Object.create(null)
|
|
this._actions = Object.create(null)
|
|
|
|
+ this._actionSubscribers = []
|
|
this._mutations = Object.create(null)
|
|
this._mutations = Object.create(null)
|
|
this._wrappedGetters = Object.create(null)
|
|
this._wrappedGetters = Object.create(null)
|
|
this._modules = new ModuleCollection(options)
|
|
this._modules = new ModuleCollection(options)
|
|
@@ -123,6 +124,7 @@ export class Store {
|
|
payload
|
|
payload
|
|
} = unifyObjectStyle(_type, _payload)
|
|
} = unifyObjectStyle(_type, _payload)
|
|
|
|
|
|
|
|
+ const action = { type, payload }
|
|
const entry = this._actions[type]
|
|
const entry = this._actions[type]
|
|
if (!entry) {
|
|
if (!entry) {
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
@@ -130,22 +132,20 @@ export class Store {
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ this._actionSubscribers.forEach(sub => sub(action, this.state))
|
|
|
|
+
|
|
return entry.length > 1
|
|
return entry.length > 1
|
|
? Promise.all(entry.map(handler => handler(payload)))
|
|
? Promise.all(entry.map(handler => handler(payload)))
|
|
: entry[0](payload)
|
|
: entry[0](payload)
|
|
}
|
|
}
|
|
|
|
|
|
subscribe (fn) {
|
|
subscribe (fn) {
|
|
- const subs = this._subscribers
|
|
|
|
- if (subs.indexOf(fn) < 0) {
|
|
|
|
- subs.push(fn)
|
|
|
|
- }
|
|
|
|
- return () => {
|
|
|
|
- const i = subs.indexOf(fn)
|
|
|
|
- if (i > -1) {
|
|
|
|
- subs.splice(i, 1)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ return genericSubscribe(fn, this._subscribers)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ subscribeAction (fn) {
|
|
|
|
+ return genericSubscribe(fn, this._actionSubscribers)
|
|
}
|
|
}
|
|
|
|
|
|
watch (getter, cb, options) {
|
|
watch (getter, cb, options) {
|
|
@@ -203,6 +203,18 @@ export class Store {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+function genericSubscribe (fn, subs) {
|
|
|
|
+ if (subs.indexOf(fn) < 0) {
|
|
|
|
+ subs.push(fn)
|
|
|
|
+ }
|
|
|
|
+ return () => {
|
|
|
|
+ const i = subs.indexOf(fn)
|
|
|
|
+ if (i > -1) {
|
|
|
|
+ subs.splice(i, 1)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
function resetStore (store, hot) {
|
|
function resetStore (store, hot) {
|
|
store._actions = Object.create(null)
|
|
store._actions = Object.create(null)
|
|
store._mutations = Object.create(null)
|
|
store._mutations = Object.create(null)
|