Skip to content

Commit 51a91c0

Browse files
committed
refactor: simplify
1 parent ec904ad commit 51a91c0

File tree

1 file changed

+26
-40
lines changed

1 file changed

+26
-40
lines changed

packages/pinia/src/store.ts

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,34 @@ import {
55
hasInjectionContext,
66
getCurrentInstance,
77
reactive,
8-
DebuggerEvent,
9-
WatchOptions,
10-
UnwrapRef,
118
markRaw,
129
isRef,
1310
isReactive,
1411
isShallow,
1512
effectScope,
16-
EffectScope,
17-
ComputedRef,
1813
toRaw,
1914
toRef,
2015
toRefs,
21-
Ref,
2216
ref,
2317
nextTick,
2418
triggerRef,
19+
type DebuggerEvent,
20+
type WatchOptions,
21+
type UnwrapRef,
22+
type EffectScope,
23+
type ComputedRef,
24+
type ShallowRef,
25+
type Ref,
2526
} from 'vue'
26-
import {
27+
import type {
28+
_DeepPartial,
2729
StateTree,
2830
SubscriptionCallback,
29-
_DeepPartial,
30-
isPlainObject,
3131
Store,
3232
_Method,
3333
DefineStoreOptions,
3434
StoreDefinition,
3535
_GettersTree,
36-
MutationType,
3736
StoreOnActionListener,
3837
_ActionsTree,
3938
SubscriptionCallbackMutation,
@@ -48,7 +47,13 @@ import {
4847
_ExtractStateFromSetupStore,
4948
_StoreWithState,
5049
} from './types'
51-
import { setActivePinia, piniaSymbol, Pinia, activePinia } from './rootStore'
50+
import { isPlainObject, MutationType } from './types'
51+
import {
52+
setActivePinia,
53+
piniaSymbol,
54+
type Pinia,
55+
activePinia,
56+
} from './rootStore'
5257
import { IS_CLIENT } from './env'
5358
import { patchObject } from './hmr'
5459
import { addSubscription, triggerSubscriptions, noop } from './subscriptions'
@@ -88,11 +93,14 @@ function mergeReactiveObjects<
8893
patchToApply.forEach(target.add, target)
8994
}
9095

96+
// the raw version lets us see shallow refs
97+
const rawTarget = toRaw(target)
98+
9199
// no need to go through symbols because they cannot be serialized anyway
92100
for (const key in patchToApply) {
93101
if (!patchToApply.hasOwnProperty(key)) continue
94-
const subPatch = patchToApply[key]
95-
const targetValue = target[key]
102+
var subPatch = patchToApply[key]
103+
var targetValue = target[key]
96104

97105
if (
98106
isPlainObject(targetValue) &&
@@ -109,6 +117,11 @@ function mergeReactiveObjects<
109117
// @ts-expect-error: subPatch is a valid value
110118
target[key] = subPatch
111119
}
120+
121+
// enables $patching shallow refs
122+
if (isShallow(rawTarget[key])) {
123+
triggerRef(rawTarget[key] as ShallowRef)
124+
}
112125
}
113126

114127
return target
@@ -149,15 +162,6 @@ function isComputed(o: any): o is ComputedRef {
149162
return !!(isRef(o) && (o as any).effect)
150163
}
151164

152-
/**
153-
* Checks if a value is a shallowRef
154-
* @param value - value to check
155-
* @returns true if the value is a shallowRef
156-
*/
157-
function isShallowRef(value: any): value is Ref {
158-
return isRef(value) && isShallow(value)
159-
}
160-
161165
function createOptionsStore<
162166
Id extends string,
163167
S extends StateTree,
@@ -321,24 +325,6 @@ function createSetupStore<
321325
} else {
322326
mergeReactiveObjects(pinia.state.value[$id], partialStateOrMutator)
323327

324-
// Handle shallowRef reactivity: inspect raw store to avoid ref unwrapping
325-
{
326-
const rawStore = toRaw(store) as Record<string, unknown>
327-
const shallowRefsToTrigger: Ref[] = []
328-
for (const key in partialStateOrMutator) {
329-
if (!Object.prototype.hasOwnProperty.call(partialStateOrMutator, key))
330-
continue
331-
const prop = (rawStore as any)[key]
332-
if (
333-
isShallowRef(prop) &&
334-
isPlainObject((partialStateOrMutator as any)[key])
335-
) {
336-
shallowRefsToTrigger.push(prop)
337-
}
338-
}
339-
shallowRefsToTrigger.forEach(triggerRef)
340-
}
341-
342328
subscriptionMutation = {
343329
type: MutationType.patchObject,
344330
payload: partialStateOrMutator,

0 commit comments

Comments
 (0)