UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

86 lines 2.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ioMixin = void 0; const wechaty_puppet_1 = require("@juzi/wechaty-puppet"); const io_js_1 = require("../io.js"); /** * Huan(202111): we should not include the IO logic internally * * TODO: remove all IO related logics from Wechaty internal */ const ioMixin = (mixinBase) => { wechaty_puppet_1.log.verbose('WechatyIoMixin', 'ioMixin(%s)', mixinBase.name); class IoMixin extends mixinBase { __io; get io() { if (!this.__io) { throw new Error('NO IO'); } return this.__io; } __ioToken; constructor(...args) { wechaty_puppet_1.log.verbose('WechatyIoMixin', 'constructor()'); super(...args); const options = args[0] || {}; if (options.ioToken) { this.__ioToken = options.ioToken; } } async start() { wechaty_puppet_1.log.verbose('WechatyIoMixin', 'start()'); await super.start(); if (!this.__ioToken) { return; } /** * Clean the memory leak-ed io (?) */ if (this.__io) { wechaty_puppet_1.log.error('WechatyIoMixin', 'start() found existing io instance: stopping...'); try { await this.__io.stop(); } catch (e) { this.emitError(e); } wechaty_puppet_1.log.error('WechatyIoMixin', 'start() found existing io instance: stopping... done'); this.__io = undefined; } /** * Initialize IO instance */ this.__io = new io_js_1.Io({ token: this.__ioToken, wechaty: this, // <- FIXME: remove any, Huan(202111) }); wechaty_puppet_1.log.verbose('WechatyIoMixin', 'start() starting io ...'); await this.__io.start(); wechaty_puppet_1.log.verbose('WechatyIoMixin', 'start() starting io ... done'); } async stop() { wechaty_puppet_1.log.verbose('WechatyIoMixin', 'stop()'); try { if (!this.__io) { return; } const io = this.__io; this.__io = undefined; try { wechaty_puppet_1.log.verbose('WechatyIoMixin', 'stop() starting io ...'); await io.stop(); wechaty_puppet_1.log.verbose('WechatyIoMixin', 'stop() starting io ... done'); } catch (e) { this.emitError(e); } } finally { await super.stop(); } } } return IoMixin; }; exports.ioMixin = ioMixin; //# sourceMappingURL=io-mixin.js.map