File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -1006,9 +1006,27 @@ describe('reactivity/computed', () => {
10061006 expect ( serializeInner ( root ) ) . toBe ( `<button>Step</button><p>Step 2</p>` )
10071007 } )
10081008
1009- it ( 'manual trigger computed' , ( ) => {
1009+ test ( 'manual trigger computed' , ( ) => {
10101010 const cValue = computed ( ( ) => 1 )
10111011 triggerRef ( cValue )
10121012 expect ( cValue . value ) . toBe ( 1 )
10131013 } )
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+ } )
10141032} )
Original file line number Diff line number Diff line change @@ -399,7 +399,7 @@ export function refreshComputed(computed: ComputedRefImpl): undefined {
399399 }
400400}
401401
402- function removeSub ( link : Link ) {
402+ function removeSub ( link : Link , fromComputed = false ) {
403403 const { dep, prevSub, nextSub } = link
404404 if ( prevSub ) {
405405 prevSub . nextSub = nextSub
@@ -425,9 +425,9 @@ function removeSub(link: Link) {
425425 // value can be GCed
426426 dep . computed . flags &= ~ EffectFlags . TRACKING
427427 for ( let l = dep . computed . deps ; l ; l = l . nextDep ) {
428- removeSub ( l )
428+ removeSub ( l , true )
429429 }
430- } else if ( dep . map ) {
430+ } else if ( dep . map && ! fromComputed ) {
431431 // property dep, remove it from the owner depsMap
432432 dep . map . delete ( dep . key )
433433 if ( ! dep . map . size ) targetMap . delete ( dep . target ! )
You can’t perform that action at this time.
0 commit comments