import { createAtom } from "./mobxShim" ; import { autorunThrottled } from "./autorunThrottled" ; param compute param delay param name export function throttledComputed < T > ( compute , delay , name ? ) { "use strict" ; let monitor ; let latestValue ; let latestError ; function wake ( ) { sleep ( ) ; monitor = autorunThrottled ( observe , delay , name ) ; } function observe ( ) { try { const newValue = compute ( ) ; if ( latestError || newValue !== latestValue ) { latestValue = newValue ; latestError = undefined ; atom . reportChanged ( ) ; } } catch ( x ) { latestError = x ; atom . reportChanged ( ) ; } } function sleep ( ) { const dispose = monitor ; monitor = undefined ; if ( dispose ) { dispose ( ) ; } } const atom = createAtom ( name || "DelayedComputedAtom" , wake , sleep ) ; return { get ( ) { atom . reportObserved ( ) ; if ( latestError ) { throw latestError ; } return latestValue ! ; } , refresh ( ) { wake ( ) ; } } ; }