@@ -43,12 +43,12 @@ export abstract class BaseAccessor<S, Arg, D, E> {
4343 private removeOnReconnectListener : ( ( ) => void ) | null = null ;
4444 private removeOnVisibilityChangeListener : ( ( ) => void ) | null = null ;
4545 private pollingTimeoutId : number | undefined ;
46- private isStale = false ;
4746 private onMount : ( ) => void ;
4847 private onUnmount : ( ) => void ;
4948 private autoListeners : ( ( ) => void ) [ ] = [ ] ;
5049 private removeAllListeners : ( ( ) => void ) | null = null ;
5150 private isAuto : boolean ;
51+ private setStaleTime : ( staleTime : number ) => void ;
5252
5353 /**
5454 * Return the result of the revalidation.
@@ -61,6 +61,7 @@ export abstract class BaseAccessor<S, Arg, D, E> {
6161 * Get the state of the corresponding model.
6262 */
6363 getState : ( serverStateKey ?: object ) => S ;
64+ isStale : ( ) => boolean ;
6465
6566 /**
6667 * @internal
@@ -74,10 +75,9 @@ export abstract class BaseAccessor<S, Arg, D, E> {
7475 arg,
7576 isAuto,
7677 creatorName,
77- } : Pick <
78- BaseConstructorArgs < S , Arg > ,
79- 'getState' | 'modelSubscribe' | 'onMount' | 'onUnmount' | 'arg' | 'notifyModel' | 'isAuto'
80- > & { creatorName : string } ) {
78+ setStaleTime,
79+ getIsStale,
80+ } : Omit < BaseConstructorArgs < S , Arg > , 'updateState' > & { creatorName : string } ) {
8181 this . getState = getState ;
8282 this . modelSubscribe = modelSubscribe ;
8383 this . onMount = onMount ;
@@ -86,6 +86,8 @@ export abstract class BaseAccessor<S, Arg, D, E> {
8686 this . arg = arg ;
8787 this . isAuto = isAuto ;
8888 this . creatorName = creatorName ;
89+ this . isStale = getIsStale ;
90+ this . setStaleTime = setStaleTime ;
8991 }
9092
9193 getIsAuto = ( ) => {
@@ -153,17 +155,8 @@ export abstract class BaseAccessor<S, Arg, D, E> {
153155 return this . status ;
154156 } ;
155157
156- /**
157- * Get whether this accessor is stale or not.
158- *
159- * @internal
160- */
161- getIsStale = ( ) => {
162- return this . isStale ;
163- } ;
164-
165158 invalidate = ( ) => {
166- this . isStale = true ;
159+ this . setStaleTime ( 0 ) ;
167160 if ( this . isMounted ( ) ) {
168161 this . revalidate ( ) ;
169162 }
@@ -272,12 +265,12 @@ export abstract class BaseAccessor<S, Arg, D, E> {
272265 error,
273266 data,
274267 } : OnFetchingFinishContext < D , E > ) : FetchPromiseResult < E , D > => {
275- const { pollingInterval } = this . getOptions ( ) ;
268+ const { pollingInterval, staleTime } = this . getOptions ( ) ;
276269 if ( pollingInterval > 0 ) {
277270 this . pollingTimeoutId = window . setTimeout ( this . invokePollingRevalidation , pollingInterval ) ;
278271 }
279272
280- this . isStale = true ;
273+ this . setStaleTime ( staleTime ) ;
281274 if ( error ) {
282275 this . action . onError ?.( { error, arg : this . arg } ) ;
283276 this . updateStatus ( { isFetching : false , error } ) ;
0 commit comments