提交 d85ba62f 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

fix init race condition

上级 dd2238de
......@@ -31,7 +31,7 @@ import Profile from '../profile'
import Bridge from './bridge'
test.only('PuppetWebBridge', async t => {
test('PuppetWebBridge', async t => {
const profile = new Profile()
const bridge = new Bridge({ profile })
try {
......
......@@ -128,7 +128,7 @@ export class Bridge extends EventEmitter {
this.emit('error', new Error(`${dialog.type}(${dialog.message()})`))
}
const onLoad = async () => {
const onLoad = async (done: () => void) => {
log.verbose('PuppetWebBridge', 'initPage() on(load) %s', page.url())
if (this.state.off()) {
......@@ -168,13 +168,16 @@ export class Bridge extends EventEmitter {
log.error('PuppetWebBridge', 'init() initPage() onLoad() exception: %s', e)
this.emit('error', e)
} finally {
done()
}
}
page.on('dialog', onDialog)
page.on('load', onLoad)
page.on('error', e => this.emit('error', e))
const loaded = new Promise(resolve => page.on('load', () => onLoad(resolve)))
///////////////////
const cookieList = this.options.profile.get('cookies') as Cookie[]
......@@ -190,6 +193,8 @@ export class Bridge extends EventEmitter {
await page.reload() // reload page to make effect of the new cookie.
}
await loaded // wait the page on load finish
return page
}
......@@ -677,7 +682,7 @@ export class Bridge extends EventEmitter {
return resolve()
}
const ret = +obj.error.ret
const message = obj.error.message as string
const message = obj.error.message
const e = new Error(message)
......
......@@ -71,12 +71,12 @@ test('retryPromise()', async t => {
declare const WechatyBro
test.only('WechatyBro.ding()', async t => {
test('WechatyBro.ding()', async t => {
const profile = new Profile(Math.random().toString(36).substr(2, 5))
const bridge = new Bridge({
profile,
})
t.ok(bridge, 'should instanciated a bridge with mocked puppet')
t.ok(bridge, 'should instanciated a bridge')
try {
await bridge.init()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册