Use functions that return a Promise

This commit is contained in:
Andrew Ferrazzutti 2021-02-10 03:24:28 -05:00
parent 85814f9793
commit e8592dcc8b
3 changed files with 19 additions and 20 deletions

View File

@ -82,8 +82,9 @@ async def login_do(evt: CommandEvent, gen: AsyncGenerator[Tuple[str, str], None]
pin_event_id = await evt.az.intent.send_message(evt.room_id, content) pin_event_id = await evt.az.intent.send_message(evt.room_id, content)
elif item[0] in ("failure", "error"): elif item[0] in ("failure", "error"):
# TODO Handle errors differently? # TODO Handle errors differently?
reason = item[1]
failure = True failure = True
reason = item[1]
if reason:
content = TextMessageEventContent(body=reason, msgtype=MessageType.NOTICE) content = TextMessageEventContent(body=reason, msgtype=MessageType.NOTICE)
await evt.az.intent.send_message(evt.room_id, content) await evt.az.intent.send_message(evt.room_id, content)
# else: pass # else: pass

View File

@ -79,7 +79,7 @@ class Client(RPCClient):
event.set() event.set()
async def failure_handler(req: LoginCommand) -> None: async def failure_handler(req: LoginCommand) -> None:
data.append(("failure", req["reason"])) data.append(("failure", req.get("reason")))
event.set() event.set()
async def cancel_watcher() -> None: async def cancel_watcher() -> None:

View File

@ -150,7 +150,7 @@ export default class MessagesPuppeteer {
case "email": { case "email": {
this.log("Running email login") this.log("Running email login")
if (!login_data) { if (!login_data) {
_sendLoginFailure("No login credentials provided for email login") this._sendLoginFailure("No login credentials provided for email login")
return return
} }
@ -169,7 +169,7 @@ export default class MessagesPuppeteer {
} }
// TODO Phone number login // TODO Phone number login
default: default:
_sendLoginFailure(`Invalid login type: ${login_type}`) this._sendLoginFailure(`Invalid login type: ${login_type}`)
return return
} }
@ -181,20 +181,17 @@ export default class MessagesPuppeteer {
this.log("Waiting for login response") this.log("Waiting for login response")
let doneWaiting = false let doneWaiting = false
let loginSuccess = false let loginSuccess = false
const cancelableResolve = (promiseWithShortTimeout) => { const cancelableResolve = (promiseFn) => {
const executor = (resolve, reject) => { const executor = (resolve, reject) => {
promiseWithShortTimeout.then( promiseFn().then(
value => { value => {
this.log(`Done: ${value}`)
doneWaiting = true doneWaiting = true
resolve(value) resolve(value)
}, },
reason => { reason => {
if (!doneWaiting) { if (!doneWaiting) {
this.log(`Not done, waiting some more. ${reason}`) setTimeout(executor, 1000, resolve, reject)
setTimeout(executor, 3000, resolve, reject)
} else { } else {
this.log(`Final fail. ${reason}`)
resolve() resolve()
} }
} }
@ -204,25 +201,27 @@ export default class MessagesPuppeteer {
} }
const result = await Promise.race([ const result = await Promise.race([
this.page.waitForSelector("#wrap_message_sync", {timeout: 2000}) () => this.page.waitForSelector("#wrap_message_sync", {timeout: 2000})
.then(element => { .then(element => {
loginSuccess = true loginSuccess = true
return element return element
}), }),
this.page.waitForSelector("#login_incorrect", {visible: true, timeout: 2000}) () => this.page.waitForSelector("#login_incorrect", {visible: true, timeout: 2000})
.then(element => element.innerText), .then(element => element.innerText),
this._waitForLoginCancel(), () => this._waitForLoginCancel(),
].map(promise => cancelableResolve(promise))) ].map(promiseFn => cancelableResolve(promiseFn)))
this.log("Removing observers") this.log("Removing observers")
await this.page.evaluate(() => window.__mautrixController.removeQRChangeObserver()) await this.page.evaluate(() => window.__mautrixController.removeQRChangeObserver())
await this.page.evaluate(() => window.__mautrixController.removeLoginChildrenObserver(element)) await this.page.evaluate(() => window.__mautrixController.removeQRAppearObserver())
await this.page.evaluate(() => window.__mautrixController.removeEmailAppearObserver())
await this.page.evaluate(() => window.__mautrixController.removePINAppearObserver())
await this.page.evaluate(() => window.__mautrixController.removeExpiryObserver()) await this.page.evaluate(() => window.__mautrixController.removeExpiryObserver())
delete this.login_email delete this.login_email
delete this.login_password delete this.login_password
if (!loginSuccess) { if (!loginSuccess) {
_sendLoginFailure(result) this._sendLoginFailure(result)
return return
} }
@ -246,13 +245,12 @@ export default class MessagesPuppeteer {
async cancelLogin() { async cancelLogin() {
if (this.loginRunning) { if (this.loginRunning) {
this.loginCancelled = true this.loginCancelled = true
//await this._preparePage(false) await this._preparePage(false)
} }
} }
_waitForLoginCancel() { _waitForLoginCancel() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log(`>>>>> ${this.loginCancelled}`)
if (this.loginCancelled) { if (this.loginCancelled) {
resolve() resolve()
} else { } else {