Jelajahi Sumber

[build] 2.0.0-rc.6

Evan You 8 tahun lalu
induk
melakukan
a4c899ca36
3 mengubah file dengan 570 tambahan dan 444 penghapusan
  1. 113 0
      dist/logger.js
  2. 456 443
      dist/vuex.js
  3. 1 1
      dist/vuex.min.js

+ 113 - 0
dist/logger.js

@@ -0,0 +1,113 @@
+(function (global, factory) {
+  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
+  typeof define === 'function' && define.amd ? define(factory) :
+  (global.createVuexLogger = factory());
+}(this, (function () { 'use strict';
+
+/**
+ * Get the first item that pass the test
+ * by second argument function
+ *
+ * @param {Array} list
+ * @param {Function} f
+ * @return {*}
+ */
+function find (list, f) {
+  return list.filter(f)[0]
+}
+
+/**
+ * Deep copy the given object considering circular structure.
+ * This function caches all nested objects and its copies.
+ * If it detects circular structure, use cached copy to avoid infinite loop.
+ *
+ * @param {*} obj
+ * @param {Array<Object>} cache
+ * @return {*}
+ */
+function deepCopy (obj, cache) {
+  if ( cache === void 0 ) cache = [];
+
+  // just return if obj is immutable value
+  if (obj === null || typeof obj !== 'object') {
+    return obj
+  }
+
+  // if obj is hit, it is in circular structure
+  var hit = find(cache, function (c) { return c.original === obj; })
+  if (hit) {
+    return hit.copy
+  }
+
+  var copy = Array.isArray(obj) ? [] : {}
+  // put the copy into cache at first
+  // because we want to refer it in recursive deepCopy
+  cache.push({
+    original: obj,
+    copy: copy
+  })
+
+  Object.keys(obj).forEach(function (key) {
+    copy[key] = deepCopy(obj[key], cache)
+  })
+
+  return copy
+}
+
+// Credits: borrowed code from fcomb/redux-logger
+
+function createLogger (ref) {
+  if ( ref === void 0 ) ref = {};
+  var collapsed = ref.collapsed; if ( collapsed === void 0 ) collapsed = true;
+  var transformer = ref.transformer; if ( transformer === void 0 ) transformer = function (state) { return state; };
+  var mutationTransformer = ref.mutationTransformer; if ( mutationTransformer === void 0 ) mutationTransformer = function (mut) { return mut; };
+
+  return function (store) {
+    var prevState = deepCopy(store.state)
+
+    store.subscribe(function (mutation, state) {
+      if (typeof console === 'undefined') {
+        return
+      }
+      var nextState = deepCopy(state)
+      var time = new Date()
+      var formattedTime = " @ " + (pad(time.getHours(), 2)) + ":" + (pad(time.getMinutes(), 2)) + ":" + (pad(time.getSeconds(), 2)) + "." + (pad(time.getMilliseconds(), 3))
+      var formattedMutation = mutationTransformer(mutation)
+      var message = "mutation " + (mutation.type) + formattedTime
+      var startMessage = collapsed
+        ? console.groupCollapsed
+        : console.group
+
+      // render
+      try {
+        startMessage.call(console, message)
+      } catch (e) {
+        console.log(message)
+      }
+
+      console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
+      console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
+      console.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))
+
+      try {
+        console.groupEnd()
+      } catch (e) {
+        console.log('—— log end ——')
+      }
+
+      prevState = nextState
+    })
+  }
+}
+
+function repeat (str, times) {
+  return (new Array(times + 1)).join(str)
+}
+
+function pad (num, maxLength) {
+  return repeat('0', maxLength - num.toString().length) + num
+}
+
+return createLogger;
+
+})));

+ 456 - 443
dist/vuex.js

@@ -1,5 +1,5 @@
 /**
- * vuex v2.0.0-rc.5
+ * vuex v2.0.0-rc.6
  * (c) 2016 Evan You
  * @license MIT
  */
@@ -7,509 +7,522 @@
   typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
   typeof define === 'function' && define.amd ? define(factory) :
   (global.Vuex = factory());
-}(this, function () { 'use strict';
+}(this, (function () { 'use strict';
 
-  var devtoolHook =
-    typeof window !== 'undefined' &&
-    window.__VUE_DEVTOOLS_GLOBAL_HOOK__
+var devtoolHook =
+  typeof window !== 'undefined' &&
+  window.__VUE_DEVTOOLS_GLOBAL_HOOK__
 
-  function devtoolPlugin (store) {
-    if (!devtoolHook) return
+function devtoolPlugin (store) {
+  if (!devtoolHook) { return }
 
-    store._devtoolHook = devtoolHook
+  store._devtoolHook = devtoolHook
 
-    devtoolHook.emit('vuex:init', store)
+  devtoolHook.emit('vuex:init', store)
 
-    devtoolHook.on('vuex:travel-to-state', function (targetState) {
-      store.replaceState(targetState)
-    })
+  devtoolHook.on('vuex:travel-to-state', function (targetState) {
+    store.replaceState(targetState)
+  })
 
-    store.subscribe(function (mutation, state) {
-      devtoolHook.emit('vuex:mutation', mutation, state)
-    })
-  }
+  store.subscribe(function (mutation, state) {
+    devtoolHook.emit('vuex:mutation', mutation, state)
+  })
+}
 
-  function applyMixin (Vue) {
-    var version = Number(Vue.version.split('.')[0])
+function applyMixin (Vue) {
+  var version = Number(Vue.version.split('.')[0])
 
-    if (version >= 2) {
-      var usesInit = Vue.config._lifecycleHooks.indexOf('init') > -1
-      Vue.mixin(usesInit ? { init: vuexInit } : { beforeCreate: vuexInit })
-    } else {
-      // override init and inject vuex init procedure
-      // for 1.x backwards compatibility.
-      var _init = Vue.prototype._init
-      Vue.prototype._init = function (options) {
-        if ( options === void 0 ) options = {};
-
-        options.init = options.init
-          ? [vuexInit].concat(options.init)
-          : vuexInit
-        _init.call(this, options)
-      }
-    }
+  if (version >= 2) {
+    var usesInit = Vue.config._lifecycleHooks.indexOf('init') > -1
+    Vue.mixin(usesInit ? { init: vuexInit } : { beforeCreate: vuexInit })
+  } else {
+    // override init and inject vuex init procedure
+    // for 1.x backwards compatibility.
+    var _init = Vue.prototype._init
+    Vue.prototype._init = function (options) {
+      if ( options === void 0 ) options = {};
 
-    /**
-     * Vuex init hook, injected into each instances init hooks list.
-     */
-
-    function vuexInit () {
-      var options = this.$options
-      // store injection
-      if (options.store) {
-        this.$store = options.store
-      } else if (options.parent && options.parent.$store) {
-        this.$store = options.parent.$store
-      }
+      options.init = options.init
+        ? [vuexInit].concat(options.init)
+        : vuexInit
+      _init.call(this, options)
     }
   }
 
-  function mapState (states) {
-    var res = {}
-    normalizeMap(states).forEach(function (ref) {
-      var key = ref.key;
-      var val = ref.val;
-
-      res[key] = function mappedState () {
-        return typeof val === 'function'
-          ? val.call(this, this.$store.state, this.$store.getters)
-          : this.$store.state[val]
-      }
-    })
-    return res
-  }
-
-  function mapMutations (mutations) {
-    var res = {}
-    normalizeMap(mutations).forEach(function (ref) {
-      var key = ref.key;
-      var val = ref.val;
-
-      res[key] = function mappedMutation () {
-        var args = [], len = arguments.length;
-        while ( len-- ) args[ len ] = arguments[ len ];
-
-        return this.$store.commit.apply(this.$store, [val].concat(args))
-      }
-    })
-    return res
-  }
-
-  function mapGetters (getters) {
-    var res = {}
-    normalizeMap(getters).forEach(function (ref) {
-      var key = ref.key;
-      var val = ref.val;
-
-      res[key] = function mappedGetter () {
-        if (!(val in this.$store.getters)) {
-          console.error(("[vuex] unknown getter: " + val))
-        }
-        return this.$store.getters[val]
-      }
-    })
-    return res
+  /**
+   * Vuex init hook, injected into each instances init hooks list.
+   */
+
+  function vuexInit () {
+    var options = this.$options
+    // store injection
+    if (options.store) {
+      this.$store = options.store
+    } else if (options.parent && options.parent.$store) {
+      this.$store = options.parent.$store
+    }
   }
+}
+
+function mapState (states) {
+  var res = {}
+  normalizeMap(states).forEach(function (ref) {
+    var key = ref.key;
+    var val = ref.val;
+
+    res[key] = function mappedState () {
+      return typeof val === 'function'
+        ? val.call(this, this.$store.state, this.$store.getters)
+        : this.$store.state[val]
+    }
+  })
+  return res
+}
 
-  function mapActions (actions) {
-    var res = {}
-    normalizeMap(actions).forEach(function (ref) {
-      var key = ref.key;
-      var val = ref.val;
+function mapMutations (mutations) {
+  var res = {}
+  normalizeMap(mutations).forEach(function (ref) {
+    var key = ref.key;
+    var val = ref.val;
 
-      res[key] = function mappedAction () {
-        var args = [], len = arguments.length;
-        while ( len-- ) args[ len ] = arguments[ len ];
+    res[key] = function mappedMutation () {
+      var args = [], len = arguments.length;
+      while ( len-- ) args[ len ] = arguments[ len ];
 
-        return this.$store.dispatch.apply(this.$store, [val].concat(args))
+      return this.$store.commit.apply(this.$store, [val].concat(args))
+    }
+  })
+  return res
+}
+
+function mapGetters (getters) {
+  var res = {}
+  normalizeMap(getters).forEach(function (ref) {
+    var key = ref.key;
+    var val = ref.val;
+
+    res[key] = function mappedGetter () {
+      if (!(val in this.$store.getters)) {
+        console.error(("[vuex] unknown getter: " + val))
       }
-    })
-    return res
-  }
-
-  function normalizeMap (map) {
-    return Array.isArray(map)
-      ? map.map(function (key) { return ({ key: key, val: key }); })
-      : Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
-  }
-
-  var Vue // bind on install
-
-  var Store = function Store (options) {
-    var this$1 = this;
-    if ( options === void 0 ) options = {};
+      return this.$store.getters[val]
+    }
+  })
+  return res
+}
 
-    assert(Vue, "must call Vue.use(Vuex) before creating a store instance.")
-    assert(typeof Promise !== 'undefined', "vuex requires a Promise polyfill in this browser.")
+function mapActions (actions) {
+  var res = {}
+  normalizeMap(actions).forEach(function (ref) {
+    var key = ref.key;
+    var val = ref.val;
 
-    var state = options.state; if ( state === void 0 ) state = {};
-    var plugins = options.plugins; if ( plugins === void 0 ) plugins = [];
-    var strict = options.strict; if ( strict === void 0 ) strict = false;
+    res[key] = function mappedAction () {
+      var args = [], len = arguments.length;
+      while ( len-- ) args[ len ] = arguments[ len ];
 
-    // store internal state
-    this._options = options
-    this._committing = false
-    this._actions = Object.create(null)
-    this._mutations = Object.create(null)
-    this._wrappedGetters = Object.create(null)
-      this._runtimeModules = Object.create(null)
-    this._subscribers = []
-    this._watcherVM = new Vue()
+      return this.$store.dispatch.apply(this.$store, [val].concat(args))
+    }
+  })
+  return res
+}
+
+function normalizeMap (map) {
+  return Array.isArray(map)
+    ? map.map(function (key) { return ({ key: key, val: key }); })
+    : Object.keys(map).map(function (key) { return ({ key: key, val: map[key] }); })
+}
+
+function isObject (obj) {
+  return obj !== null && typeof obj === 'object'
+}
+
+function isPromise (val) {
+  return val && typeof val.then === 'function'
+}
+
+function assert (condition, msg) {
+  if (!condition) { throw new Error(("[vuex] " + msg)) }
+}
+
+var Vue // bind on install
+
+var Store = function Store (options) {
+  var this$1 = this;
+  if ( options === void 0 ) options = {};
+
+  assert(Vue, "must call Vue.use(Vuex) before creating a store instance.")
+  assert(typeof Promise !== 'undefined', "vuex requires a Promise polyfill in this browser.")
+
+  var state = options.state; if ( state === void 0 ) state = {};
+  var plugins = options.plugins; if ( plugins === void 0 ) plugins = [];
+  var strict = options.strict; if ( strict === void 0 ) strict = false;
+
+  // store internal state
+  this._options = options
+  this._committing = false
+  this._actions = Object.create(null)
+  this._mutations = Object.create(null)
+  this._wrappedGetters = Object.create(null)
+  this._runtimeModules = Object.create(null)
+  this._subscribers = []
+  this._watcherVM = new Vue()
 
     // bind commit and dispatch to self
-    var store = this
-    var ref = this;
-    var dispatch = ref.dispatch;
-    var commit = ref.commit;
-      this.dispatch = function boundDispatch (type, payload) {
-      return dispatch.call(store, type, payload)
+  var store = this
+  var ref = this;
+  var dispatch = ref.dispatch;
+  var commit = ref.commit;
+  this.dispatch = function boundDispatch (type, payload) {
+    return dispatch.call(store, type, payload)
     }
     this.commit = function boundCommit (type, payload, options) {
-      return commit.call(store, type, payload, options)
-    }
-
-    // strict mode
-    this.strict = strict
-
-    // init root module.
-    // this also recursively registers all sub-modules
-    // and collects all module getters inside this._wrappedGetters
-    installModule(this, state, [], options)
-
-    // initialize the store vm, which is responsible for the reactivity
-    // (also registers _wrappedGetters as computed properties)
-    resetStoreVM(this, state)
-
-    // apply plugins
-    plugins.concat(devtoolPlugin).forEach(function (plugin) { return plugin(this$1); })
-  };
-
-  var prototypeAccessors = { state: {} };
-
-  prototypeAccessors.state.get = function () {
-    return this._vm.state
-  };
-
-  prototypeAccessors.state.set = function (v) {
-    assert(false, "Use store.replaceState() to explicit replace store state.")
-  };
+    return commit.call(store, type, payload, options)
+  }
 
-  Store.prototype.commit = function commit (type, payload, options) {
-      var this$1 = this;
+  // strict mode
+  this.strict = strict
 
-    // check object-style commit
-    var mutation
-    if (isObject(type) && type.type) {
-      options = payload
-      payload = mutation = type
-      type = type.type
-    } else {
-      mutation = { type: type, payload: payload }
-    }
-    var entry = this._mutations[type]
-    if (!entry) {
-      console.error(("[vuex] unknown mutation type: " + type))
-      return
-    }
-    this._withCommit(function () {
-      entry.forEach(function commitIterator (handler) {
-        handler(payload)
-      })
-    })
-    if (!options || !options.silent) {
-      this._subscribers.forEach(function (sub) { return sub(mutation, this$1.state); })
-    }
-  };
+  // init root module.
+  // this also recursively registers all sub-modules
+  // and collects all module getters inside this._wrappedGetters
+  installModule(this, state, [], options)
 
-  Store.prototype.dispatch = function dispatch (type, payload) {
-    var entry = this._actions[type]
-    if (!entry) {
-      console.error(("[vuex] unknown action type: " + type))
-      return
-    }
-    return entry.length > 1
-      ? Promise.all(entry.map(function (handler) { return handler(payload); }))
-      : entry[0](payload)
-  };
-
-  Store.prototype.subscribe = function subscribe (fn) {
-    var subs = this._subscribers
-    if (subs.indexOf(fn) < 0) {
-      subs.push(fn)
-    }
-    return function () {
-      var i = subs.indexOf(fn)
-      if (i > -1) {
-        subs.splice(i, 1)
-      }
-    }
-  };
+  // initialize the store vm, which is responsible for the reactivity
+  // (also registers _wrappedGetters as computed properties)
+  resetStoreVM(this, state)
 
-  Store.prototype.watch = function watch (getter, cb, options) {
-      var this$1 = this;
+  // apply plugins
+  plugins.concat(devtoolPlugin).forEach(function (plugin) { return plugin(this$1); })
+};
 
-    assert(typeof getter === 'function', "store.watch only accepts a function.")
-    return this._watcherVM.$watch(function () { return getter(this$1.state); }, cb, options)
-  };
+var prototypeAccessors = { state: {} };
 
-  Store.prototype.replaceState = function replaceState (state) {
-      var this$1 = this;
+prototypeAccessors.state.get = function () {
+  return this._vm.state
+};
 
-    this._withCommit(function () {
-      this$1._vm.state = state
-    })
-  };
-
-  Store.prototype.registerModule = function registerModule (path, module) {
-    if (typeof path === 'string') path = [path]
-    assert(Array.isArray(path), "module path must be a string or an Array.")
-    this._runtimeModules[path.join('.')] = module
-    installModule(this, this.state, path, module)
-    // reset store to update getters...
-    resetStoreVM(this, this.state)
-  };
-
-  Store.prototype.unregisterModule = function unregisterModule (path) {
-      var this$1 = this;
-
-    if (typeof path === 'string') path = [path]
-    assert(Array.isArray(path), "module path must be a string or an Array.")
-    delete this._runtimeModules[path.join('.')]
-    this._withCommit(function () {
-      var parentState = getNestedState(this$1.state, path.slice(0, -1))
-      Vue.delete(parentState, path[path.length - 1])
-    })
-    resetStore(this)
-  };
+prototypeAccessors.state.set = function (v) {
+  assert(false, "Use store.replaceState() to explicit replace store state.")
+};
 
-  Store.prototype.hotUpdate = function hotUpdate (newOptions) {
-    var options = this._options
-    if (newOptions.actions) {
-      options.actions = newOptions.actions
-    }
-    if (newOptions.mutations) {
-      options.mutations = newOptions.mutations
-    }
-    if (newOptions.getters) {
-      options.getters = newOptions.getters
-    }
-    if (newOptions.modules) {
-      for (var key in newOptions.modules) {
-        options.modules[key] = newOptions.modules[key]
-      }
-    }
-    resetStore(this)
-  };
-
-  Store.prototype._withCommit = function _withCommit (fn) {
-    var committing = this._committing
-      this._committing = true
-    fn()
-    this._committing = committing
-  };
-
-  Object.defineProperties( Store.prototype, prototypeAccessors );
+Store.prototype.commit = function commit (type, payload, options) {
+    var this$1 = this;
 
-  function assert (condition, msg) {
-    if (!condition) throw new Error(("[vuex] " + msg))
+  // check object-style commit
+  if (isObject(type) && type.type) {
+    options = payload
+    payload = type
+    type = type.type
   }
-
-  function resetStore (store) {
-    store._actions = Object.create(null)
-    store._mutations = Object.create(null)
-    store._wrappedGetters = Object.create(null)
-    var state = store.state
-    // init root module
-    installModule(store, state, [], store._options, true)
-    // init all runtime modules
-    Object.keys(store._runtimeModules).forEach(function (key) {
-      installModule(store, state, key.split('.'), store._runtimeModules[key], true)
-    })
-    // reset vm
-    resetStoreVM(store, state)
+  var mutation = { type: type, payload: payload }
+  var entry = this._mutations[type]
+  if (!entry) {
+    console.error(("[vuex] unknown mutation type: " + type))
+    return
   }
-
-  function resetStoreVM (store, state) {
-    var oldVm = store._vm
-
-    // bind store public getters
-    store.getters = {}
-    var wrappedGetters = store._wrappedGetters
-    var computed = {}
-    Object.keys(wrappedGetters).forEach(function (key) {
-      var fn = wrappedGetters[key]
-      // use computed to leverage its lazy-caching mechanism
-      computed[key] = function () { return fn(store); }
-      Object.defineProperty(store.getters, key, {
-        get: function () { return store._vm[key]; }
-      })
+  this._withCommit(function () {
+    entry.forEach(function commitIterator (handler) {
+      handler(payload)
     })
+  })
+  if (!options || !options.silent) {
+    this._subscribers.forEach(function (sub) { return sub(mutation, this$1.state); })
+  }
+};
 
-    // use a Vue instance to store the state tree
-    // suppress warnings just in case the user has added
-    // some funky global mixins
-    var silent = Vue.config.silent
-    Vue.config.silent = true
-    store._vm = new Vue({
-      data: { state: state },
-      computed: computed
-    })
-    Vue.config.silent = silent
-
-    // enable strict mode for new vm
-    if (store.strict) {
-      enableStrictMode(store)
-    }
-
-    if (oldVm) {
-      // dispatch changes in all subscribed watchers
-      // to force getter re-evaluation.
-      store._withCommit(function () {
-        oldVm.state = null
-      })
-      Vue.nextTick(function () { return oldVm.$destroy(); })
+Store.prototype.dispatch = function dispatch (type, payload) {
+  // check object-style dispatch
+  if (isObject(type) && type.type) {
+    payload = type
+    type = type.type
+  }
+  var entry = this._actions[type]
+  if (!entry) {
+    console.error(("[vuex] unknown action type: " + type))
+    return
+  }
+  return entry.length > 1
+    ? Promise.all(entry.map(function (handler) { return handler(payload); }))
+    : entry[0](payload)
+};
+
+Store.prototype.subscribe = function subscribe (fn) {
+  var subs = this._subscribers
+  if (subs.indexOf(fn) < 0) {
+    subs.push(fn)
+  }
+  return function () {
+    var i = subs.indexOf(fn)
+    if (i > -1) {
+      subs.splice(i, 1)
     }
   }
+};
 
-  function installModule (store, rootState, path, module, hot) {
-    var isRoot = !path.length
-    var state = module.state;
-    var actions = module.actions;
-    var mutations = module.mutations;
-    var getters = module.getters;
-    var modules = module.modules;
-
-    // set state
-    if (!isRoot && !hot) {
-      var parentState = getNestedState(rootState, path.slice(0, -1))
-      var moduleName = path[path.length - 1]
-      store._withCommit(function () {
-        Vue.set(parentState, moduleName, state || {})
-      })
-    }
+Store.prototype.watch = function watch (getter, cb, options) {
+    var this$1 = this;
 
-    if (mutations) {
-      Object.keys(mutations).forEach(function (key) {
-        registerMutation(store, key, mutations[key], path)
-      })
-    }
+  assert(typeof getter === 'function', "store.watch only accepts a function.")
+  return this._watcherVM.$watch(function () { return getter(this$1.state); }, cb, options)
+};
 
-    if (actions) {
-      Object.keys(actions).forEach(function (key) {
-        registerAction(store, key, actions[key], path)
-      })
-    }
+Store.prototype.replaceState = function replaceState (state) {
+    var this$1 = this;
 
-    if (getters) {
-      wrapGetters(store, getters, path)
-    }
+  this._withCommit(function () {
+    this$1._vm.state = state
+  })
+};
+
+Store.prototype.registerModule = function registerModule (path, module) {
+  if (typeof path === 'string') { path = [path] }
+  assert(Array.isArray(path), "module path must be a string or an Array.")
+  this._runtimeModules[path.join('.')] = module
+  installModule(this, this.state, path, module)
+  // reset store to update getters...
+  resetStoreVM(this, this.state)
+};
+
+Store.prototype.unregisterModule = function unregisterModule (path) {
+    var this$1 = this;
 
-    if (modules) {
-      Object.keys(modules).forEach(function (key) {
-        installModule(store, rootState, path.concat(key), modules[key], hot)
-      })
+  if (typeof path === 'string') { path = [path] }
+  assert(Array.isArray(path), "module path must be a string or an Array.")
+    delete this._runtimeModules[path.join('.')]
+  this._withCommit(function () {
+    var parentState = getNestedState(this$1.state, path.slice(0, -1))
+    Vue.delete(parentState, path[path.length - 1])
+  })
+  resetStore(this)
+};
+
+Store.prototype.hotUpdate = function hotUpdate (newOptions) {
+  updateModule(this._options, newOptions)
+  resetStore(this)
+};
+
+Store.prototype._withCommit = function _withCommit (fn) {
+  var committing = this._committing
+  this._committing = true
+  fn()
+  this._committing = committing
+};
+
+Object.defineProperties( Store.prototype, prototypeAccessors );
+
+function updateModule (targetModule, newModule) {
+  if (newModule.actions) {
+    targetModule.actions = newModule.actions
+  }
+  if (newModule.mutations) {
+    targetModule.mutations = newModule.mutations
+  }
+  if (newModule.getters) {
+    targetModule.getters = newModule.getters
+  }
+  if (newModule.modules) {
+    for (var key in newModule.modules) {
+      if (!(targetModule.modules && targetModule.modules[key])) {
+        console.warn(
+          "[vuex] trying to add a new module '" + key + "' on hot reloading, " +
+          'manual reload is needed'
+        )
+        return
+      }
+      updateModule(targetModule.modules[key], newModule.modules[key])
     }
   }
-
-  function registerMutation (store, type, handler, path) {
-    if ( path === void 0 ) path = [];
-
-    var entry = store._mutations[type] || (store._mutations[type] = [])
-    entry.push(function wrappedMutationHandler (payload) {
-      handler(getNestedState(store.state, path), payload)
+}
+
+function resetStore (store) {
+  store._actions = Object.create(null)
+  store._mutations = Object.create(null)
+  store._wrappedGetters = Object.create(null)
+  var state = store.state
+  // init root module
+  installModule(store, state, [], store._options, true)
+  // init all runtime modules
+  Object.keys(store._runtimeModules).forEach(function (key) {
+    installModule(store, state, key.split('.'), store._runtimeModules[key], true)
+  })
+  // reset vm
+  resetStoreVM(store, state)
+}
+
+function resetStoreVM (store, state) {
+  var oldVm = store._vm
+
+  // bind store public getters
+  store.getters = {}
+  var wrappedGetters = store._wrappedGetters
+  var computed = {}
+  Object.keys(wrappedGetters).forEach(function (key) {
+    var fn = wrappedGetters[key]
+    // use computed to leverage its lazy-caching mechanism
+    computed[key] = function () { return fn(store); }
+    Object.defineProperty(store.getters, key, {
+      get: function () { return store._vm[key]; }
     })
+  })
+
+  // use a Vue instance to store the state tree
+  // suppress warnings just in case the user has added
+  // some funky global mixins
+  var silent = Vue.config.silent
+  Vue.config.silent = true
+  store._vm = new Vue({
+    data: { state: state },
+    computed: computed
+  })
+  Vue.config.silent = silent
+
+  // enable strict mode for new vm
+  if (store.strict) {
+    enableStrictMode(store)
   }
 
-  function registerAction (store, type, handler, path) {
-    if ( path === void 0 ) path = [];
-
-    var entry = store._actions[type] || (store._actions[type] = [])
-    var dispatch = store.dispatch;
-    var commit = store.commit;
-    entry.push(function wrappedActionHandler (payload, cb) {
-      var res = handler({
-        dispatch: dispatch,
-        commit: commit,
-        getters: store.getters,
-        state: getNestedState(store.state, path),
-        rootState: store.state
-      }, payload, cb)
-      if (!isPromise(res)) {
-        res = Promise.resolve(res)
-      }
-      if (store._devtoolHook) {
-        return res.catch(function (err) {
-          store._devtoolHook.emit('vuex:error', err)
-          throw err
-        })
-      } else {
-        return res
-      }
+  if (oldVm) {
+    // dispatch changes in all subscribed watchers
+    // to force getter re-evaluation.
+    store._withCommit(function () {
+      oldVm.state = null
     })
+    Vue.nextTick(function () { return oldVm.$destroy(); })
   }
-
-  function wrapGetters (store, moduleGetters, modulePath) {
-    Object.keys(moduleGetters).forEach(function (getterKey) {
-      var rawGetter = moduleGetters[getterKey]
-      if (store._wrappedGetters[getterKey]) {
-        console.error(("[vuex] duplicate getter key: " + getterKey))
-        return
-      }
-      store._wrappedGetters[getterKey] = function wrappedGetter (store) {
-        return rawGetter(
-          getNestedState(store.state, modulePath), // local state
-          store.getters, // getters
-          store.state // root state
-        )
-      }
+}
+
+function installModule (store, rootState, path, module, hot) {
+  var isRoot = !path.length
+  var state = module.state;
+  var actions = module.actions;
+  var mutations = module.mutations;
+  var getters = module.getters;
+  var modules = module.modules;
+
+  // set state
+  if (!isRoot && !hot) {
+    var parentState = getNestedState(rootState, path.slice(0, -1))
+    var moduleName = path[path.length - 1]
+    store._withCommit(function () {
+      Vue.set(parentState, moduleName, state || {})
     })
   }
 
-  function enableStrictMode (store) {
-    store._vm.$watch('state', function () {
-      assert(store._committing, "Do not mutate vuex store state outside mutation handlers.")
-    }, { deep: true, sync: true })
+  if (mutations) {
+    Object.keys(mutations).forEach(function (key) {
+      registerMutation(store, key, mutations[key], path)
+    })
   }
 
-  function isObject (obj) {
-    return obj !== null && typeof obj === 'object'
+  if (actions) {
+    Object.keys(actions).forEach(function (key) {
+      registerAction(store, key, actions[key], path)
+    })
   }
 
-  function isPromise (val) {
-    return val && typeof val.then === 'function'
+  if (getters) {
+    wrapGetters(store, getters, path)
   }
 
-  function getNestedState (state, path) {
-    return path.length
-      ? path.reduce(function (state, key) { return state[key]; }, state)
-      : state
+  if (modules) {
+    Object.keys(modules).forEach(function (key) {
+      installModule(store, rootState, path.concat(key), modules[key], hot)
+    })
   }
-
-  function install (_Vue) {
-    if (Vue) {
-      console.error(
-        '[vuex] already installed. Vue.use(Vuex) should be called only once.'
-      )
+}
+
+function registerMutation (store, type, handler, path) {
+  if ( path === void 0 ) path = [];
+
+  var entry = store._mutations[type] || (store._mutations[type] = [])
+  entry.push(function wrappedMutationHandler (payload) {
+    handler(getNestedState(store.state, path), payload)
+  })
+}
+
+function registerAction (store, type, handler, path) {
+  if ( path === void 0 ) path = [];
+
+  var entry = store._actions[type] || (store._actions[type] = [])
+  var dispatch = store.dispatch;
+  var commit = store.commit;
+  entry.push(function wrappedActionHandler (payload, cb) {
+    var res = handler({
+      dispatch: dispatch,
+      commit: commit,
+      getters: store.getters,
+      state: getNestedState(store.state, path),
+      rootState: store.state
+    }, payload, cb)
+    if (!isPromise(res)) {
+      res = Promise.resolve(res)
+    }
+    if (store._devtoolHook) {
+      return res.catch(function (err) {
+        store._devtoolHook.emit('vuex:error', err)
+        throw err
+      })
+    } else {
+      return res
+    }
+  })
+}
+
+function wrapGetters (store, moduleGetters, modulePath) {
+  Object.keys(moduleGetters).forEach(function (getterKey) {
+    var rawGetter = moduleGetters[getterKey]
+    if (store._wrappedGetters[getterKey]) {
+      console.error(("[vuex] duplicate getter key: " + getterKey))
       return
     }
-    Vue = _Vue
-    applyMixin(Vue)
-  }
-
-  // auto install in dist mode
-  if (typeof window !== 'undefined' && window.Vue) {
-    install(window.Vue)
-  }
-
-  var index = {
-    Store: Store,
-    install: install,
-    mapState: mapState,
-    mapMutations: mapMutations,
-    mapGetters: mapGetters,
-    mapActions: mapActions
+    store._wrappedGetters[getterKey] = function wrappedGetter (store) {
+      return rawGetter(
+        getNestedState(store.state, modulePath), // local state
+        store.getters, // getters
+        store.state // root state
+      )
+    }
+  })
+}
+
+function enableStrictMode (store) {
+  store._vm.$watch('state', function () {
+    assert(store._committing, "Do not mutate vuex store state outside mutation handlers.")
+  }, { deep: true, sync: true })
+}
+
+function getNestedState (state, path) {
+  return path.length
+    ? path.reduce(function (state, key) { return state[key]; }, state)
+    : state
+}
+
+function install (_Vue) {
+  if (Vue) {
+    console.error(
+      '[vuex] already installed. Vue.use(Vuex) should be called only once.'
+    )
+    return
   }
-
-  return index;
-
-}));
+  Vue = _Vue
+  applyMixin(Vue)
+}
+
+// auto install in dist mode
+if (typeof window !== 'undefined' && window.Vue) {
+  install(window.Vue)
+}
+
+var index = {
+  Store: Store,
+  install: install,
+  mapState: mapState,
+  mapMutations: mapMutations,
+  mapGetters: mapGetters,
+  mapActions: mapActions
+}
+
+return index;
+
+})));

File diff ditekan karena terlalu besar
+ 1 - 1
dist/vuex.min.js


Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini