Skip to content

Commit

Permalink
refactor: only refetch super like count if its already fetched before
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Jan 30, 2024
1 parent 013cb7c commit 433e7e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/components/utils/datahub/active-staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { PostRewards } from 'src/rtk/features/activeStaking/postRewardSlice'
import { RewardHistory } from 'src/rtk/features/activeStaking/rewardHistorySlice'
import { fetchRewardReport, RewardReport } from 'src/rtk/features/activeStaking/rewardReportSlice'
import {
fetchSuperLikeCounts,
invalidateSuperLikeCounts,
SuperLikeCount,
} from 'src/rtk/features/activeStaking/superLikeCountsSlice'
import {
Expand Down Expand Up @@ -395,7 +395,7 @@ async function processSubscriptionEvent(
const dispatch = getStoreDispatcher()
if (!dispatch) throw new Error('Dispatcher not exist')

dispatch(fetchSuperLikeCounts({ postIds: [post.persistentId], reload: true }))
dispatch(invalidateSuperLikeCounts({ postId: post.persistentId }))
if (staker.id === myAddress) {
dispatch(fetchRewardReport({ address: myAddress, reload: true }))
dispatch(
Expand Down
13 changes: 12 additions & 1 deletion src/rtk/features/activeStaking/superLikeCountsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createEntityAdapter, createSlice } from '@reduxjs/toolkit'
import { createAsyncThunk, createEntityAdapter, createSlice } from '@reduxjs/toolkit'
import { getSuperLikeCounts } from 'src/components/utils/datahub/active-staking'
import { ThunkApiConfig } from 'src/rtk/app/helpers'
import { RootState } from 'src/rtk/app/rootReducer'
import { createSimpleManyFetchWrapper } from 'src/rtk/app/wrappers'

Expand Down Expand Up @@ -35,6 +36,16 @@ export const fetchSuperLikeCounts = createSimpleManyFetchWrapper<
},
})

export const invalidateSuperLikeCounts = createAsyncThunk<void, { postId: string }, ThunkApiConfig>(
`${sliceName}/invalidate`,
async ({ postId }, { getState, dispatch }) => {
const state = selectPostSuperLikeCount(getState(), postId)
if (!state) return

await dispatch(fetchSuperLikeCounts({ postIds: [postId], reload: true }))
},
)

const slice = createSlice({
name: sliceName,
initialState: adapter.getInitialState(),
Expand Down

0 comments on commit 433e7e4

Please sign in to comment.