Skip to content

strict

javascript
class Store {
  constructor () {
    this._committing = false
  }

  // 严格模式下只能在 mutation 中修改 state
  // 监视 state,当变化时 this._committing 为 false 就警告。
  _withCommit (fn) {
    const committing = this._committing
    this._committing = true
    // 如果 fn 是异步,执行时 this._committing 必为 false。
    fn()
    this._committing = committing
  }

  commit (_type, _payload, _options) {
    const entry = this._mutations[type]
    if (!entry) {
      return
    }
    this._withCommit(() => {
      entry.forEach(function commitIterator (handler) {
        handler(payload)
      })
    })
  }

  // 内部有点地方需要直接修改 state,通过调用 this._withCommit 并在回调内修改,避免
  // 警告
  replaceState (state) {
    this._withCommit(() => {
      this._vm._data.$$state = state
    })
  }
}

function resetStoreVM (store, state) {
  if (store.strict) {
    enableStrictMode(store)
  }
}

function enableStrictMode (store) {
  store._vm.$watch(function () { return this._data.$$state }, () => {
    if (__DEV__) {
      assert(store._committing, `do not mutate vuex store state outside mutation handlers.`)
    }
  }, { deep: true, sync: true })
}