forked from fair/matrix-puppeteer-line
Fix problems with outgoing messages
1. Don't set the "own message" promise to null on resolve, as other places may still be awaiting on it afterwards 2. Retry pressing "Enter" to send a message if it doesn't work on the first try, which can happen for some reason
This commit is contained in:
parent
e1b822bd52
commit
69028c6edd
|
@ -95,6 +95,7 @@ class MautrixController {
|
|||
this.pinAppearObserver = null
|
||||
this.ownID = null
|
||||
|
||||
this.ownMsgPromise = Promise.resolve(-1)
|
||||
this._promiseOwnMsgReset()
|
||||
}
|
||||
|
||||
|
@ -474,13 +475,20 @@ class MautrixController {
|
|||
}, timeoutLimitMillis)
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we're waiting for a Matrix-sent message to resolve.
|
||||
*/
|
||||
_isWaitingForOwnMessage() {
|
||||
return !!this.promiseOwnMsgResolve
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a user-sent message to finish getting sent.
|
||||
*
|
||||
* @return {Promise<number>} - The ID of the sent message.
|
||||
*/
|
||||
async waitForOwnMessage() {
|
||||
return this.ownMsgPromise ? await this.ownMsgPromise : -1
|
||||
return await this.ownMsgPromise
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -867,7 +875,7 @@ class MautrixController {
|
|||
addChatListObserver() {
|
||||
this.removeChatListObserver()
|
||||
this.chatListObserver = new MutationObserver(async (mutations) => {
|
||||
if (this.ownMsgPromise) {
|
||||
if (this._isWaitingForOwnMessage()) {
|
||||
// Wait for pending sent messages to be resolved before responding to mutations
|
||||
try {
|
||||
await this.ownMsgPromise
|
||||
|
@ -1213,8 +1221,7 @@ class MautrixController {
|
|||
}
|
||||
|
||||
_observeOwnMessage(ownMsg) {
|
||||
if (!this.ownMsgPromise) {
|
||||
// Not waiting for a pending sent message
|
||||
if (!this._isWaitingForOwnMessage()) {
|
||||
return null
|
||||
}
|
||||
|
||||
|
@ -1299,7 +1306,6 @@ class MautrixController {
|
|||
}
|
||||
|
||||
_promiseOwnMsgReset() {
|
||||
this.ownMsgPromise = null
|
||||
this.promiseOwnMsgSuccessSelector = null
|
||||
this.promiseOwnMsgFailureSelector = null
|
||||
this.promiseOwnMsgResolve = null
|
||||
|
|
|
@ -790,7 +790,18 @@ export default class MessagesPuppeteer {
|
|||
// Setting its innerText directly works fine though...
|
||||
await input.click()
|
||||
await input.evaluate((e, text) => e.innerText = text, text)
|
||||
await input.press("Enter")
|
||||
while (true) {
|
||||
await input.press("Enter")
|
||||
try {
|
||||
await this.page.waitForFunction(
|
||||
e => e.innerText == "",
|
||||
{timeout: 500},
|
||||
input)
|
||||
break
|
||||
} catch (e) {
|
||||
this.error(`Failed to press Enter when sending message, try again (${e})`)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return await this._waitForSentMessage(chatID)
|
||||
|
|
Loading…
Reference in New Issue