Make init sync also
This commit is contained in:
+40
-24
@@ -315,37 +315,46 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
init() {
|
||||||
if (
|
if (
|
||||||
this.status === ConvoStatus.Uninitialized ||
|
this.status === ConvoStatus.Uninitialized ||
|
||||||
this.status === ConvoStatus.Error
|
this.status === ConvoStatus.Error
|
||||||
) {
|
) {
|
||||||
logger.debug('Convo: init', {id: this.id}, logger.DebugContext.convo)
|
logger.debug('Convo: init', {id: this.id}, logger.DebugContext.convo)
|
||||||
|
|
||||||
try {
|
this.status = ConvoStatus.Initializing
|
||||||
this.status = ConvoStatus.Initializing
|
this.commit()
|
||||||
this.commit()
|
|
||||||
|
|
||||||
await this.refreshConvo()
|
this.refreshConvo()
|
||||||
this.status = ConvoStatus.Ready
|
.then(async () => {
|
||||||
this.commit()
|
await new Promise(y => setTimeout(y, 2000))
|
||||||
|
if (this.status === ConvoStatus.Initializing) {
|
||||||
|
this.status = ConvoStatus.Ready
|
||||||
|
this.commit()
|
||||||
|
|
||||||
await this.fetchMessageHistory()
|
this.fetchMessageHistory()
|
||||||
|
this.pollEvents()
|
||||||
this.pollEvents()
|
} else {
|
||||||
} catch (e: any) {
|
logger.debug(
|
||||||
logger.error('Convo: failed to init')
|
`Convo: init was canceled`,
|
||||||
this.error = {
|
{},
|
||||||
exception: e,
|
logger.DebugContext.convo,
|
||||||
code: ConvoError.InitFailed,
|
)
|
||||||
retry: () => {
|
}
|
||||||
this.error = undefined
|
})
|
||||||
this.init()
|
.catch(e => {
|
||||||
},
|
logger.error('Convo: failed to init')
|
||||||
}
|
this.error = {
|
||||||
this.status = ConvoStatus.Error
|
exception: e,
|
||||||
this.commit()
|
code: ConvoError.InitFailed,
|
||||||
}
|
retry: () => {
|
||||||
|
this.error = undefined
|
||||||
|
this.init()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
this.status = ConvoStatus.Error
|
||||||
|
this.commit()
|
||||||
|
})
|
||||||
} else if (this.status === ConvoStatus.Suspended) {
|
} else if (this.status === ConvoStatus.Suspended) {
|
||||||
this.resume()
|
this.resume()
|
||||||
} else {
|
} else {
|
||||||
@@ -354,6 +363,7 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO resume failure
|
// TODO resume failure
|
||||||
|
// TODO can I resume from error?
|
||||||
resume() {
|
resume() {
|
||||||
if (
|
if (
|
||||||
this.status === ConvoStatus.Suspended ||
|
this.status === ConvoStatus.Suspended ||
|
||||||
@@ -381,7 +391,11 @@ export class Convo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
background() {
|
background() {
|
||||||
if (this.status === ConvoStatus.Ready) {
|
if (
|
||||||
|
this.status === ConvoStatus.Initializing ||
|
||||||
|
this.status === ConvoStatus.Ready ||
|
||||||
|
this.status === ConvoStatus.Error
|
||||||
|
) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
'Convo: backgrounded',
|
'Convo: backgrounded',
|
||||||
{id: this.id},
|
{id: this.id},
|
||||||
@@ -395,7 +409,9 @@ export class Convo {
|
|||||||
|
|
||||||
suspend() {
|
suspend() {
|
||||||
if (
|
if (
|
||||||
|
this.status === ConvoStatus.Initializing ||
|
||||||
this.status === ConvoStatus.Ready ||
|
this.status === ConvoStatus.Ready ||
|
||||||
|
this.status === ConvoStatus.Error ||
|
||||||
this.status === ConvoStatus.Backgrounded
|
this.status === ConvoStatus.Backgrounded
|
||||||
) {
|
) {
|
||||||
logger.debug('Convo: suspended', {id: this.id}, logger.DebugContext.convo)
|
logger.debug('Convo: suspended', {id: this.id}, logger.DebugContext.convo)
|
||||||
|
|||||||
Reference in New Issue
Block a user