(wip) react query subscribe on focus
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
diff --git a/node_modules/@react-navigation/core/lib/module/index.js b/node_modules/@react-navigation/core/lib/module/index.js
|
||||||
|
index dc83d8c..b707811 100644
|
||||||
|
--- a/node_modules/@react-navigation/core/lib/module/index.js
|
||||||
|
+++ b/node_modules/@react-navigation/core/lib/module/index.js
|
||||||
|
@@ -23,7 +23,7 @@ export { ThemeProvider } from "./theming/ThemeProvider.js";
|
||||||
|
export { useTheme } from "./theming/useTheme.js";
|
||||||
|
export * from "./types.js";
|
||||||
|
export { useFocusEffect } from "./useFocusEffect.js";
|
||||||
|
-export { useIsFocused } from "./useIsFocused.js";
|
||||||
|
+export { IsFocusedContext, useIsFocused } from "./useIsFocused.js";
|
||||||
|
export { useNavigation } from "./useNavigation.js";
|
||||||
|
export { useNavigationBuilder } from "./useNavigationBuilder.js";
|
||||||
|
export { useNavigationContainerRef } from "./useNavigationContainerRef.js";
|
||||||
|
diff --git a/node_modules/@react-navigation/core/lib/typescript/src/index.d.ts b/node_modules/@react-navigation/core/lib/typescript/src/index.d.ts
|
||||||
|
index 220e113..1b406cf 100644
|
||||||
|
--- a/node_modules/@react-navigation/core/lib/typescript/src/index.d.ts
|
||||||
|
+++ b/node_modules/@react-navigation/core/lib/typescript/src/index.d.ts
|
||||||
|
@@ -21,7 +21,7 @@ export { ThemeProvider } from './theming/ThemeProvider';
|
||||||
|
export { useTheme } from './theming/useTheme';
|
||||||
|
export * from './types';
|
||||||
|
export { useFocusEffect } from './useFocusEffect';
|
||||||
|
-export { useIsFocused } from './useIsFocused';
|
||||||
|
+export { IsFocusedContext, useIsFocused } from './useIsFocused';
|
||||||
|
export { useNavigation } from './useNavigation';
|
||||||
|
export { useNavigationBuilder } from './useNavigationBuilder';
|
||||||
|
export { useNavigationContainerRef } from './useNavigationContainerRef';
|
||||||
|
diff --git a/node_modules/@react-navigation/core/src/index.tsx b/node_modules/@react-navigation/core/src/index.tsx
|
||||||
|
index 6f5175f..211d585 100644
|
||||||
|
--- a/node_modules/@react-navigation/core/src/index.tsx
|
||||||
|
+++ b/node_modules/@react-navigation/core/src/index.tsx
|
||||||
|
@@ -33,7 +33,7 @@ export { ThemeProvider } from './theming/ThemeProvider';
|
||||||
|
export { useTheme } from './theming/useTheme';
|
||||||
|
export * from './types';
|
||||||
|
export { useFocusEffect } from './useFocusEffect';
|
||||||
|
-export { useIsFocused } from './useIsFocused';
|
||||||
|
+export { IsFocusedContext, useIsFocused } from './useIsFocused';
|
||||||
|
export { useNavigation } from './useNavigation';
|
||||||
|
export { useNavigationBuilder } from './useNavigationBuilder';
|
||||||
|
export { useNavigationContainerRef } from './useNavigationContainerRef';
|
||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
RichText,
|
RichText,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
import {t} from '@lingui/core/macro'
|
import {t} from '@lingui/core/macro'
|
||||||
|
import {useIsFocused} from '@react-navigation/native'
|
||||||
import {
|
import {
|
||||||
type InfiniteData,
|
type InfiniteData,
|
||||||
keepPreviousData,
|
keepPreviousData,
|
||||||
@@ -176,10 +177,12 @@ export function getAvatarTypeFromUri(uri: string) {
|
|||||||
export function useFeedSourceInfoQuery({uri}: {uri: string}) {
|
export function useFeedSourceInfoQuery({uri}: {uri: string}) {
|
||||||
const type = getFeedTypeFromUri(uri)
|
const type = getFeedTypeFromUri(uri)
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
const isFocused = useIsFocused()
|
||||||
|
|
||||||
return useQuery({
|
return useQuery({
|
||||||
staleTime: STALE.INFINITY,
|
staleTime: STALE.INFINITY,
|
||||||
queryKey: feedSourceInfoQueryKey({uri}),
|
queryKey: feedSourceInfoQueryKey({uri}),
|
||||||
|
subscribed: isFocused,
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
let view: FeedSourceInfo
|
let view: FeedSourceInfo
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
type ModerationDecision,
|
type ModerationDecision,
|
||||||
type ModerationPrefs,
|
type ModerationPrefs,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {useIsFocused} from '@react-navigation/native'
|
||||||
import {
|
import {
|
||||||
type InfiniteData,
|
type InfiniteData,
|
||||||
type QueryClient,
|
type QueryClient,
|
||||||
@@ -134,6 +135,7 @@ export function usePostFeedQuery(
|
|||||||
const feedTuners = useFeedTuners(feedDesc)
|
const feedTuners = useFeedTuners(feedDesc)
|
||||||
const moderationOpts = useModerationOpts()
|
const moderationOpts = useModerationOpts()
|
||||||
const {data: preferences} = usePreferencesQuery()
|
const {data: preferences} = usePreferencesQuery()
|
||||||
|
const isFocused = useIsFocused()
|
||||||
/**
|
/**
|
||||||
* Load bearing: we need to await AA state or risk FOUC. This marginally
|
* Load bearing: we need to await AA state or risk FOUC. This marginally
|
||||||
* delays feeds, but AA state is fetched immediately on load and is then
|
* delays feeds, but AA state is fetched immediately on load and is then
|
||||||
@@ -183,6 +185,7 @@ export function usePostFeedQuery(
|
|||||||
>({
|
>({
|
||||||
enabled,
|
enabled,
|
||||||
staleTime: STALE.INFINITY,
|
staleTime: STALE.INFINITY,
|
||||||
|
subscribed: isFocused,
|
||||||
queryKey: RQKEY(feedDesc, params),
|
queryKey: RQKEY(feedDesc, params),
|
||||||
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
||||||
logger.debug('usePostFeedQuery', {feedDesc, cursor: pageParam?.cursor})
|
logger.debug('usePostFeedQuery', {feedDesc, cursor: pageParam?.cursor})
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import {useCallback} from 'react'
|
import {useCallback, useContext} from 'react'
|
||||||
import {
|
import {
|
||||||
type AppBskyActorDefs,
|
type AppBskyActorDefs,
|
||||||
type BskyFeedViewPreference,
|
type BskyFeedViewPreference,
|
||||||
type LabelPreference,
|
type LabelPreference,
|
||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
|
import {IsFocusedContext} from '@react-navigation/native'
|
||||||
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {PROD_DEFAULT_FEED} from '#/lib/constants'
|
import {PROD_DEFAULT_FEED} from '#/lib/constants'
|
||||||
@@ -38,6 +39,7 @@ export const preferencesQueryKey = [PERSISTED_QUERY_ROOT, 'getPreferences']
|
|||||||
export function usePreferencesQuery() {
|
export function usePreferencesQuery() {
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
const aa = useAgeAssurance()
|
const aa = useAgeAssurance()
|
||||||
|
const isFocused = useOptionalIsFocused()
|
||||||
|
|
||||||
const query = useQuery({
|
const query = useQuery({
|
||||||
staleTime: STALE.SECONDS.FIFTEEN,
|
staleTime: STALE.SECONDS.FIFTEEN,
|
||||||
@@ -45,6 +47,7 @@ export function usePreferencesQuery() {
|
|||||||
refetchOnWindowFocus: true,
|
refetchOnWindowFocus: true,
|
||||||
queryKey: preferencesQueryKey,
|
queryKey: preferencesQueryKey,
|
||||||
gcTime: PERSISTED_QUERY_GCTIME,
|
gcTime: PERSISTED_QUERY_GCTIME,
|
||||||
|
subscribed: isFocused,
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
if (!agent.did) {
|
if (!agent.did) {
|
||||||
return DEFAULT_LOGGED_OUT_PREFERENCES
|
return DEFAULT_LOGGED_OUT_PREFERENCES
|
||||||
@@ -107,6 +110,11 @@ export function usePreferencesQuery() {
|
|||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// same as useIsFocused, but returns true if not in context (i.e. not in a navigator)
|
||||||
|
function useOptionalIsFocused() {
|
||||||
|
return useContext(IsFocusedContext) ?? true
|
||||||
|
}
|
||||||
|
|
||||||
export function useClearPreferencesMutation() {
|
export function useClearPreferencesMutation() {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
|
|||||||
Reference in New Issue
Block a user