Skip to content

Commit f009ec3

Browse files
committed
插件
1 parent c19a06a commit f009ec3

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

vuex3/store.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ export class Store {
3535
// store internal state
3636
this._committing = false
3737
this._actions = Object.create(null) // 存放 actions
38-
this._actionSubscribers = [] // actions 订阅者
38+
this._actionSubscribers = [] // action 订阅者
3939
this._mutations = Object.create(null) // 存放 mutations
4040
this._wrappedGetters = Object.create(null) // 存放 getters
4141
this._modules = new ModuleCollection(options) // module 收集器
4242
this._modulesNamespaceMap = Object.create(null)
43-
this._subscribers = [] // 订阅者
43+
this._subscribers = [] // mutation 订阅者
4444
this._watcherVM = new Vue()
4545
this._makeLocalGettersCache = Object.create(null) // namespaced module 对应 getters 代理的缓存
4646

@@ -123,9 +123,10 @@ export class Store {
123123
})
124124
})
125125

126+
// 通知所有 mutation 订阅者
126127
this._subscribers
127128
.slice() // shallow copy to prevent iterator invalidation if subscriber synchronously calls unsubscribe
128-
.forEach(sub => sub(mutation, this.state))
129+
.forEach(sub => sub(mutation, this.state /* 最新的 state */))
129130

130131
if (
131132
__DEV__ &&
@@ -157,6 +158,7 @@ export class Store {
157158
}
158159

159160
try {
161+
// 通知所有 action 订阅者
160162
this._actionSubscribers
161163
.slice() // shallow copy to prevent iterator invalidation if subscriber synchronously calls unsubscribe
162164
.filter(sub => sub.before)
@@ -204,6 +206,7 @@ export class Store {
204206
})
205207
}
206208

209+
// 注册一个订阅函数,返回一个取消订阅的函数
207210
subscribe (fn, options) {
208211
return genericSubscribe(fn, this._subscribers, options)
209212
}
@@ -289,6 +292,7 @@ export class Store {
289292
}
290293
}
291294

295+
// 注册一个订阅函数,返回一个取消订阅的函数
292296
function genericSubscribe (fn, subs, options) {
293297
if (subs.indexOf(fn) < 0) {
294298
options && options.prepend

vuex3/util.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export function find (list, f) {
1919
* @param {Array<Object>} cache
2020
* @return {*}
2121
*/
22+
23+
// 深拷贝
2224
export function deepCopy (obj, cache = []) {
2325
// just return if obj is immutable value
2426
if (obj === null || typeof obj !== 'object') {

0 commit comments

Comments
 (0)