/
vuex.js
192 lines (148 loc) · 4.36 KB
/
vuex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import applyMixin from './mixin.js';
import installModules from './register.js';
class Store {
constructor(options = {}) {
const { state } = options;
this._options = options;
this._committing = false;
this._mutations = Object.create(null);
this._actions = Object.create(null);
this._wrappedGetters = Object.create(null);
this._subscribers = [];
this._runtimeModules = Object.create(null);
this._watcherVM = new Vue();
const store = this;
const { commit, dispatch } = this;
this.commit = function(type, payload) {
return commit.call(store, type, payload);
}
this.dispatch = function(type, payload) {
return dispatch.call(store, type, payload);
}
installModules(this, state, [], options);
resetStoreVM(this, state);
}
get state() {
return this._vm.state;
}
set state(val) {
console.error('set %s fail, please use replaceState', val);
}
commit(type, payload) {
const entry = this._mutations[type];
if (!entry) {
console.log(`===type===: ${type} not exist`);
return;
}
const mutation = { type, payload };
entry.forEach(function mutationIterator(handler) {
console.log(`===exe commit===: ${type}, payload: ${payload}`);
handler(payload);
});
// subscribers
this._subscribers.forEach((handler) => handler(mutation, this.state));
}
dispatch(type, payload) {
console.log({type, payload});
const entry = this._actions[type];
if (!entry) {
console.warn('===type===', type, 'not exist');
return;
}
return entry.length > 1
? Promise.all(entry.map(handler => handler(payload)))
: entry[0](payload);
}
subscribe(handler, options = {}) {
const subs = this._subscribers;
if (subs.indexOf(handler) < 0) {
subs[options.prepend ? 'unshift' : 'push'](handler);
}
return () => {
var index = subs.indexOf(handler);
if (index > -1) {
subs.splice(index, 1);
}
}
}
registerModule(path, module) {
if (typeof path ==='string') path = [path];
this._runtimeModules[path.join('.')] = module;
installModules(this, this.state, path, module);
resetStoreVM(this, this.state)
}
unRegisterModule(path) {
if (typeof path === 'string') path = [path];
delete this._runtimeModules[path.join('.')];
const parentState = this.state;
Vue.delete(parentState, path[path.length - 1]);
resetStore(this);
}
replaceState(state) {
this._withCommit(() => {
this._vm.state = state;
});
}
_withCommit(fn) {
const _committing = this._committing;
this._committing = true;
fn();
this._committing = _committing;
}
watch(getter, callback, options) {
return this._watcherVM.$watch(() => getter(this.state), callback, options);
}
}
function resetStoreVM(store, state) {
// 外部访问的getters
store.getters = {};
// 响应式计算属性
const computed = {};
Object.keys(store._wrappedGetters).forEach(key => {
const fn = store._wrappedGetters[key];
// 这里不直接返回,包了一层,是给getters注入store对象
// Vue实例中的computed,只能获取当前实例上的数据,无法获取store数据
computed[key] = () => {
return fn(store);
};
// 定义外部访问的getters设置对应的getter
// 访问顺序:获取store.getters.foo时,顺序为:
// store_vm[key] -> computed[key] -> wrappedGetter
// 在这里做了层代理
Object.defineProperty(store.getters, key, {
get: () => store._vm[key]
});
});
store._vm = new Vue({
data: { state },
computed
})
}
function resetStore(store) {
store._mutations = Object.create(null);
store._actions = Object.create(null);
store._wrappedGetters = Object.create(null);
const state = store.state;
// reset root store
installModules(store, state, [], store._options, true);
// reset runtime store
Object.keys(store._runtimeModules).forEach(key => {
installModules(store, state, key.split('.'), store._runtimeModules[key], true);
});
// reset vm
resetStoreVM(store, state);
}
// install vuex plugin
var Vue;
function install(_Vue) {
if (Vue) {
console.error('[vuex] has installed already');
return;
}
Vue = _Vue;
applyMixin(Vue);
}
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
export default { install, Store };