An ATproto social media client -- with an independent Appview.

Reset after 5 min (#4026)

authored by

Eric Bailey and committed by
GitHub
6382fec7 6efe90a5

+27 -7
+26 -7
src/state/messages/convo/agent.ts
··· 13 13 import { 14 14 ACTIVE_POLL_INTERVAL, 15 15 BACKGROUND_POLL_INTERVAL, 16 + INACTIVE_TIMEOUT, 16 17 } from '#/state/messages/convo/const' 17 18 import { 18 19 ConvoDispatch, ··· 78 79 private headerItems: Map<string, ConvoItem> = new Map() 79 80 80 81 private isProcessingPendingMessages = false 82 + 83 + private lastActiveTimestamp: number | undefined 81 84 82 85 convoId: string 83 86 convo: ChatBskyConvoDefs.ConvoView | undefined ··· 272 275 } 273 276 case ConvoStatus.Backgrounded: { 274 277 switch (action.event) { 275 - // TODO truncate history if needed 276 278 case ConvoDispatchEvent.Resume: { 277 - if (this.convo) { 278 - this.status = ConvoStatus.Ready 279 - this.refreshConvo() 279 + if (this.wasChatInactive()) { 280 + this.reset() 280 281 } else { 281 - this.status = ConvoStatus.Initializing 282 - this.setup() 282 + if (this.convo) { 283 + this.status = ConvoStatus.Ready 284 + this.refreshConvo() 285 + } else { 286 + this.status = ConvoStatus.Initializing 287 + this.setup() 288 + } 289 + this.requestPollInterval(ACTIVE_POLL_INTERVAL) 283 290 } 284 - this.requestPollInterval(ACTIVE_POLL_INTERVAL) 285 291 break 286 292 } 287 293 case ConvoDispatchEvent.Suspend: { ··· 354 360 logger.DebugContext.convo, 355 361 ) 356 362 363 + this.updateLastActiveTimestamp() 357 364 this.commit() 358 365 } 359 366 ··· 434 441 suspend() { 435 442 this.dispatch({event: ConvoDispatchEvent.Suspend}) 436 443 DEBUG_ACTIVE_CHAT = undefined 444 + } 445 + 446 + /** 447 + * Called on any state transition, like when the chat is backgrounded. This 448 + * value is then checked on background -> foreground transitions. 449 + */ 450 + private updateLastActiveTimestamp() { 451 + this.lastActiveTimestamp = Date.now() 452 + } 453 + private wasChatInactive() { 454 + if (!this.lastActiveTimestamp) return true 455 + return Date.now() - this.lastActiveTimestamp > INACTIVE_TIMEOUT 437 456 } 438 457 439 458 private requestedPollInterval: (() => void) | undefined
+1
src/state/messages/convo/const.ts
··· 1 1 export const ACTIVE_POLL_INTERVAL = 1e3 2 2 export const BACKGROUND_POLL_INTERVAL = 5e3 3 + export const INACTIVE_TIMEOUT = 60e3 * 5