Handle init/resume/suspend/background and polling
This commit is contained in:
+72
-34
@@ -190,7 +190,6 @@ export class Convo {
|
|||||||
private historyCursor: string | undefined | null = undefined
|
private historyCursor: string | undefined | null = undefined
|
||||||
private isFetchingHistory = false
|
private isFetchingHistory = false
|
||||||
private eventsCursor: string | undefined = undefined
|
private eventsCursor: string | undefined = undefined
|
||||||
private pollingFailure = false
|
|
||||||
|
|
||||||
private pastMessages: Map<
|
private pastMessages: Map<
|
||||||
string,
|
string,
|
||||||
@@ -208,8 +207,9 @@ export class Convo {
|
|||||||
private footerItems: Map<string, ConvoItem> = new Map()
|
private footerItems: Map<string, ConvoItem> = new Map()
|
||||||
private headerItems: Map<string, ConvoItem> = new Map()
|
private headerItems: Map<string, ConvoItem> = new Map()
|
||||||
|
|
||||||
private pendingEventIngestion: Promise<void> | undefined
|
|
||||||
private isProcessingPendingMessages = false
|
private isProcessingPendingMessages = false
|
||||||
|
private pendingPoll: Promise<void> | undefined
|
||||||
|
private nextPoll: NodeJS.Timeout | undefined
|
||||||
|
|
||||||
convoId: string
|
convoId: string
|
||||||
convo: ChatBskyConvoDefs.ConvoView | undefined
|
convo: ChatBskyConvoDefs.ConvoView | undefined
|
||||||
@@ -217,7 +217,10 @@ export class Convo {
|
|||||||
recipients: AppBskyActorDefs.ProfileViewBasic[] | undefined = undefined
|
recipients: AppBskyActorDefs.ProfileViewBasic[] | undefined = undefined
|
||||||
snapshot: ConvoState | undefined
|
snapshot: ConvoState | undefined
|
||||||
|
|
||||||
|
id: string
|
||||||
|
|
||||||
constructor(params: ConvoParams) {
|
constructor(params: ConvoParams) {
|
||||||
|
this.id = nanoid(2)
|
||||||
this.convoId = params.convoId
|
this.convoId = params.convoId
|
||||||
this.agent = params.agent
|
this.agent = params.agent
|
||||||
this.__tempFromUserDid = params.__tempFromUserDid
|
this.__tempFromUserDid = params.__tempFromUserDid
|
||||||
@@ -227,6 +230,12 @@ export class Convo {
|
|||||||
this.sendMessage = this.sendMessage.bind(this)
|
this.sendMessage = this.sendMessage.bind(this)
|
||||||
this.deleteMessage = this.deleteMessage.bind(this)
|
this.deleteMessage = this.deleteMessage.bind(this)
|
||||||
this.fetchMessageHistory = this.fetchMessageHistory.bind(this)
|
this.fetchMessageHistory = this.fetchMessageHistory.bind(this)
|
||||||
|
|
||||||
|
logger.debug(
|
||||||
|
'Convo: created',
|
||||||
|
{convoId: this.convoId, id: this.id},
|
||||||
|
logger.DebugContext.convo,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private commit() {
|
private commit() {
|
||||||
@@ -318,12 +327,12 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
logger.debug('Convo: init', {}, logger.DebugContext.convo)
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.status === ConvoStatus.Uninitialized ||
|
this.status === ConvoStatus.Uninitialized ||
|
||||||
this.status === ConvoStatus.Error
|
this.status === ConvoStatus.Error
|
||||||
) {
|
) {
|
||||||
|
logger.debug('Convo: init', {}, logger.DebugContext.convo)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.status = ConvoStatus.Initializing
|
this.status = ConvoStatus.Initializing
|
||||||
this.commit()
|
this.commit()
|
||||||
@@ -348,21 +357,25 @@ export class Convo {
|
|||||||
this.status = ConvoStatus.Error
|
this.status = ConvoStatus.Error
|
||||||
this.commit()
|
this.commit()
|
||||||
}
|
}
|
||||||
|
} else if (this.status === ConvoStatus.Suspended) {
|
||||||
|
this.resume()
|
||||||
} else {
|
} else {
|
||||||
logger.warn(`Convo: cannot init from ${this.status}`)
|
logger.warn(`Convo: cannot init from ${this.status}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async resume() {
|
async resume() {
|
||||||
logger.debug('Convo: resume', {}, logger.DebugContext.convo)
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.status === ConvoStatus.Suspended ||
|
this.status === ConvoStatus.Suspended ||
|
||||||
this.status === ConvoStatus.Backgrounded
|
this.status === ConvoStatus.Backgrounded
|
||||||
) {
|
) {
|
||||||
|
logger.debug('Convo: resume', {}, logger.DebugContext.convo)
|
||||||
|
|
||||||
const fromStatus = this.status
|
const fromStatus = this.status
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.cancelNextPoll()
|
||||||
|
|
||||||
this.status = ConvoStatus.Resuming
|
this.status = ConvoStatus.Resuming
|
||||||
this.commit()
|
this.commit()
|
||||||
|
|
||||||
@@ -391,22 +404,37 @@ export class Convo {
|
|||||||
this.commit()
|
this.commit()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.warn(`Convo: cannot resume from ${this.status}`)
|
logger.debug(
|
||||||
|
`Convo: resume called from ${this.status}`,
|
||||||
|
{},
|
||||||
|
logger.DebugContext.convo,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async background() {
|
async background() {
|
||||||
|
if (
|
||||||
|
this.status === ConvoStatus.Ready ||
|
||||||
|
this.status === ConvoStatus.Resuming
|
||||||
|
) {
|
||||||
logger.debug('Convo: backgrounded', {}, logger.DebugContext.convo)
|
logger.debug('Convo: backgrounded', {}, logger.DebugContext.convo)
|
||||||
this.status = ConvoStatus.Backgrounded
|
this.status = ConvoStatus.Backgrounded
|
||||||
this.pollInterval = BACKGROUND_POLL_INTERVAL
|
this.pollInterval = BACKGROUND_POLL_INTERVAL
|
||||||
this.commit()
|
this.commit()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async suspend() {
|
async suspend() {
|
||||||
|
if (
|
||||||
|
this.status === ConvoStatus.Ready ||
|
||||||
|
this.status === ConvoStatus.Backgrounded ||
|
||||||
|
this.status === ConvoStatus.Resuming
|
||||||
|
) {
|
||||||
logger.debug('Convo: suspended', {}, logger.DebugContext.convo)
|
logger.debug('Convo: suspended', {}, logger.DebugContext.convo)
|
||||||
this.status = ConvoStatus.Suspended
|
this.status = ConvoStatus.Suspended
|
||||||
this.commit()
|
this.commit()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async refreshConvo() {
|
async refreshConvo() {
|
||||||
const response = await this.agent.api.chat.bsky.convo.getConvo(
|
const response = await this.agent.api.chat.bsky.convo.getConvo(
|
||||||
@@ -518,28 +546,54 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async pollEvents() {
|
private async pollEvents() {
|
||||||
|
if (this.pendingPoll) return
|
||||||
|
|
||||||
if (
|
if (
|
||||||
this.status === ConvoStatus.Ready ||
|
this.status === ConvoStatus.Ready ||
|
||||||
this.status === ConvoStatus.Backgrounded
|
this.status === ConvoStatus.Backgrounded
|
||||||
) {
|
) {
|
||||||
if (this.pendingEventIngestion) return
|
logger.debug(
|
||||||
|
'Convo: poll events',
|
||||||
|
{convoId: this.convoId, id: this.id},
|
||||||
|
logger.DebugContext.convo,
|
||||||
|
)
|
||||||
|
|
||||||
/*
|
try {
|
||||||
* Represents a failed state, which is retryable.
|
this.pendingPoll = this.ingestLatestEvents()
|
||||||
*/
|
await this.pendingPoll
|
||||||
if (this.pollingFailure) return
|
this.pendingPoll = undefined
|
||||||
|
this.nextPoll = setTimeout(() => {
|
||||||
setTimeout(async () => {
|
|
||||||
this.pendingEventIngestion = this.ingestLatestEvents()
|
|
||||||
await this.pendingEventIngestion
|
|
||||||
this.pendingEventIngestion = undefined
|
|
||||||
this.pollEvents()
|
this.pollEvents()
|
||||||
}, this.pollInterval)
|
}, this.pollInterval)
|
||||||
|
} catch (e: any) {
|
||||||
|
logger.error('Convo: poll events failed')
|
||||||
|
|
||||||
|
this.cancelNextPoll()
|
||||||
|
this.pendingPoll = undefined
|
||||||
|
|
||||||
|
this.footerItems.set(ConvoItemError.PollFailed, {
|
||||||
|
type: 'error-recoverable',
|
||||||
|
key: ConvoItemError.PollFailed,
|
||||||
|
code: ConvoItemError.PollFailed,
|
||||||
|
retry: () => {
|
||||||
|
this.footerItems.delete(ConvoItemError.PollFailed)
|
||||||
|
this.commit()
|
||||||
|
this.pollEvents()
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
this.commit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
private cancelNextPoll() {
|
||||||
|
if (this.nextPoll) clearTimeout(this.nextPoll)
|
||||||
|
}
|
||||||
|
|
||||||
async ingestLatestEvents() {
|
async ingestLatestEvents() {
|
||||||
try {
|
|
||||||
// throw new Error('UNCOMMENT TO TEST POLL FAILURE')
|
// throw new Error('UNCOMMENT TO TEST POLL FAILURE')
|
||||||
const response = await this.agent.api.chat.bsky.convo.getLog(
|
const response = await this.agent.api.chat.bsky.convo.getLog(
|
||||||
{
|
{
|
||||||
@@ -613,22 +667,6 @@ export class Convo {
|
|||||||
if (needsCommit) {
|
if (needsCommit) {
|
||||||
this.commit()
|
this.commit()
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
|
||||||
logger.error('Convo: failed to poll events')
|
|
||||||
this.pollingFailure = true
|
|
||||||
this.footerItems.set(ConvoItemError.PollFailed, {
|
|
||||||
type: 'error-recoverable',
|
|
||||||
key: ConvoItemError.PollFailed,
|
|
||||||
code: ConvoItemError.PollFailed,
|
|
||||||
retry: () => {
|
|
||||||
this.footerItems.delete(ConvoItemError.PollFailed)
|
|
||||||
this.pollingFailure = false
|
|
||||||
this.commit()
|
|
||||||
this.pollEvents()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
this.commit()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendMessage(message: ChatBskyConvoSendMessage.InputSchema['message']) {
|
async sendMessage(message: ChatBskyConvoSendMessage.InputSchema['message']) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import React, {useContext, useState, useSyncExternalStore} from 'react'
|
import React, {useContext, useState, useSyncExternalStore} from 'react'
|
||||||
|
import {AppState} from 'react-native'
|
||||||
import {BskyAgent} from '@atproto-labs/api'
|
import {BskyAgent} from '@atproto-labs/api'
|
||||||
import {useFocusEffect} from '@react-navigation/native'
|
import {useFocusEffect} from '@react-navigation/native'
|
||||||
|
|
||||||
@@ -44,5 +45,21 @@ export function ChatProvider({
|
|||||||
}, [convo]),
|
}, [convo]),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const handleAppStateChange = (nextAppState: string) => {
|
||||||
|
if (nextAppState === 'active') {
|
||||||
|
convo.resume()
|
||||||
|
} else {
|
||||||
|
convo.background()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const sub = AppState.addEventListener('change', handleAppStateChange)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
sub.remove()
|
||||||
|
}
|
||||||
|
}, [convo])
|
||||||
|
|
||||||
return <ChatContext.Provider value={service}>{children}</ChatContext.Provider>
|
return <ChatContext.Provider value={service}>{children}</ChatContext.Provider>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user