Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(reactivity): no longer allows stop computed effect #11244

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions packages/reactivity/__tests__/computed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,6 @@ describe('reactivity/computed', () => {
expect(getter2).toHaveBeenCalledTimes(2)
})

it('should no longer update when stopped', () => {
const value = reactive<{ foo?: number }>({})
const cValue = computed(() => value.foo)
let dummy
effect(() => {
dummy = cValue.value
})
expect(dummy).toBe(undefined)
value.foo = 1
expect(dummy).toBe(1)
cValue.effect.stop()
value.foo = 2
expect(dummy).toBe(1)
})

it('should support setter', () => {
const n = ref(1)
const plusOne = computed({
Expand Down
16 changes: 0 additions & 16 deletions packages/reactivity/__tests__/deferredComputed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,4 @@ describe('deferred computed', () => {
c2.value
expect(effectSpy).toHaveBeenCalledTimes(2)
})

test('should not compute if deactivated before scheduler is called', () => {
const c1Spy = vi.fn()
const src = ref(0)
const c1 = computed(() => {
c1Spy()
return src.value % 2
})
effect(() => c1.value)
expect(c1Spy).toHaveBeenCalledTimes(1)

c1.effect.stop()
// trigger
src.value++
expect(c1Spy).toHaveBeenCalledTimes(1)
})
})
3 changes: 1 addition & 2 deletions packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ describe('reactivity/effect/scope', () => {
r.value++
c!.value
await nextTick()
// should not trigger anymore
expect(computedSpy).toHaveBeenCalledTimes(2)
expect(computedSpy).toHaveBeenCalledTimes(3)
expect(watchSpy).toHaveBeenCalledTimes(1)
expect(watchEffectSpy).toHaveBeenCalledTimes(2)
})
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class ReactiveEffect<T = any> {
}

stop() {
if (this.active) {
if (this.active && !this.computed) {
preCleanupEffect(this)
postCleanupEffect(this)
this.onStop && this.onStop()
Expand Down
35 changes: 0 additions & 35 deletions packages/runtime-core/__tests__/apiSetupHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {
type ComponentInternalInstance,
type ComputedRef,
type SetupContext,
Suspense,
computed,
createApp,
defineComponent,
getCurrentInstance,
Expand Down Expand Up @@ -419,38 +417,5 @@ describe('SFC <script setup> helpers', () => {
expect(uids.one.before).toBe(uids.one.after)
expect(uids.two.before).toBe(uids.two.after)
})

test('should teardown in-scope effects', async () => {
let resolve: (val?: any) => void
const ready = new Promise(r => {
resolve = r
})

let c: ComputedRef

const Comp = defineComponent({
async setup() {
let __temp: any, __restore: any
;[__temp, __restore] = withAsyncContext(() => Promise.resolve())
__temp = await __temp
__restore()

c = computed(() => {})
// register the lifecycle after an await statement
onMounted(resolve)
return () => ''
},
})

const app = createApp(() => h(Suspense, () => h(Comp)))
const root = nodeOps.createElement('div')
app.mount(root)

await ready
expect(c!.effect.active).toBe(true)

app.unmount()
expect(c!.effect.active).toBe(false)
})
})
})