Skip to content

Commit 86677eb

Browse files
ktsnyyx990803
authored andcommitted
fix: avoid to call root state function twice (#1034)
1 parent e821f1b commit 86677eb

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/store.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ export class Store {
2525
strict = false
2626
} = options
2727

28-
let {
29-
state = {}
30-
} = options
31-
if (typeof state === 'function') {
32-
state = state() || {}
33-
}
34-
3528
// store internal state
3629
this._committing = false
3730
this._actions = Object.create(null)
@@ -56,6 +49,8 @@ export class Store {
5649
// strict mode
5750
this.strict = strict
5851

52+
const state = this._modules.root.state
53+
5954
// init root module.
6055
// this also recursively registers all sub-modules
6156
// and collects all module getters inside this._wrappedGetters

test/unit/store.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ describe('Store', () => {
286286
expect(store.state.a).toBe(3)
287287
})
288288

289+
it('should not call root state function twice', () => {
290+
const spy = jasmine.createSpy().and.returnValue(1)
291+
new Vuex.Store({
292+
state: spy
293+
})
294+
expect(spy).toHaveBeenCalledTimes(1)
295+
})
296+
289297
it('subscribe: should handle subscriptions / unsubscriptions', () => {
290298
const subscribeSpy = jasmine.createSpy()
291299
const secondSubscribeSpy = jasmine.createSpy()

0 commit comments

Comments
 (0)