Skip to content

Commit

Permalink
[Fix] Sync state (#24)
Browse files Browse the repository at this point in the history
* fixes for sync state

* bump

* check undefined and null
  • Loading branch information
BrettCleary authored Oct 22, 2024
1 parent f08cc31 commit 24300ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperplay/quests-ui",
"version": "0.0.28",
"version": "0.0.29",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
17 changes: 12 additions & 5 deletions src/state/QuestPlayStreakSyncState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class QuestPlayStreakSyncState {
string,
{
syncTimers: NodeJS.Timeout[]
intervalTimers: NodeJS.Timer[]
intervalTimers: NodeJS.Timeout[]
}
> = {}

Expand Down Expand Up @@ -92,6 +92,11 @@ class QuestPlayStreakSyncState {
const syncThisProjectMutation = async () =>
this.queryClient?.fetchQuery({
queryKey: getSyncPlaysessionQueryKey(projectId),
/**
* if multiple quests are syncing at the same time (within 1 second), we want to only send once.
* if one quest finishes in 40 seconds and another in 41 seconds, then we want to post at 40 and 41 sec
*/
staleTime: 500,
queryFn: async () => {
this.syncPlaySession(
projectId,
Expand All @@ -105,6 +110,7 @@ class QuestPlayStreakSyncState {
)
this.appQueryClient?.invalidateQueries({ queryKey })
}
return null
}
})

Expand All @@ -114,11 +120,12 @@ class QuestPlayStreakSyncState {
const minimumRequiredPlayTimeInSeconds =
questMeta?.eligibility?.play_streak.minimum_session_time_in_seconds
if (
minimumRequiredPlayTimeInSeconds &&
currentPlayTimeInSeconds &&
minimumRequiredPlayTimeInSeconds !== undefined &&
minimumRequiredPlayTimeInSeconds !== null &&
currentPlayTimeInSeconds !== undefined &&
currentPlayTimeInSeconds !== null &&
currentPlayTimeInSeconds < minimumRequiredPlayTimeInSeconds
) {
console.log('setting timeout for post mutation')
const finalSyncTimer = setTimeout(
syncThisProjectMutation,
minimumRequiredPlayTimeInSeconds - currentPlayTimeInSeconds
Expand All @@ -130,7 +137,7 @@ class QuestPlayStreakSyncState {
syncThisProjectMutation,
this.intervalSyncTick
)
this.projectSyncData[projectId].syncTimers.push(intervalId)
this.projectSyncData[projectId].intervalTimers.push(intervalId)
} catch (err) {
console.error(`Error while setting up playstreak sync: ${err}`)
}
Expand Down

0 comments on commit 24300ac

Please sign in to comment.