|
@@ -18,38 +18,25 @@ export default class Vuex {
|
|
|
mutations = {},
|
|
|
middlewares = []
|
|
|
} = {}) {
|
|
|
-
|
|
|
|
|
|
this._vm = new Vue({
|
|
|
data: state
|
|
|
})
|
|
|
-
|
|
|
-
|
|
|
this.actions = Object.create(null)
|
|
|
- actions = Array.isArray(actions)
|
|
|
- ? mergeObjects(actions)
|
|
|
- : actions
|
|
|
- Object.keys(actions).forEach(name => {
|
|
|
- this.actions[name] = createAction(actions[name], this)
|
|
|
- })
|
|
|
+ this._setupActions(actions)
|
|
|
+ this._setupMutations(mutations)
|
|
|
+ this._setupMiddlewares(middlewares, state)
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- this._mutations = Array.isArray(mutations)
|
|
|
- ? mergeObjects(mutations, true)
|
|
|
- : mutations
|
|
|
+
|
|
|
+ * Getter for the entire state tree.
|
|
|
+ * Read only.
|
|
|
+ *
|
|
|
+ * @return {Object}
|
|
|
+ */
|
|
|
|
|
|
-
|
|
|
- this._middlewares = [devtoolMiddleware].concat(middlewares)
|
|
|
- this._needSnapshots = middlewares.some(m => m.snapshot)
|
|
|
- const initialSnapshot = this._prevSnapshot = this._needSnapshots
|
|
|
- ? deepClone(state)
|
|
|
- : null
|
|
|
-
|
|
|
- this._middlewares.forEach(m => {
|
|
|
- if (m.onInit) {
|
|
|
- m.onInit(m.snapshot ? initialSnapshot : state)
|
|
|
- }
|
|
|
- })
|
|
|
+ get state () {
|
|
|
+ return this._vm._data
|
|
|
}
|
|
|
|
|
|
|
|
@@ -88,13 +75,66 @@ export default class Vuex {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * Getter for the entire state tree.
|
|
|
+ * Hot update actions and mutations.
|
|
|
*
|
|
|
- * @return {Object}
|
|
|
+ * @param {Object} options
|
|
|
+ * - {Object} [actions]
|
|
|
+ * - {Object} [mutations]
|
|
|
*/
|
|
|
|
|
|
- get state () {
|
|
|
- return this._vm._data
|
|
|
+ hotUpdate ({ actions, mutations } = {}) {
|
|
|
+ if (actions) {
|
|
|
+ this._setupActions(actions, true)
|
|
|
+ }
|
|
|
+ if (mutations) {
|
|
|
+ this._setupMutations(mutations)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ _setupActions (actions, hot) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ this._actions = Object.create(null)
|
|
|
+ actions = Array.isArray(actions)
|
|
|
+ ? mergeObjects(actions)
|
|
|
+ : actions
|
|
|
+ Object.keys(actions).forEach(name => {
|
|
|
+ this._actions[name] = createAction(actions[name], this)
|
|
|
+ if (!this.actions[name]) {
|
|
|
+ this.actions[name] = () => this._actions[name]()
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ if (hot) {
|
|
|
+ Object.keys(this.actions).forEach(name => {
|
|
|
+ if (!actions[name]) {
|
|
|
+ delete this.actions[name]
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ _setupMutations (mutations) {
|
|
|
+ this._mutations = Array.isArray(mutations)
|
|
|
+ ? mergeObjects(mutations, true)
|
|
|
+ : mutations
|
|
|
+ }
|
|
|
+
|
|
|
+ _setupMiddlewares (middlewares, state) {
|
|
|
+ this._middlewares = [devtoolMiddleware].concat(middlewares)
|
|
|
+ this._needSnapshots = middlewares.some(m => m.snapshot)
|
|
|
+ const initialSnapshot = this._prevSnapshot = this._needSnapshots
|
|
|
+ ? deepClone(state)
|
|
|
+ : null
|
|
|
+
|
|
|
+ this._middlewares.forEach(m => {
|
|
|
+ if (m.onInit) {
|
|
|
+ m.onInit(m.snapshot ? initialSnapshot : state)
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
|