@@ -89,6 +89,12 @@ export const useClient: UseClient = (
8989 /**
9090 * Refs
9191 */
92+ type InterserctionObserverCallback = (
93+ entries : IntersectionObserverEntry [ ]
94+ ) => void ;
95+ const intersectionObserverCallback = useRef <
96+ InterserctionObserverCallback | undefined
97+ > ( ) ;
9298 const intersectionObserver = useRef < IntersectionObserver | null > ( null ) ;
9399 const lazyAnchorRef = useRef < HTMLDivElement > ( null ) ;
94100 const registeredIframes = useRef <
@@ -233,6 +239,14 @@ export const useClient: UseClient = (
233239 setState ( ( prev ) => ( { ...prev , error : null , status : "running" } ) ) ;
234240 } , [ createClient ] ) ;
235241
242+ intersectionObserverCallback . current = (
243+ entries : IntersectionObserverEntry [ ]
244+ ) : void => {
245+ if ( entries . some ( ( entry ) => entry . isIntersecting ) ) {
246+ runSandpack ( ) ;
247+ }
248+ } ;
249+
236250 const initializeSandpackIframe = useCallback ( ( ) : void => {
237251 const autorun = options ?. autorun ?? true ;
238252
@@ -252,23 +266,14 @@ export const useClient: UseClient = (
252266 // If any component registered a lazy anchor ref component, use that for the intersection observer
253267 intersectionObserver . current = new IntersectionObserver ( ( entries ) => {
254268 if ( entries . some ( ( entry ) => entry . isIntersecting ) ) {
255- runSandpack ( ) ;
256-
257- if ( lazyAnchorRef . current ) {
258- intersectionObserver . current ?. unobserve ( lazyAnchorRef . current ) ;
259- }
269+ intersectionObserverCallback . current ?.( entries ) ;
260270 }
261271 } , observerOptions ) ;
262272
263273 intersectionObserver . current . observe ( lazyAnchorRef . current ) ;
264274 } else if ( lazyAnchorRef . current && state . initMode === "user-visible" ) {
265275 intersectionObserver . current = new IntersectionObserver ( ( entries ) => {
266- if ( entries . some ( ( entry ) => entry . isIntersecting ) ) {
267- runSandpack ( ) ;
268- } else {
269- Object . keys ( clients . current ) . map ( unregisterBundler ) ;
270- unregisterAllClients ( ) ;
271- }
276+ intersectionObserverCallback . current ?.( entries ) ;
272277 } , observerOptions ) ;
273278
274279 intersectionObserver . current . observe ( lazyAnchorRef . current ) ;
@@ -371,7 +376,7 @@ export const useClient: UseClient = (
371376 } ;
372377
373378 const recompileMode = options ?. recompileMode ?? "delayed" ;
374- const recompileDelay = options ?. recompileDelay ?? 500 ;
379+ const recompileDelay = options ?. recompileDelay ?? 200 ;
375380
376381 const dispatchMessage = (
377382 message : SandpackMessage ,
0 commit comments