export function removeElementFromArray < T > ( array , elem ) { const index = array . indexOf ( elem ) ; if ( index === - 1 ) { return ; } array . splice ( array . indexOf ( elem ) , 1 ) ; } export interface Type < T > extends Function { new ( ... args ) : T ; } type Callback < T > = ( value : T ) => void ; const resizeListenersMap = new WeakMap < Element , Callback < ResizeObserverEntry > [ ] > ( ) ; const observedElements = new Set < Element > ( ) ; let observer ; function getObserver ( ) { if ( observer ) { return observer ; } const newObserver = new ResizeObserver ( entries => { for ( const entry of entries ) { const listeners = resizeListenersMap . get ( entry . target ) ; if ( ! listeners ) { continue ; } listeners . forEach ( callback => { callback ( entry ) ; } ) ; } } ) ; observer = newObserver ; return newObserver ; } export function observeElementResize ( element , callback ) { if ( ! ResizeObserver ) { return ; } const currentObservers = resizeListenersMap . get ( element ) ?? [ ] ; currentObservers . push ( callback ) ; resizeListenersMap . set ( element , currentObservers ) ; if ( ! observedElements . has ( element ) ) { getObserver ( ) . observe ( element ) ; } return function cancel ( ) { removeElementFromArray ( currentObservers , callback ) ; if ( currentObservers . length === 0 ) { getObserver ( ) . unobserve ( element ) ; observedElements . delete ( element ) ; } } ; }