Skip to content

Commit

Permalink
Merge pull request #61 from VladBrok/fix/collectState
Browse files Browse the repository at this point in the history
Collect state correctly if it's a function
  • Loading branch information
euaaaio authored Nov 12, 2023
2 parents 27ff2f1 + 4506c91 commit dfba683
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,14 @@ 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] = module.state
moduleState[moduleName] =
typeof module.state === 'function' ? module.state() : module.state
}
if (module.modules) {
forEachValue(module.modules, (childModule, childModuleName) => {
Expand Down
6 changes: 3 additions & 3 deletions store/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ it('applies old actions from store in modules', async () => {
{
user: {
namespaced: false,
state: { value: 0 },
state: () => ({ value: 0 }),
mutations: {
'user/historyLine': historyLine
}
Expand Down Expand Up @@ -1033,7 +1033,7 @@ it('applies old actions from store in nested modules', async () => {
}

let store2 = _createStore2<Store2State>({
state: { value: 0 },
state: () => ({ value: 0 }),
mutations: { historyLine },
modules: {
a: {
Expand All @@ -1047,7 +1047,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: {
Expand Down

0 comments on commit dfba683

Please sign in to comment.