import { Ref , ReactiveEffect , ref , stop } from '@vue/reactivity' ; import { invokeLifeCycle } from './lifecycle' ; import { InstanceStateMap , InternalInstanceState , LifecycleHooks } from './types' ; /**
 * When `reactivue` dependency gets updated during development
 * your build tool re-executes it and `_vueState` becomes its
 * initial state. Storing our reactive effects in `window.__reactivue_state`
 * and filling our `_vueState` with it.
 */ declare global { interface Window { __reactivue_state : InstanceStateMap ; __reactivue_id : number ; } } let _id = ( __DEV__ && __BROWSER__ && window . __reactivue_id ) || 0 ; const _vueState = ( __DEV__ && __BROWSER__ && window . __reactivue_state ) || { } ; if ( __DEV__ && __BROWSER__ ) window . __reactivue_state = _vueState ; export let currentInstance = null ; export let currentInstanceId = null ; export const getNewInstanceId = ( ) => { if ( __DEV__ ) { Object . keys ( _vueState ) . forEach ( ( id ) => { setTimeout ( ( ) => { if ( _vueState [ + id ] ?. isActive === false ) unmount ( + id ) ; } , 0 ) ; } ) ; } _id ++ ; if ( __DEV__ && __BROWSER__ ) window . __reactivue_id = _id ; return _id ; } ; export const getCurrentInstance = ( ) => currentInstance ; export const setCurrentInstance = ( instance ) => { currentInstance = instance ; } ; export const setCurrentInstanceId = ( id ) => { currentInstanceId = id ; currentInstance = id != null ? ( _vueState [ id ] || null ) : null ; return currentInstance ; } ; export const createNewInstanceWithId = ( id , props , data = ref ( null ) ) => { const instance = { _id : id , props , data , isActive : false , isUnmounted : false , isUnmounting : false , hooks : { } , initialState : { } , provides : __BROWSER__ ? { ... window . __reactivue_context ?. provides } : { } , } ; _vueState [ id ] = instance ; return instance ; } ; export const useInstanceScope = ( id , cb ) => { const prev = currentInstanceId ; const instance = setCurrentInstanceId ( id ) ; cb ( instance ) ; setCurrentInstanceId ( prev ) ; } ; const unmount = ( id ) => { invokeLifeCycle ( LifecycleHooks . BEFORE_UNMOUNT , _vueState [ id ] ) ; for ( const effect of _vueState [ id ] . effects || [ ] ) stop ( effect ) ; invokeLifeCycle ( LifecycleHooks . UNMOUNTED , _vueState [ id ] ) ; _vueState [ id ] . isUnmounted = true ; delete _vueState [ id ] ; } ; export const unmountInstance = ( id ) => { if ( ! _vueState [ id ] ) return ; _vueState [ id ] . isUnmounting = true ; /**
     * Postpone unmounting on dev. So we can check setup values
     * in useSetup.ts after hmr updates. That will not be an issue
     * for really unmounting components. Because they are increasing
     * instance id unlike the hmr updated components.
     */ if ( __DEV__ ) setTimeout ( ( ) => _vueState [ id ] ?. isUnmounting && unmount ( id ) , 0 ) ; else unmount ( id ) ; } ; export function recordInstanceBoundEffect ( effect ) { if ( currentInstance ) { if ( ! currentInstance . effects ) currentInstance . effects = [ ] ; currentInstance . effects . push ( effect ) ; } }