From 0dc3becdbd772dd94665a621c5f73daad0d1ec28 Mon Sep 17 00:00:00 2001 From: VladBrok Date: Sun, 12 Nov 2023 02:22:48 +0300 Subject: [PATCH 1/2] fix(store): collect state from modules if state is a function --- store/index.js | 3 ++- store/index.test.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/store/index.js b/store/index.js index 1963310..4fefd6b 100644 --- a/store/index.js +++ b/store/index.js @@ -436,7 +436,8 @@ function collectState(store) { let state = store.state || {} function collectModuleState(module, moduleName, moduleState) { if (moduleName) { - moduleState[moduleName] = module.state + moduleState[moduleName] = + typeof module.state === 'function' ? module.state() : module.state } if (module.modules) { forEachValue(module.modules, (childModule, childModuleName) => { diff --git a/store/index.test.ts b/store/index.test.ts index 53883ee..b426682 100644 --- a/store/index.test.ts +++ b/store/index.test.ts @@ -870,7 +870,7 @@ it('applies old actions from store in modules', async () => { { user: { namespaced: false, - state: { value: 0 }, + state: () => ({ value: 0 }), mutations: { 'user/historyLine': historyLine } From cf756dd16eb172a20d64c672495b2744b7379045 Mon Sep 17 00:00:00 2001 From: VladBrok Date: Sun, 12 Nov 2023 02:32:52 +0300 Subject: [PATCH 2/2] fix(store): handle case when the root store is a function --- store/index.js | 5 ++++- store/index.test.ts | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/store/index.js b/store/index.js index 4fefd6b..7332184 100644 --- a/store/index.js +++ b/store/index.js @@ -433,7 +433,10 @@ function unifyCommitArgs(type, payload = {}, options = {}) { } function collectState(store) { - let state = store.state || {} + let state = + store.state && typeof store.state === 'function' + ? store.state() + : store.state || {} function collectModuleState(module, moduleName, moduleState) { if (moduleName) { moduleState[moduleName] = diff --git a/store/index.test.ts b/store/index.test.ts index b426682..66ea426 100644 --- a/store/index.test.ts +++ b/store/index.test.ts @@ -1034,7 +1034,7 @@ it('applies old actions from store in nested modules', async () => { } let store2 = _createStore2({ - state: { value: 0 }, + state: () => ({ value: 0 }), mutations: { historyLine }, modules: { a: { @@ -1048,7 +1048,7 @@ it('applies old actions from store in nested modules', async () => { modules: { c: { namespaced: true, - state: { value: 0 }, + state: () => ({ value: 0 }), mutations: { historyLine }, modules: { d: {