Skip to content

Commit

Permalink
Reset after 5 min (#4026)
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey authored May 15, 2024
1 parent 6efe90a commit 6382fec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/state/messages/convo/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {isNative} from '#/platform/detection'
import {
ACTIVE_POLL_INTERVAL,
BACKGROUND_POLL_INTERVAL,
INACTIVE_TIMEOUT,
} from '#/state/messages/convo/const'
import {
ConvoDispatch,
Expand Down Expand Up @@ -79,6 +80,8 @@ export class Convo {

private isProcessingPendingMessages = false

private lastActiveTimestamp: number | undefined

convoId: string
convo: ChatBskyConvoDefs.ConvoView | undefined
sender: AppBskyActorDefs.ProfileViewBasic | undefined
Expand Down Expand Up @@ -272,16 +275,19 @@ export class Convo {
}
case ConvoStatus.Backgrounded: {
switch (action.event) {
// TODO truncate history if needed
case ConvoDispatchEvent.Resume: {
if (this.convo) {
this.status = ConvoStatus.Ready
this.refreshConvo()
if (this.wasChatInactive()) {
this.reset()
} else {
this.status = ConvoStatus.Initializing
this.setup()
if (this.convo) {
this.status = ConvoStatus.Ready
this.refreshConvo()
} else {
this.status = ConvoStatus.Initializing
this.setup()
}
this.requestPollInterval(ACTIVE_POLL_INTERVAL)
}
this.requestPollInterval(ACTIVE_POLL_INTERVAL)
break
}
case ConvoDispatchEvent.Suspend: {
Expand Down Expand Up @@ -354,6 +360,7 @@ export class Convo {
logger.DebugContext.convo,
)

this.updateLastActiveTimestamp()
this.commit()
}

Expand Down Expand Up @@ -436,6 +443,18 @@ export class Convo {
DEBUG_ACTIVE_CHAT = undefined
}

/**
* Called on any state transition, like when the chat is backgrounded. This
* value is then checked on background -> foreground transitions.
*/
private updateLastActiveTimestamp() {
this.lastActiveTimestamp = Date.now()
}
private wasChatInactive() {
if (!this.lastActiveTimestamp) return true
return Date.now() - this.lastActiveTimestamp > INACTIVE_TIMEOUT
}

private requestedPollInterval: (() => void) | undefined
private requestPollInterval(interval: number) {
this.withdrawRequestedPollInterval()
Expand Down
1 change: 1 addition & 0 deletions src/state/messages/convo/const.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const ACTIVE_POLL_INTERVAL = 1e3
export const BACKGROUND_POLL_INTERVAL = 5e3
export const INACTIVE_TIMEOUT = 60e3 * 5

0 comments on commit 6382fec

Please sign in to comment.