From ba15e24519066cd3c5f9cbed5034f2abb5ea9a55 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Mon, 6 May 2024 17:49:49 -0500 Subject: [PATCH] Make state transitions sync --- src/state/messages/convo.ts | 69 ++++++++----------------------------- 1 file changed, 14 insertions(+), 55 deletions(-) diff --git a/src/state/messages/convo.ts b/src/state/messages/convo.ts index ae529f00f6..869de74e71 100644 --- a/src/state/messages/convo.ts +++ b/src/state/messages/convo.ts @@ -18,7 +18,6 @@ export type ConvoParams = { export enum ConvoStatus { Uninitialized = 'uninitialized', Initializing = 'initializing', - Resuming = 'resuming', Ready = 'ready', Error = 'error', Backgrounded = 'backgrounded', @@ -133,20 +132,6 @@ export type ConvoState = ) => Promise fetchMessageHistory: () => Promise } - | { - status: ConvoStatus.Resuming - items: ConvoItem[] - convo: ChatBskyConvoDefs.ConvoView - error: undefined - sender: AppBskyActorDefs.ProfileViewBasic - recipients: AppBskyActorDefs.ProfileViewBasic[] - isFetchingHistory: boolean - deleteMessage: (messageId: string) => Promise - sendMessage: ( - message: ChatBskyConvoSendMessage.InputSchema['message'], - ) => Promise - fetchMessageHistory: () => Promise - } | { status: ConvoStatus.Error items: [] @@ -285,7 +270,6 @@ export class Convo { } case ConvoStatus.Suspended: case ConvoStatus.Backgrounded: - case ConvoStatus.Resuming: case ConvoStatus.Ready: { return { status: this.status, @@ -369,45 +353,24 @@ export class Convo { } } - async resume() { + // TODO resume failure + resume() { if ( this.status === ConvoStatus.Suspended || this.status === ConvoStatus.Backgrounded ) { logger.debug('Convo: resume', {id: this.id}, logger.DebugContext.convo) - const fromStatus = this.status + this.status = ConvoStatus.Ready + this.cancelNextPoll() + this.pollInterval = ACTIVE_POLL_INTERVAL + this.pollEvents() + this.commit() - try { - this.cancelNextPoll() - - this.status = ConvoStatus.Resuming + // intentionally un-awaited + this.refreshConvo().then(() => { this.commit() - - await this.refreshConvo() - this.status = ConvoStatus.Ready - this.commit() - - // throw new Error('UNCOMMENT TO TEST RESUME FAILURE') - - this.pollInterval = ACTIVE_POLL_INTERVAL - 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() - } + }) } else { logger.debug( `Convo: resume called from ${this.status}`, @@ -417,11 +380,8 @@ export class Convo { } } - async background() { - if ( - this.status === ConvoStatus.Ready || - this.status === ConvoStatus.Resuming - ) { + background() { + if (this.status === ConvoStatus.Ready) { logger.debug( 'Convo: backgrounded', {id: this.id}, @@ -433,11 +393,10 @@ export class Convo { } } - async suspend() { + suspend() { if ( this.status === ConvoStatus.Ready || - this.status === ConvoStatus.Backgrounded || - this.status === ConvoStatus.Resuming + this.status === ConvoStatus.Backgrounded ) { logger.debug('Convo: suspended', {id: this.id}, logger.DebugContext.convo) DEBUG_ACTIVE_CHAT = undefined