Skip to content

Commit f44ef0b

Browse files
committed
feat: implement getCurrentEffect
1 parent a078ad1 commit f44ef0b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

packages/reactivity/src/baseWatch.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,31 @@ export type HandleWarn = (msg: string, ...args: any[]) => void
105105
const cleanupMap: WeakMap<ReactiveEffect, (() => void)[]> = new WeakMap()
106106
let activeEffect: ReactiveEffect | undefined = undefined
107107

108+
/**
109+
* Returns the current active effect if there is one.
110+
*/
111+
export function getCurrentEffect() {
112+
return activeEffect
113+
}
114+
115+
/**
116+
* Registers a cleanup callback on the current active effect. This
117+
* registered cleanup callback will be invoked right before the
118+
* associated effect re-runs.
119+
*
120+
* @param cleanupFn - The callback function to attach to the effect's cleanup.
121+
*/
108122
export function onEffectCleanup(cleanupFn: () => void) {
109123
if (activeEffect) {
110124
const cleanups =
111125
cleanupMap.get(activeEffect) ||
112126
cleanupMap.set(activeEffect, []).get(activeEffect)!
113127
cleanups.push(cleanupFn)
128+
} else if (__DEV__) {
129+
warn(
130+
`onEffectCleanup() was called when there was no active effect` +
131+
` to associate with.`,
132+
)
114133
}
115134
}
116135

0 commit comments

Comments
 (0)