Skip to content

Commit

Permalink
fix(store): keep original refs with $reset
Browse files Browse the repository at this point in the history
Fix #593
  • Loading branch information
posva committed Jul 30, 2021
1 parent b242f42 commit a7dadff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
3 changes: 3 additions & 0 deletions __tests__/store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ describe('Store', () => {
a: { b: 'string' },
},
})

// https://github.com/posva/pinia/issues/593
expect(store.nested.foo).toBe('bar')
})

it('can create an empty state if no state option is provided', () => {
Expand Down
14 changes: 8 additions & 6 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,15 @@ function createOptionsStore<
hot?: boolean
): Store<Id, S, G, A> {
const { state, actions, getters } = options
function $reset() {
pinia.state.value[id] = state ? state() : {}
}

const initialState: StateTree | undefined = pinia.state.value[id]

let store: Store<Id, S, G, A>

function setup() {
if (!initialState && (!__DEV__ || !hot)) {
$reset()
pinia.state.value[id] = state ? state() : {}
}
// pinia.state.value[id] = state ? state() : {}

// avoid creating a state in pinia.state.value
const localState =
Expand Down Expand Up @@ -135,7 +131,13 @@ function createOptionsStore<

store = createSetupStore(id, setup, options, pinia, hot)

store.$reset = $reset
store.$reset = function $reset() {
const newState = state ? state() : {}
// we use a patch to group all changes into one single subscription
this.$patch(($state) => {
assign($state, newState)
})
}

return store as any
}
Expand Down

0 comments on commit a7dadff

Please sign in to comment.