Skip to content
Merged
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: 11 additions & 4 deletions packages/reactivity/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ export class ReactiveEffect<T = any>
flags: number = ReactiveFlags.Watching | ReactiveFlags.Dirty

/**
* Allocated by the first cleanup registered.
* @internal
*/
cleanups: (() => void)[] = []
cleanups: (() => void)[] | undefined = undefined
/**
* @internal
*/
Expand Down Expand Up @@ -277,12 +278,16 @@ export function resetTracking(): void {
}

export function cleanup(
sub: ReactiveNode & { cleanups: (() => void)[]; cleanupsLength: number },
sub: ReactiveNode & {
cleanups: (() => void)[] | undefined
cleanupsLength: number
},
): void {
const l = sub.cleanupsLength
if (l) {
const cleanups = sub.cleanups!
for (let i = 0; i < l; i++) {
sub.cleanups[i]()
cleanups[i]()
}
sub.cleanupsLength = 0
}
Expand All @@ -302,7 +307,9 @@ export function cleanup(
*/
export function onEffectCleanup(fn: () => void, failSilently = false): void {
if (activeSub instanceof ReactiveEffect) {
activeSub.cleanups[activeSub.cleanupsLength++] = () => cleanupEffect(fn)
;(activeSub.cleanups || (activeSub.cleanups = []))[
activeSub.cleanupsLength++
] = () => cleanupEffect(fn)
} else if (__DEV__ && !failSilently) {
warn(
`onEffectCleanup() was called when there was no active effect` +
Expand Down
7 changes: 5 additions & 2 deletions packages/reactivity/src/effectScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export class EffectScope implements ReactiveNode {
flags: number = 0

/**
* Allocated by the first cleanup registered.
* @internal
*/
cleanups: (() => void)[] = []
cleanups: (() => void)[] | undefined = undefined
/**
* @internal
*/
Expand Down Expand Up @@ -148,7 +149,9 @@ export function setCurrentScope(scope?: EffectScope): EffectScope | undefined {
export function onScopeDispose(fn: () => void, failSilently = false): void {
if (activeEffectScope !== undefined) {
if (activeEffectScope.active) {
activeEffectScope.cleanups[activeEffectScope.cleanupsLength++] = fn
;(activeEffectScope.cleanups || (activeEffectScope.cleanups = []))[
activeEffectScope.cleanupsLength++
] = fn
} else {
// The scope has already been stopped and will not run its cleanups
// again, e.g. a component's async setup resumed after it was unmounted.
Expand Down
3 changes: 2 additions & 1 deletion packages/reactivity/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ export function onWatcherCleanup(
if (owner.cb) {
;(owner.cbCleanups || (owner.cbCleanups = [])).push(cleanup)
} else {
owner.cleanups[owner.cleanupsLength++] = cleanup
;(owner.cleanups || (owner.cleanups = []))[owner.cleanupsLength++] =
cleanup
}
} else if (__DEV__ && !failSilently) {
warn(
Expand Down
Loading