Skip to content

Commit a7dadff

Browse files
committed
fix(store): keep original refs with $reset
Fix #593
1 parent b242f42 commit a7dadff

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

__tests__/store.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ describe('Store', () => {
9393
a: { b: 'string' },
9494
},
9595
})
96+
97+
// https://github.com/posva/pinia/issues/593
98+
expect(store.nested.foo).toBe('bar')
9699
})
97100

98101
it('can create an empty state if no state option is provided', () => {

src/store.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,15 @@ function createOptionsStore<
9595
hot?: boolean
9696
): Store<Id, S, G, A> {
9797
const { state, actions, getters } = options
98-
function $reset() {
99-
pinia.state.value[id] = state ? state() : {}
100-
}
10198

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

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

106103
function setup() {
107104
if (!initialState && (!__DEV__ || !hot)) {
108-
$reset()
105+
pinia.state.value[id] = state ? state() : {}
109106
}
110-
// pinia.state.value[id] = state ? state() : {}
111107

112108
// avoid creating a state in pinia.state.value
113109
const localState =
@@ -135,7 +131,13 @@ function createOptionsStore<
135131

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

138-
store.$reset = $reset
134+
store.$reset = function $reset() {
135+
const newState = state ? state() : {}
136+
// we use a patch to group all changes into one single subscription
137+
this.$patch(($state) => {
138+
assign($state, newState)
139+
})
140+
}
139141

140142
return store as any
141143
}

0 commit comments

Comments
 (0)