Make state transitions sync
This commit is contained in:
+12
-53
@@ -18,7 +18,6 @@ export type ConvoParams = {
|
|||||||
export enum ConvoStatus {
|
export enum ConvoStatus {
|
||||||
Uninitialized = 'uninitialized',
|
Uninitialized = 'uninitialized',
|
||||||
Initializing = 'initializing',
|
Initializing = 'initializing',
|
||||||
Resuming = 'resuming',
|
|
||||||
Ready = 'ready',
|
Ready = 'ready',
|
||||||
Error = 'error',
|
Error = 'error',
|
||||||
Backgrounded = 'backgrounded',
|
Backgrounded = 'backgrounded',
|
||||||
@@ -133,20 +132,6 @@ export type ConvoState =
|
|||||||
) => Promise<void>
|
) => Promise<void>
|
||||||
fetchMessageHistory: () => Promise<void>
|
fetchMessageHistory: () => Promise<void>
|
||||||
}
|
}
|
||||||
| {
|
|
||||||
status: ConvoStatus.Resuming
|
|
||||||
items: ConvoItem[]
|
|
||||||
convo: ChatBskyConvoDefs.ConvoView
|
|
||||||
error: undefined
|
|
||||||
sender: AppBskyActorDefs.ProfileViewBasic
|
|
||||||
recipients: AppBskyActorDefs.ProfileViewBasic[]
|
|
||||||
isFetchingHistory: boolean
|
|
||||||
deleteMessage: (messageId: string) => Promise<void>
|
|
||||||
sendMessage: (
|
|
||||||
message: ChatBskyConvoSendMessage.InputSchema['message'],
|
|
||||||
) => Promise<void>
|
|
||||||
fetchMessageHistory: () => Promise<void>
|
|
||||||
}
|
|
||||||
| {
|
| {
|
||||||
status: ConvoStatus.Error
|
status: ConvoStatus.Error
|
||||||
items: []
|
items: []
|
||||||
@@ -285,7 +270,6 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
case ConvoStatus.Suspended:
|
case ConvoStatus.Suspended:
|
||||||
case ConvoStatus.Backgrounded:
|
case ConvoStatus.Backgrounded:
|
||||||
case ConvoStatus.Resuming:
|
|
||||||
case ConvoStatus.Ready: {
|
case ConvoStatus.Ready: {
|
||||||
return {
|
return {
|
||||||
status: this.status,
|
status: this.status,
|
||||||
@@ -369,45 +353,24 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async resume() {
|
// TODO resume failure
|
||||||
|
resume() {
|
||||||
if (
|
if (
|
||||||
this.status === ConvoStatus.Suspended ||
|
this.status === ConvoStatus.Suspended ||
|
||||||
this.status === ConvoStatus.Backgrounded
|
this.status === ConvoStatus.Backgrounded
|
||||||
) {
|
) {
|
||||||
logger.debug('Convo: resume', {id: this.id}, logger.DebugContext.convo)
|
logger.debug('Convo: resume', {id: this.id}, logger.DebugContext.convo)
|
||||||
|
|
||||||
const fromStatus = this.status
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.cancelNextPoll()
|
|
||||||
|
|
||||||
this.status = ConvoStatus.Resuming
|
|
||||||
this.commit()
|
|
||||||
|
|
||||||
await this.refreshConvo()
|
|
||||||
this.status = ConvoStatus.Ready
|
this.status = ConvoStatus.Ready
|
||||||
this.commit()
|
this.cancelNextPoll()
|
||||||
|
|
||||||
// throw new Error('UNCOMMENT TO TEST RESUME FAILURE')
|
|
||||||
|
|
||||||
this.pollInterval = ACTIVE_POLL_INTERVAL
|
this.pollInterval = ACTIVE_POLL_INTERVAL
|
||||||
this.pollEvents()
|
this.pollEvents()
|
||||||
} catch (e) {
|
|
||||||
logger.error('Convo: failed to resume')
|
|
||||||
|
|
||||||
this.footerItems.set(ConvoItemError.ResumeFailed, {
|
|
||||||
type: 'error-recoverable',
|
|
||||||
key: ConvoItemError.ResumeFailed,
|
|
||||||
code: ConvoItemError.ResumeFailed,
|
|
||||||
retry: () => {
|
|
||||||
this.footerItems.delete(ConvoItemError.ResumeFailed)
|
|
||||||
this.resume()
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
this.status = fromStatus
|
|
||||||
this.commit()
|
this.commit()
|
||||||
}
|
|
||||||
|
// intentionally un-awaited
|
||||||
|
this.refreshConvo().then(() => {
|
||||||
|
this.commit()
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Convo: resume called from ${this.status}`,
|
`Convo: resume called from ${this.status}`,
|
||||||
@@ -417,11 +380,8 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async background() {
|
background() {
|
||||||
if (
|
if (this.status === ConvoStatus.Ready) {
|
||||||
this.status === ConvoStatus.Ready ||
|
|
||||||
this.status === ConvoStatus.Resuming
|
|
||||||
) {
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
'Convo: backgrounded',
|
'Convo: backgrounded',
|
||||||
{id: this.id},
|
{id: this.id},
|
||||||
@@ -433,11 +393,10 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async suspend() {
|
suspend() {
|
||||||
if (
|
if (
|
||||||
this.status === ConvoStatus.Ready ||
|
this.status === ConvoStatus.Ready ||
|
||||||
this.status === ConvoStatus.Backgrounded ||
|
this.status === ConvoStatus.Backgrounded
|
||||||
this.status === ConvoStatus.Resuming
|
|
||||||
) {
|
) {
|
||||||
logger.debug('Convo: suspended', {id: this.id}, logger.DebugContext.convo)
|
logger.debug('Convo: suspended', {id: this.id}, logger.DebugContext.convo)
|
||||||
DEBUG_ACTIVE_CHAT = undefined
|
DEBUG_ACTIVE_CHAT = undefined
|
||||||
|
|||||||
Reference in New Issue
Block a user