File tree 2 files changed +22
-4
lines changed
2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -1006,9 +1006,27 @@ describe('reactivity/computed', () => {
1006
1006
expect ( serializeInner ( root ) ) . toBe ( `<button>Step</button><p>Step 2</p>` )
1007
1007
} )
1008
1008
1009
- it ( 'manual trigger computed' , ( ) => {
1009
+ test ( 'manual trigger computed' , ( ) => {
1010
1010
const cValue = computed ( ( ) => 1 )
1011
1011
triggerRef ( cValue )
1012
1012
expect ( cValue . value ) . toBe ( 1 )
1013
1013
} )
1014
+
1015
+ test ( 'computed should remain live after losing all subscribers' , ( ) => {
1016
+ const toggle = ref ( true )
1017
+ const state = reactive ( {
1018
+ a : 1 ,
1019
+ } )
1020
+ const p = computed ( ( ) => state . a + 1 )
1021
+ const pp = computed ( ( ) => {
1022
+ return toggle . value ? p . value : 111
1023
+ } )
1024
+
1025
+ const { effect : e } = effect ( ( ) => pp . value )
1026
+ e . stop ( )
1027
+
1028
+ expect ( p . value ) . toBe ( 2 )
1029
+ state . a ++
1030
+ expect ( p . value ) . toBe ( 3 )
1031
+ } )
1014
1032
} )
Original file line number Diff line number Diff line change @@ -399,7 +399,7 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
399
399
}
400
400
}
401
401
402
- function removeSub ( link : Link ) {
402
+ function removeSub ( link : Link , fromComputed = false ) {
403
403
const { dep, prevSub, nextSub } = link
404
404
if ( prevSub ) {
405
405
prevSub . nextSub = nextSub
@@ -425,9 +425,9 @@ function removeSub(link: Link) {
425
425
// value can be GCed
426
426
dep . computed . flags &= ~ EffectFlags . TRACKING
427
427
for ( let l = dep . computed . deps ; l ; l = l . nextDep ) {
428
- removeSub ( l )
428
+ removeSub ( l , true )
429
429
}
430
- } else if ( dep . map ) {
430
+ } else if ( dep . map && ! fromComputed ) {
431
431
// property dep, remove it from the owner depsMap
432
432
dep . map . delete ( dep . key )
433
433
if ( ! dep . map . size ) targetMap . delete ( dep . target ! )
You can’t perform that action at this time.
0 commit comments