Last active
March 29, 2021 15:17
-
-
Save charlypoly/4f420378a7652918ad018ef26c339361 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NetworkStatus, isNetworkRequestInFlight } from 'apollo-client/core/networkStatus'; | |
type isNetworkTransitionArg = { | |
networkStatus: NetworkStatus; | |
}; | |
export enum NetworkTransition { | |
LOADED_DATA, | |
WAS_IN_CACHE, | |
LOADING, | |
REFETCHING, | |
REFETCHED, | |
FETCHING_MORE | |
} | |
export const networkTransitionIs = ( | |
prev: isNetworkTransitionArg, | |
current: isNetworkTransitionArg, | |
transition: NetworkTransition | |
) => { | |
switch (transition) { | |
case NetworkTransition.WAS_IN_CACHE: | |
return ( | |
current.networkStatus === NetworkStatus.ready && | |
prev.networkStatus === NetworkStatus.ready | |
); | |
case NetworkTransition.LOADED_DATA: | |
return ( | |
current.networkStatus === NetworkStatus.ready && | |
isNetworkRequestInFlight(prev.networkStatus) | |
); | |
case NetworkTransition.REFETCHING: | |
return ( | |
current.networkStatus === NetworkStatus.refetch | |
); | |
case NetworkTransition.REFETCHED: | |
return ( | |
current.networkStatus === NetworkStatus.ready && | |
prev.networkStatus === NetworkStatus.refetch | |
); | |
case NetworkTransition.FETCHING_MORE: | |
return ( | |
current.networkStatus === NetworkStatus.ready && | |
prev.networkStatus === NetworkStatus.fetchMore | |
); | |
case NetworkTransition.LOADING: | |
return ( | |
current.networkStatus === NetworkStatus.loading && | |
isNetworkRequestInFlight(prev.networkStatus) | |
); | |
default: | |
return false; | |
} | |
}; | |
export const isQueryLoading = ( | |
prev: isNetworkTransitionArg, | |
current: isNetworkTransitionArg | |
) => { | |
return networkTransitionIs(prev, current, NetworkTransition.LOADING); | |
}; | |
export const isQueryLoaded = ( | |
prev: isNetworkTransitionArg, | |
current: isNetworkTransitionArg, | |
{ from_cache = false }: { from_cache: boolean } | |
) => { | |
return networkTransitionIs( | |
prev, | |
current, | |
from_cache ? | |
NetworkTransition.WAS_IN_CACHE : | |
NetworkTransition.LOADED_DATA | |
); | |
}; | |
export const isQueryRefetching = ( | |
prev: isNetworkTransitionArg, | |
current: isNetworkTransitionArg | |
) => { | |
return networkTransitionIs(prev, current, NetworkTransition.REFETCHING); | |
}; | |
export const isQueryRefetched = ( | |
prev: isNetworkTransitionArg, | |
current: isNetworkTransitionArg | |
) => { | |
return networkTransitionIs(prev, current, NetworkTransition.REFETCHED); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment