Skip to content

Commit 819f21c

Browse files
committed
refactor: improve shallowRef handling based on CodeRabbit feedback
- Replace setupStoreRef with toRaw(store) to eliminate coupling and state drift - Use Object.prototype.hasOwnProperty.call() for safer property checking - Improve type safety with Ref[] instead of any[] - Remove unnecessary setupStoreRef assignment and variable declaration - Maintain same functionality with more robust implementation
1 parent 3cf2466 commit 819f21c

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

packages/pinia/src/store.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,6 @@ function createSetupStore<
296296
// https://github.com/vuejs/pinia/issues/1129
297297
let activeListener: Symbol | undefined
298298

299-
// Store reference for shallowRef handling - will be set after setupStore creation
300-
let setupStoreRef: any = null
301-
302299
function $patch(stateMutation: (state: UnwrapRef<S>) => void): void
303300
function $patch(partialState: _DeepPartial<UnwrapRef<S>>): void
304301
function $patch(
@@ -323,24 +320,21 @@ function createSetupStore<
323320
} else {
324321
mergeReactiveObjects(pinia.state.value[$id], partialStateOrMutator)
325322

326-
// Handle shallowRef reactivity: check if any patched properties are shallowRefs
327-
// and trigger their reactivity manually
328-
if (setupStoreRef) {
329-
const shallowRefsToTrigger: any[] = []
323+
// Handle shallowRef reactivity: inspect raw store to avoid ref unwrapping
324+
{
325+
const rawStore = toRaw(store) as Record<string, unknown>
326+
const shallowRefsToTrigger: Ref[] = []
330327
for (const key in partialStateOrMutator) {
331-
if (partialStateOrMutator.hasOwnProperty(key)) {
332-
// Check if the property in the setupStore is a shallowRef
333-
const setupStoreProperty = setupStoreRef[key]
334-
if (
335-
isShallowRef(setupStoreProperty) &&
336-
isPlainObject(partialStateOrMutator[key])
337-
) {
338-
shallowRefsToTrigger.push(setupStoreProperty)
339-
}
328+
if (!Object.prototype.hasOwnProperty.call(partialStateOrMutator, key))
329+
continue
330+
const prop = (rawStore as any)[key]
331+
if (
332+
isShallowRef(prop) &&
333+
isPlainObject((partialStateOrMutator as any)[key])
334+
) {
335+
shallowRefsToTrigger.push(prop)
340336
}
341337
}
342-
343-
// Trigger reactivity for all shallowRefs that were patched
344338
shallowRefsToTrigger.forEach(triggerRef)
345339
}
346340

@@ -531,8 +525,7 @@ function createSetupStore<
531525
pinia._e.run(() => (scope = effectScope()).run(() => setup({ action }))!)
532526
)!
533527

534-
// Set setupStore reference for shallowRef handling in $patch
535-
setupStoreRef = setupStore
528+
// no-op: `$patch` inspects refs via `toRaw(store)`
536529

537530
// overwrite existing actions to support $onAction
538531
for (const key in setupStore) {

0 commit comments

Comments
 (0)