Skip to content

Commit

Permalink
Thread queryClient explicitly through (#3328)
Browse files Browse the repository at this point in the history
* Pass queryClient explicitly to resetProfilePostsQueries

* Pass queryClient explicitly to updatePostShadow

* Pass queryClient explicitly to updateProfileShadow
  • Loading branch information
gaearon authored Apr 3, 2024
1 parent 73df7e5 commit fc1e30a
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 103 deletions.
18 changes: 12 additions & 6 deletions src/state/cache/post-shadow.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {useEffect, useState, useMemo} from 'react'
import EventEmitter from 'eventemitter3'
import {useEffect, useMemo, useState} from 'react'
import {AppBskyFeedDefs} from '@atproto/api'
import {QueryClient} from '@tanstack/react-query'
import EventEmitter from 'eventemitter3'

import {batchedUpdates} from '#/lib/batchedUpdates'
import {Shadow, castAsShadow} from './types'
import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from '../queries/notifications/feed'
import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '../queries/post-feed'
import {findAllPostsInQueryData as findAllPostsInThreadQueryData} from '../queries/post-thread'
import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '../queries/search-posts'
import {queryClient} from 'lib/react-query'
import {castAsShadow, Shadow} from './types'
export type {Shadow} from './types'

export interface PostShadow {
Expand Down Expand Up @@ -93,8 +94,12 @@ function mergeShadow(
})
}

export function updatePostShadow(uri: string, value: Partial<PostShadow>) {
const cachedPosts = findPostsInCache(uri)
export function updatePostShadow(
queryClient: QueryClient,
uri: string,
value: Partial<PostShadow>,
) {
const cachedPosts = findPostsInCache(queryClient, uri)
for (let post of cachedPosts) {
shadows.set(post, {...shadows.get(post), ...value})
}
Expand All @@ -104,6 +109,7 @@ export function updatePostShadow(uri: string, value: Partial<PostShadow>) {
}

function* findPostsInCache(
queryClient: QueryClient,
uri: string,
): Generator<AppBskyFeedDefs.PostView, void> {
for (let post of findAllPostsInFeedQueryData(queryClient, uri)) {
Expand Down
15 changes: 9 additions & 6 deletions src/state/cache/profile-shadow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {useEffect, useState, useMemo} from 'react'
import EventEmitter from 'eventemitter3'
import {useEffect, useMemo, useState} from 'react'
import {AppBskyActorDefs} from '@atproto/api'
import {QueryClient} from '@tanstack/react-query'
import EventEmitter from 'eventemitter3'

import {batchedUpdates} from '#/lib/batchedUpdates'
import {findAllProfilesInQueryData as findAllProfilesInActorSearchQueryData} from '../queries/actor-search'
import {findAllProfilesInQueryData as findAllProfilesInListMembersQueryData} from '../queries/list-members'
import {findAllProfilesInQueryData as findAllProfilesInMyBlockedAccountsQueryData} from '../queries/my-blocked-accounts'
import {findAllProfilesInQueryData as findAllProfilesInMyMutedAccountsQueryData} from '../queries/my-muted-accounts'
Expand All @@ -11,9 +14,7 @@ import {findAllProfilesInQueryData as findAllProfilesInProfileQueryData} from '.
import {findAllProfilesInQueryData as findAllProfilesInProfileFollowersQueryData} from '../queries/profile-followers'
import {findAllProfilesInQueryData as findAllProfilesInProfileFollowsQueryData} from '../queries/profile-follows'
import {findAllProfilesInQueryData as findAllProfilesInSuggestedFollowsQueryData} from '../queries/suggested-follows'
import {findAllProfilesInQueryData as findAllProfilesInActorSearchQueryData} from '../queries/actor-search'
import {Shadow, castAsShadow} from './types'
import {queryClient} from 'lib/react-query'
import {castAsShadow, Shadow} from './types'
export type {Shadow} from './types'

export interface ProfileShadow {
Expand Down Expand Up @@ -58,10 +59,11 @@ export function useProfileShadow<
}

export function updateProfileShadow(
queryClient: QueryClient,
did: string,
value: Partial<ProfileShadow>,
) {
const cachedProfiles = findProfilesInCache(did)
const cachedProfiles = findProfilesInCache(queryClient, did)
for (let post of cachedProfiles) {
shadows.set(post, {...shadows.get(post), ...value})
}
Expand Down Expand Up @@ -90,6 +92,7 @@ function mergeShadow<TProfileView extends AppBskyActorDefs.ProfileView>(
}

function* findProfilesInCache(
queryClient: QueryClient,
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
yield* findAllProfilesInListMembersQueryData(queryClient, did)
Expand Down
40 changes: 22 additions & 18 deletions src/state/queries/post-feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ import {AppState} from 'react-native'
import {
AppBskyFeedDefs,
AppBskyFeedPost,
ModerationDecision,
AtUri,
ModerationDecision,
} from '@atproto/api'
import {
useInfiniteQuery,
InfiniteData,
QueryKey,
QueryClient,
QueryKey,
useInfiniteQuery,
useQueryClient,
} from '@tanstack/react-query'

import {HomeFeedAPI} from '#/lib/api/feed/home'
import {moderatePost_wrapped as moderatePost} from '#/lib/moderatePost_wrapped'
import {useFeedTuners} from '../preferences/feed-tuners'
import {FeedTuner, FeedTunerFn, NoopFeedTuner} from 'lib/api/feed-manip'
import {FeedAPI, ReasonFeedSource} from 'lib/api/feed/types'
import {FollowingFeedAPI} from 'lib/api/feed/following'
import {logger} from '#/logger'
import {STALE} from '#/state/queries'
import {DEFAULT_LOGGED_OUT_PREFERENCES} from '#/state/queries/preferences/const'
import {getAgent} from '#/state/session'
import {AuthorFeedAPI} from 'lib/api/feed/author'
import {LikesFeedAPI} from 'lib/api/feed/likes'
import {CustomFeedAPI} from 'lib/api/feed/custom'
import {FollowingFeedAPI} from 'lib/api/feed/following'
import {LikesFeedAPI} from 'lib/api/feed/likes'
import {ListFeedAPI} from 'lib/api/feed/list'
import {MergeFeedAPI} from 'lib/api/feed/merge'
import {HomeFeedAPI} from '#/lib/api/feed/home'
import {logger} from '#/logger'
import {STALE} from '#/state/queries'
import {precacheFeedPostProfiles} from './profile'
import {getAgent} from '#/state/session'
import {DEFAULT_LOGGED_OUT_PREFERENCES} from '#/state/queries/preferences/const'
import {FeedAPI, ReasonFeedSource} from 'lib/api/feed/types'
import {FeedTuner, FeedTunerFn, NoopFeedTuner} from 'lib/api/feed-manip'
import {BSKY_FEED_OWNER_DIDS} from 'lib/constants'
import {KnownError} from '#/view/com/posts/FeedErrorMessage'
import {embedViewRecordToPostView, getEmbeddedPost} from './util'
import {useFeedTuners} from '../preferences/feed-tuners'
import {useModerationOpts} from './preferences'
import {queryClient} from 'lib/react-query'
import {BSKY_FEED_OWNER_DIDS} from 'lib/constants'
import {precacheFeedPostProfiles} from './profile'
import {embedViewRecordToPostView, getEmbeddedPost} from './util'

type ActorDid = string
type AuthorFilter =
Expand Down Expand Up @@ -458,7 +458,11 @@ function assertSomePostsPassModeration(feed: AppBskyFeedDefs.FeedViewPost[]) {
}
}

export function resetProfilePostsQueries(did: string, timeout = 0) {
export function resetProfilePostsQueries(
queryClient: QueryClient,
did: string,
timeout = 0,
) {
setTimeout(() => {
queryClient.resetQueries({
predicate: query =>
Expand Down
36 changes: 20 additions & 16 deletions src/state/queries/post.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {useCallback} from 'react'
import {AppBskyFeedDefs, AtUri} from '@atproto/api'
import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query'
import {Shadow} from '#/state/cache/types'
import {getAgent} from '#/state/session'
import {updatePostShadow} from '#/state/cache/post-shadow'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'

import {track} from '#/lib/analytics/analytics'
import {logEvent, LogEvents} from '#/lib/statsig/statsig'
import {useToggleMutationQueue} from '#/lib/hooks/useToggleMutationQueue'
import {logEvent, LogEvents} from '#/lib/statsig/statsig'
import {updatePostShadow} from '#/state/cache/post-shadow'
import {Shadow} from '#/state/cache/types'
import {getAgent} from '#/state/session'

export const RQKEY = (postUri: string) => ['post', postUri]

Expand Down Expand Up @@ -62,6 +63,7 @@ export function usePostLikeMutationQueue(
logContext: LogEvents['post:like']['logContext'] &
LogEvents['post:unlike']['logContext'],
) {
const queryClient = useQueryClient()
const postUri = post.uri
const postCid = post.cid
const initialLikeUri = post.viewer?.like
Expand Down Expand Up @@ -89,27 +91,27 @@ export function usePostLikeMutationQueue(
},
onSuccess(finalLikeUri) {
// finalize
updatePostShadow(postUri, {
updatePostShadow(queryClient, postUri, {
likeUri: finalLikeUri,
})
},
})

const queueLike = useCallback(() => {
// optimistically update
updatePostShadow(postUri, {
updatePostShadow(queryClient, postUri, {
likeUri: 'pending',
})
return queueToggle(true)
}, [postUri, queueToggle])
}, [queryClient, postUri, queueToggle])

const queueUnlike = useCallback(() => {
// optimistically update
updatePostShadow(postUri, {
updatePostShadow(queryClient, postUri, {
likeUri: undefined,
})
return queueToggle(false)
}, [postUri, queueToggle])
}, [queryClient, postUri, queueToggle])

return [queueLike, queueUnlike]
}
Expand Down Expand Up @@ -149,6 +151,7 @@ export function usePostRepostMutationQueue(
logContext: LogEvents['post:repost']['logContext'] &
LogEvents['post:unrepost']['logContext'],
) {
const queryClient = useQueryClient()
const postUri = post.uri
const postCid = post.cid
const initialRepostUri = post.viewer?.repost
Expand Down Expand Up @@ -176,27 +179,27 @@ export function usePostRepostMutationQueue(
},
onSuccess(finalRepostUri) {
// finalize
updatePostShadow(postUri, {
updatePostShadow(queryClient, postUri, {
repostUri: finalRepostUri,
})
},
})

const queueRepost = useCallback(() => {
// optimistically update
updatePostShadow(postUri, {
updatePostShadow(queryClient, postUri, {
repostUri: 'pending',
})
return queueToggle(true)
}, [postUri, queueToggle])
}, [queryClient, postUri, queueToggle])

const queueUnrepost = useCallback(() => {
// optimistically update
updatePostShadow(postUri, {
updatePostShadow(queryClient, postUri, {
repostUri: undefined,
})
return queueToggle(false)
}, [postUri, queueToggle])
}, [queryClient, postUri, queueToggle])

return [queueRepost, queueUnrepost]
}
Expand Down Expand Up @@ -234,12 +237,13 @@ function usePostUnrepostMutation(
}

export function usePostDeleteMutation() {
const queryClient = useQueryClient()
return useMutation<void, Error, {uri: string}>({
mutationFn: async ({uri}) => {
await getAgent().deletePost(uri)
},
onSuccess(data, variables) {
updatePostShadow(variables.uri, {isDeleted: true})
updatePostShadow(queryClient, variables.uri, {isDeleted: true})
track('Post:Delete')
},
})
Expand Down
Loading

0 comments on commit fc1e30a

Please sign in to comment.