import { useEffect , useState } from "react" ; type DisposableFunction = Function ; export function createDisposables ( ) { const disposables = [ ] ; const api = { add ( callback ) { disposables . push ( callback ) ; } , dispose ( ) { disposables . splice ( 0 ) . forEach ( dispose => dispose ( ) ) ; } , nextFrame ( ... args ) { api . requestAnimationFrame ( ... args ) ; } , requestAnimationFrame ( ... args ) { const id = requestAnimationFrame ( ... args ) ; api . add ( ( ) => cancelAnimationFrame ( id ) ) ; } , setTimeout ( handler , timeout ? , ... args ) { const timer = setTimeout ( handler , timeout , ... args ) ; api . add ( ( ) => clearTimeout ( timer ) ) ; } } ; return api ; } export function useDisposables ( ) { const [ d ] = useState ( createDisposables ) ; useEffect ( ( ) => { return ( ) => { d . dispose ( ) ; } ; } , [ d ] ) ; return d ; }