@@ -19,8 +19,6 @@ import {
1919 makeObservable ,
2020 observable ,
2121 runInAction ,
22- onBecomeUnobserved ,
23- onBecomeObserved ,
2422} from 'mobx' ;
2523
2624import {
@@ -35,6 +33,7 @@ import {
3533 InfiniteQueryUpdateOptionsAllVariants ,
3634} from './inifinite-query.types' ;
3735import { AnyQueryClient , QueryClientHooks } from './query-client.types' ;
36+ import { lazyObserve } from './utils/lazy-observe' ;
3837
3938const enableHolder = ( ) => false ;
4039
@@ -231,41 +230,36 @@ export class InfiniteQuery<
231230 this . updateResult ( this . queryObserver . getOptimisticResult ( this . options ) ) ;
232231
233232 if ( this . isLazy ) {
234- let dynamicOptionsDisposeFn : VoidFunction | undefined ;
235-
236- onBecomeObserved ( this , '_result' , ( ) => {
237- if ( ! this . _observerSubscription ) {
238- if ( getAllDynamicOptions ) {
239- this . update ( getAllDynamicOptions ( ) ) ;
240- }
241- this . _observerSubscription = this . queryObserver . subscribe (
242- this . updateResult ,
243- ) ;
244- if ( getAllDynamicOptions ) {
245- dynamicOptionsDisposeFn = reaction (
246- getAllDynamicOptions ,
247- this . update ,
248- {
233+ const cleanup = lazyObserve ( {
234+ context : this ,
235+ property : '_result' ,
236+ onStart : ( ) => {
237+ if ( ! this . _observerSubscription ) {
238+ if ( getAllDynamicOptions ) {
239+ this . update ( getAllDynamicOptions ( ) ) ;
240+ }
241+ this . _observerSubscription = this . queryObserver . subscribe (
242+ this . updateResult ,
243+ ) ;
244+ if ( getAllDynamicOptions ) {
245+ return reaction ( getAllDynamicOptions , this . update , {
249246 delay : this . config . dynamicOptionsUpdateDelay ,
250247 signal : config . abortSignal ,
251248 fireImmediately : true ,
252- } ,
253- ) ;
249+ } ) ;
250+ }
254251 }
255- }
252+ } ,
253+ onEnd : ( disposeFn , cleanup ) => {
254+ if ( this . _observerSubscription ) {
255+ disposeFn ?.( ) ;
256+ this . _observerSubscription ( ) ;
257+ this . _observerSubscription = undefined ;
258+ config . abortSignal ?. removeEventListener ( 'abort' , cleanup ) ;
259+ }
260+ } ,
256261 } ) ;
257262
258- const cleanup = ( ) => {
259- if ( this . _observerSubscription ) {
260- dynamicOptionsDisposeFn ?.( ) ;
261- this . _observerSubscription ( ) ;
262- this . _observerSubscription = undefined ;
263- dynamicOptionsDisposeFn = undefined ;
264- config . abortSignal ?. removeEventListener ( 'abort' , cleanup ) ;
265- }
266- } ;
267-
268- onBecomeUnobserved ( this , '_result' , cleanup ) ;
269263 config . abortSignal ?. addEventListener ( 'abort' , cleanup ) ;
270264 } else {
271265 if ( isQueryKeyDynamic ) {
0 commit comments