From 86677ebcbfaecf712f339b73a568150fc9fd5f5e Mon Sep 17 00:00:00 2001 From: katashin Date: Thu, 2 Nov 2017 00:17:16 +0900 Subject: [PATCH] fix: avoid to call root state function twice (#1034) --- src/store.js | 9 ++------- test/unit/store.spec.js | 8 ++++++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/store.js b/src/store.js index c326b1943..55edd9ea9 100644 --- a/src/store.js +++ b/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 diff --git a/test/unit/store.spec.js b/test/unit/store.spec.js index 3b586d7d2..5645f2e06 100644 --- a/test/unit/store.spec.js +++ b/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()