Get rid of ad-hoc status checks

This commit is contained in:
Eric Bailey
2024-05-07 11:27:25 -05:00
parent 3baaa94f0e
commit d8682d4a5f
+42 -45
View File
@@ -372,15 +372,16 @@ export class Convo {
this.status = ConvoStatus.Ready this.status = ConvoStatus.Ready
this.pollInterval = ACTIVE_POLL_INTERVAL this.pollInterval = ACTIVE_POLL_INTERVAL
this.fetchMessageHistory().then(() => { this.fetchMessageHistory().then(() => {
this.initiatePoll() this.restartPoll()
}) })
break break
} }
case ConvoDispatchEvent.Background: { case ConvoDispatchEvent.Background: {
this.status = ConvoStatus.Backgrounded this.status = ConvoStatus.Backgrounded
this.pollInterval = BACKGROUND_POLL_INTERVAL this.pollInterval = BACKGROUND_POLL_INTERVAL
// TODO truncate history, then poll again this.fetchMessageHistory().then(() => {
this.initiatePoll() this.restartPoll()
})
break break
} }
case ConvoDispatchEvent.Suspend: { case ConvoDispatchEvent.Suspend: {
@@ -399,21 +400,24 @@ export class Convo {
switch (action.event) { switch (action.event) {
case ConvoDispatchEvent.Resume: { case ConvoDispatchEvent.Resume: {
this.refreshConvo() this.refreshConvo()
this.initiatePoll() this.restartPoll()
break break
} }
case ConvoDispatchEvent.Background: { case ConvoDispatchEvent.Background: {
this.status = ConvoStatus.Backgrounded this.status = ConvoStatus.Backgrounded
this.pollInterval = BACKGROUND_POLL_INTERVAL this.pollInterval = BACKGROUND_POLL_INTERVAL
this.restartPoll()
break break
} }
case ConvoDispatchEvent.Suspend: { case ConvoDispatchEvent.Suspend: {
this.status = ConvoStatus.Suspended this.status = ConvoStatus.Suspended
this.cancelNextPoll()
break break
} }
case ConvoDispatchEvent.Error: { case ConvoDispatchEvent.Error: {
this.status = ConvoStatus.Error this.status = ConvoStatus.Error
this.error = action.payload this.error = action.payload
this.cancelNextPoll()
break break
} }
} }
@@ -425,16 +429,19 @@ export class Convo {
this.status = ConvoStatus.Ready this.status = ConvoStatus.Ready
this.pollInterval = ACTIVE_POLL_INTERVAL this.pollInterval = ACTIVE_POLL_INTERVAL
this.refreshConvo() this.refreshConvo()
this.initiatePoll() // TODO truncate history if needed
this.restartPoll()
break break
} }
case ConvoDispatchEvent.Suspend: { case ConvoDispatchEvent.Suspend: {
this.status = ConvoStatus.Suspended this.status = ConvoStatus.Suspended
this.cancelNextPoll()
break break
} }
case ConvoDispatchEvent.Error: { case ConvoDispatchEvent.Error: {
this.status = ConvoStatus.Error this.status = ConvoStatus.Error
this.error = action.payload this.error = action.payload
this.cancelNextPoll()
break break
} }
} }
@@ -446,14 +453,15 @@ export class Convo {
this.status = ConvoStatus.Ready this.status = ConvoStatus.Ready
this.pollInterval = ACTIVE_POLL_INTERVAL this.pollInterval = ACTIVE_POLL_INTERVAL
this.refreshConvo() this.refreshConvo()
this.initiatePoll() // TODO truncate history if needed
this.restartPoll()
break break
} }
case ConvoDispatchEvent.Resume: { case ConvoDispatchEvent.Resume: {
this.status = ConvoStatus.Ready this.status = ConvoStatus.Ready
this.pollInterval = ACTIVE_POLL_INTERVAL this.pollInterval = ACTIVE_POLL_INTERVAL
this.refreshConvo() this.refreshConvo()
this.initiatePoll() this.restartPoll()
break break
} }
case ConvoDispatchEvent.Error: { case ConvoDispatchEvent.Error: {
@@ -731,7 +739,7 @@ export class Convo {
} }
} }
private initiatePoll() { private restartPoll() {
this.cancelNextPoll() this.cancelNextPoll()
this.pollLatestEvents() this.pollLatestEvents()
} }
@@ -741,47 +749,36 @@ export class Convo {
} }
private pollLatestEvents() { private pollLatestEvents() {
if ( /*
this.status === ConvoStatus.Ready || * Uncomment to view poll events
this.status === ConvoStatus.Backgrounded */
) { logger.debug('Convo: poll events', {id: this.id}, logger.DebugContext.convo)
/*
* Uncomment to view poll events
*/
logger.debug(
'Convo: poll events',
{id: this.id},
logger.DebugContext.convo,
)
try { try {
this.fetchLatestEvents().then(({events}) => { this.fetchLatestEvents().then(({events}) => {
this.applyLatestEvents(events) this.applyLatestEvents(events)
}) })
this.nextPoll = setTimeout(() => { this.nextPoll = setTimeout(() => {
this.pollLatestEvents()
}, this.pollInterval)
} catch (e: any) {
logger.error('Convo: poll events failed')
this.cancelNextPoll()
this.footerItems.set(ConvoItemError.PollFailed, {
type: 'error-recoverable',
key: ConvoItemError.PollFailed,
code: ConvoItemError.PollFailed,
retry: () => {
this.footerItems.delete(ConvoItemError.PollFailed)
this.commit()
this.pollLatestEvents() this.pollLatestEvents()
}, this.pollInterval) },
} catch (e: any) { })
logger.error('Convo: poll events failed')
this.cancelNextPoll() this.commit()
this.footerItems.set(ConvoItemError.PollFailed, {
type: 'error-recoverable',
key: ConvoItemError.PollFailed,
code: ConvoItemError.PollFailed,
retry: () => {
this.footerItems.delete(ConvoItemError.PollFailed)
this.commit()
this.pollLatestEvents()
},
})
this.commit()
}
} }
return
} }
private pendingFetchLatestEvents: private pendingFetchLatestEvents: