Skip to content

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 authored and yyx990803 committed Nov 14, 2024
1 parent d2fd005 commit 0073a01
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/reactivity/__tests__/effectScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('reactivity/effect/scope', () => {

expect('[Vue warn] cannot run an inactive effect scope.').toHaveBeenWarned()

expect(scope.effects.length).toBe(1)
expect(scope.effects.length).toBe(0)

counter.num = 7
expect(dummy).toBe(0)
Expand Down
11 changes: 8 additions & 3 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,20 @@ export class EffectScope {
for (i = 0, l = effects.length; i < l; i++) {
effects[i].stop()
}
const cleanups = this.cleanups.slice()
for (i = 0, l = cleanups.length; i < l; i++) {
cleanups[i]()
this.effects.length = 0

for (i = 0, l = this.cleanups.length; i < l; i++) {
this.cleanups[i]()
}
this.cleanups.length = 0

if (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].stop(true)
}
this.scopes.length = 0
}

// nested scope, dereference from parent to avoid memory leaks
if (!this.detached && this.parent && !fromParent) {
// optimized O(1) removal
Expand Down
4 changes: 1 addition & 3 deletions packages/reactivity/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from './effect'
import { isReactive, isShallow } from './reactive'
import { type Ref, isRef } from './ref'
import { getCurrentScope, onScopeDispose } from './effectScope'
import { getCurrentScope } from './effectScope'

// These errors were transferred from `packages/runtime-core/src/errorHandling.ts`
// to @vue/reactivity to allow co-location with the moved base watch logic, hence
Expand Down Expand Up @@ -215,10 +215,8 @@ export function watch(
effect.stop()
if (scope && scope.active) {
remove(scope.effects, effect)
remove(scope.cleanups, watchHandle)
}
}
onScopeDispose(watchHandle, true)

if (once && cb) {
const _cb = cb
Expand Down
3 changes: 1 addition & 2 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from '@vue/runtime-test'
import {
type DebuggerEvent,
EffectFlags,
ITERATE_KEY,
type Ref,
type ShallowRef,
Expand Down Expand Up @@ -1341,7 +1340,7 @@ describe('api: watch', () => {
await nextTick()
await nextTick()

expect(instance!.scope.effects[0].flags & EffectFlags.ACTIVE).toBeFalsy()
expect(instance!.scope.effects.length).toBe(0)
})

test('this.$watch should pass `this.proxy` to watch source as the first argument ', () => {
Expand Down

0 comments on commit 0073a01

Please sign in to comment.