1
- import { createApp , customRef , isReactive , isRef , toRaw , triggerRef } from 'vue'
1
+ import { computed , createApp , isReactive , isRef , toRaw , triggerRef } from 'vue'
2
2
import type { App , ComputedRef , WritableComputedRef } from 'vue'
3
3
import {
4
4
Pinia ,
9
9
_DeepPartial ,
10
10
PiniaPluginContext ,
11
11
} from 'pinia'
12
+ // NOTE: the implementation type is correct and contains up to date types
13
+ // while the other types hide internal properties
14
+ import type { ComputedRefImpl } from '@vue/reactivity'
12
15
13
16
export interface TestingOptions {
14
17
/**
@@ -206,7 +209,7 @@ function isPlainObject(
206
209
207
210
function isComputed < T > (
208
211
v : ComputedRef < T > | WritableComputedRef < T > | unknown
209
- ) : v is ComputedRef < T > | WritableComputedRef < T > {
212
+ ) : v is ( ComputedRef < T > | WritableComputedRef < T > ) & ComputedRefImpl < T > {
210
213
return ! ! v && isRef ( v ) && 'effect' in v
211
214
}
212
215
@@ -215,36 +218,33 @@ function WritableComputed({ store }: PiniaPluginContext) {
215
218
for ( const key in rawStore ) {
216
219
const originalComputed = rawStore [ key ]
217
220
if ( isComputed ( originalComputed ) ) {
218
- const originalFn = originalComputed . effect . fn
219
- rawStore [ key ] = customRef ( ( track , trigger ) => {
220
- // override the computed with a new one
221
- const overriddenFn = ( ) =>
222
- // @ts -expect-error: internal value
223
- originalComputed . _value
224
- // originalComputed.effect.fn = overriddenFn
225
- return {
226
- get : ( ) => {
227
- track ( )
228
- return originalComputed . value
229
- } ,
230
- set : ( newValue ) => {
231
- // reset the computed to its original value by setting it to its initial state
232
- if ( newValue === undefined ) {
233
- originalComputed . effect . fn = originalFn
234
- // @ts -expect-error: private api to remove the current cached value
235
- delete originalComputed . _value
236
- // @ts -expect-error: private api to force the recomputation
237
- originalComputed . _dirty = true
238
- } else {
239
- originalComputed . effect . fn = overriddenFn
240
- // @ts -expect-error: private api
241
- originalComputed . _value = newValue
242
- }
243
- // this allows to trigger the original computed in setup stores
244
- triggerRef ( originalComputed )
245
- trigger ( )
246
- } ,
247
- }
221
+ const originalFn = originalComputed . fn
222
+ // override the computed with a new one
223
+ const overriddenFn = ( ) =>
224
+ // @ts -expect-error: internal cached value
225
+ originalComputed . _value
226
+ // originalComputed.fn = overriddenFn
227
+
228
+ rawStore [ key ] = computed < unknown > ( {
229
+ get ( ) {
230
+ return originalComputed . value
231
+ } ,
232
+ set ( newValue ) {
233
+ // reset the computed to its original value by setting it to its initial state
234
+ if ( newValue === undefined ) {
235
+ originalComputed . fn = originalFn
236
+ // @ts -expect-error: private api to remove the current cached value
237
+ delete originalComputed . _value
238
+ // @ts -expect-error: private api to force the recomputation
239
+ originalComputed . _dirty = true
240
+ } else {
241
+ originalComputed . fn = overriddenFn
242
+ // @ts -expect-error: private api
243
+ originalComputed . _value = newValue
244
+ }
245
+ // this allows to trigger the original computed in setup stores
246
+ triggerRef ( originalComputed )
247
+ } ,
248
248
} )
249
249
}
250
250
}
0 commit comments