Exemplo n.º 1
0
  public async quit(): Promise<void> {
    log.verbose('IoClient', 'quit()')

    if (this.state.off() === 'pending') {
      log.warn('IoClient', 'quit() with state.off() = `pending`, skipped')
      throw new Error('quit() with state.off() = `pending`')
    }

    this.state.off('pending')

    try {
      if (this.options.wechaty) {
        await this.options.wechaty.stop()
        // this.wechaty = null
      } else { log.warn('IoClient', 'quit() no this.wechaty') }

      if (this.io) {
        await this.io.stop()
        // this.io = null
      } else { log.warn('IoClient', 'quit() no this.io') }

    } catch (e) {
      log.error('IoClient', 'exception: %s', e.message)
      throw e
    } finally {
      this.state.off(true)
    }

    return

  }
Exemplo n.º 2
0
  public async quit(): Promise<void> {
    log.verbose('PuppetPuppeteerBridge', 'quit()')

    if (!this.page) {
      throw new Error('no page')
    }
    if (!this.browser) {
      throw new Error('no browser')
    }

    this.state.off('pending')

    try {
      await this.page.close()
      log.silly('PuppetPuppeteerBridge', 'quit() page.close()-ed')
    } catch (e) {
      log.warn('PuppetPuppeteerBridge', 'quit() page.close() exception: %s', e)
    }

    try {
      await this.browser.close()
      log.silly('PuppetPuppeteerBridge', 'quit() browser.close()-ed')
    } catch (e) {
      log.warn('PuppetPuppeteerBridge', 'quit() browser.close() exception: %s', e)
    }

    this.state.off(true)
  }
Exemplo n.º 3
0
  public async init(): Promise<void> {
    log.verbose('PuppetPuppeteerBridge', 'init()')

    this.state.on('pending')
    try {
      this.browser = await this.initBrowser()
      log.verbose('PuppetPuppeteerBridge', 'init() initBrowser() done')

      this.on('load', this.onLoad.bind(this))

      const ready = new Promise(resolve => this.once('ready', resolve))
      this.page = await this.initPage(this.browser)
      await ready

      this.state.on(true)
      log.verbose('PuppetPuppeteerBridge', 'init() initPage() done')
    } catch (e) {
      log.error('PuppetPuppeteerBridge', 'init() exception: %s', e)
      this.state.off(true)

      try {
        if (this.page) {
          await this.page.close()
        }
        if (this.browser) {
          await this.browser.close()
        }
      } catch (e2) {
        log.error('PuppetPuppeteerBridge', 'init() exception %s, close page/browser exception %s', e, e2)
      }

      this.emit('error', e)
      throw e
    }
  }
Exemplo n.º 4
0
  public async onLoad(page: Page): Promise<void> {
    log.verbose('PuppetPuppeteerBridge', 'initPage() on(load) %s', page.url())

    if (this.state.off()) {
      log.verbose('PuppetPuppeteerBridge', 'initPage() onLoad() OFF state detected. NOP')
      return // reject(new Error('onLoad() OFF state detected'))
    }

    try {
      const emitExist = await page.evaluate(() => {
        return typeof window['emit'] === 'function'
      })
      if (!emitExist) {
        await page.exposeFunction('emit', this.emit.bind(this))
      }

      await this.readyAngular(page)
      await this.inject(page)
      await this.clickSwitchAccount(page)

      this.emit('ready')

    } catch (e) {
      log.error('PuppetPuppeteerBridge', 'init() initPage() onLoad() exception: %s', e)
      await page.close()
      this.emit('error', e)
    }
  }
Exemplo n.º 5
0
  public async stop(): Promise<void> {
    log.verbose('IoClient', 'stop()')

    this.state.off('pending')

    // XXX
    if (!this.io) {
      log.warn('IoClient', 'stop() without this.io')
      this.state.off(true)
      return
    }

    await this.io.stop()
    this.state.off(true)

    // XXX 20161026
    // this.io = null
    return
  }
Exemplo n.º 6
0
  private async hookWechaty(wechaty: Wechaty): Promise<void> {
    log.verbose('IoClient', 'initWechaty()')

    if (this.state.off()) {
      const e = new Error('state.off() is true, skipped')
      log.warn('IoClient', 'initWechaty() %s', e.message)
      throw e
    }

    wechaty
    .on('login'	     , user => log.info('IoClient', `${user.name()} logined`))
    .on('logout'	   , user => log.info('IoClient', `${user.name()} logouted`))
    .on('scan', (url, code) => log.info('IoClient', `[${code}] ${url}`))
    .on('message'     , msg => this.onMessage(msg))

    return
  }
Exemplo n.º 7
0
  private async initIo(): Promise<void> {
    log.verbose('IoClient', 'initIo() with token %s', this.options.token)

    if (this.state.off()) {
      const e = new Error('initIo() state.off() is true, skipped')
      log.warn('IoClient', e.message)
      throw e
    }

    try {
      await this.io.start()
    } catch (e) {
      log.verbose('IoClient', 'initIo() init fail: %s', e.message)
      throw e
    }

    return
  }
Exemplo n.º 8
0
  public async start(): Promise<void> {
    log.verbose('IoClient', 'init()')

    if (this.state.pending()) {
      log.warn('IoClient', 'start() with a pending state, not the time')
      const e = new Error('state.pending() when start()')
      throw e
    }

    this.state.on('pending')

    try {
      await this.initIo()
      await this.hookWechaty(this.options.wechaty)
      this.state.on(true)
    } catch (e) {
      log.error('IoClient', 'init() exception: %s', e.message)
      this.state.off(true)
      throw e
    }
    return
  }