import { stop as _stop , ReactiveEffect , } from '@vue/reactivity' ; export interface EffectScope { ( fn ) : void ; _isEffectScope : true ; id : number ; active : boolean ; effects : ( ReactiveEffect | EffectScope ) [ ] ; } export interface EffectScopeOptions { detached ? : boolean ; } let uid = 0 ; const effectScopeStack = [ ] ; let activeEffectScope ; export function recordEffect ( effect ) { if ( activeEffectScope ?. active && effect ) activeEffectScope . effects . push ( effect ) ; } export function isEffect ( fn ) { return fn && fn . _isEffect === true ; } export function stop ( effect ) { if ( ! effect . active ) return ; if ( isEffect ( effect ) ) { _stop ( effect ) ; } else { effect . effects . forEach ( e => stop ( e ) ) ; effect . active = false ; } } function recordEffectScope ( effect ) { if ( activeEffectScope && activeEffectScope . active ) activeEffectScope . effects . push ( effect ) ; } export function effectScope ( fn ? , options = { } ) { const scope = function ( fn ) : unknown { if ( ! scope . active ) return ; try { if ( ! options . detached ) recordEffectScope ( scope ) ; effectScopeStack . push ( scope ) ; activeEffectScope = scope ; fn ( ) ; return scope ; } finally { effectScopeStack . pop ( ) ; activeEffectScope = effectScopeStack [ effectScopeStack . length - 1 ] ; } } as EffectScope ; scope . _isEffectScope = true ; scope . id = uid ++ ; scope . active = true ; scope . effects = [ ] ; if ( fn ) scope ( fn ) ; return scope ; }