UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

93 lines 3.12 kB
/** * Wechaty Chatbot SDK - https://github.com/wechaty/wechaty * * @copyright 2016 Huan LI (李卓桓) <https://github.com/huan>, and * Wechaty Contributors <https://github.com/wechaty>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import { MemoryCard } from 'memory-card'; import { log } from '@juzi/wechaty-puppet'; import * as UUID from 'uuid'; import { WechatyEventEmitter, } from '../schemas/mod.js'; class WechatySkeleton extends WechatyEventEmitter { static log = log; log = log; /** * the UUID of the Wechaty * @ignore */ id; __memory; get memory() { if (!this.__memory) { throw new Error('NOMEMORY'); } return this.__memory; } __options; constructor(...args) { log.verbose('WechatySkeleton', 'constructor()'); super(); this.id = UUID.v4(); this.__options = args[0] || {}; /** * Huan(202008): * * Set max listeners to 1K, so that we can add lots of listeners without the warning message. * The listeners might be one of the following functionilities: * 1. Plugins * 2. Redux Observables * 3. etc... */ super.setMaxListeners(1024); } /** * Initialize the Wechaty instance for ready to be started. * * 1. It will be called automatically by the start() * 2. It should be allowed for being called multiple times in the same instance, * by skipping the second time initialization. */ async init() { log.verbose('WechatySkeleton', 'init()'); if (!this.__memory) { this.__memory = new MemoryCard(this.__options.name); try { await this.__memory.load(); } catch (_) { log.silly('WechatySkeleton', 'onStart() memory.load() had already loaded'); } } } async start() { log.verbose('WechatySkeleton', 'start()'); // no super.start() /** * Huan(202203): Call the init() functions (with super.init() inside them for chaining) */ await this.init(); } async stop() { log.verbose('WechatySkeleton', 'stop()'); // no super.stop() } on(event, listener) { log.verbose('WechatySkeleton', 'on(%s, listener) registering... listenerCount: %s', event, this.listenerCount(event)); return super.on(event, listener); } } export { WechatySkeleton, }; //# sourceMappingURL=wechaty-skeleton.js.map