Browse Source

fix: avoid to call root state function twice (#1034)

katashin 7 years ago
parent
commit
86677ebcbf
2 changed files with 10 additions and 7 deletions
  1. 2 7
      src/store.js
  2. 8 0
      test/unit/store.spec.js

+ 2 - 7
src/store.js

@@ -25,13 +25,6 @@ export class Store {
       strict = false
     } = options
 
-    let {
-      state = {}
-    } = options
-    if (typeof state === 'function') {
-      state = state() || {}
-    }
-
     // store internal state
     this._committing = false
     this._actions = Object.create(null)
@@ -56,6 +49,8 @@ export class Store {
     // strict mode
     this.strict = strict
 
+    const state = this._modules.root.state
+
     // init root module.
     // this also recursively registers all sub-modules
     // and collects all module getters inside this._wrappedGetters

+ 8 - 0
test/unit/store.spec.js

@@ -286,6 +286,14 @@ describe('Store', () => {
     expect(store.state.a).toBe(3)
   })
 
+  it('should not call root state function twice', () => {
+    const spy = jasmine.createSpy().and.returnValue(1)
+    new Vuex.Store({
+      state: spy
+    })
+    expect(spy).toHaveBeenCalledTimes(1)
+  })
+
   it('subscribe: should handle subscriptions / unsubscriptions', () => {
     const subscribeSpy = jasmine.createSpy()
     const secondSubscribeSpy = jasmine.createSpy()