UNPKG

wechaty-puppet

Version:

Abstract Puppet for Wechaty

137 lines 5.03 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PuppetSkeleton = void 0; /** * Wechaty - https://github.com/wechaty/wechaty * * @copyright 2016-2018 Huan LI <zixia@zixia.net> * * 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. * */ const UUID = __importStar(require("uuid")); const config_js_1 = require("../config.js"); /** * Issue #165 - ReferenceError: Cannot access 'PuppetSkeleton' before initialization * @see https://github.com/wechaty/puppet/issues/165 */ const gerror_1 = require("gerror"); const events_js_1 = require("./events.js"); class PuppetSkeleton extends events_js_1.PuppetEventEmitter { /** * Puppet ID (UUID) * * Issue #160 - puppet.id will change to puppet.loggedInUserId #160 * - `id` is NOT logged in user ID * - `currentUserId` is the logged in user ID * @see https://github.com/wechaty/puppet/issues/160 */ id; options; /** * Wrap promise in sync way (catch error by emitting it) * 1. convert a async callback function to be sync function * by catcing any errors and emit them to error event * 2. wrap a Promise by catcing any errors and emit them to error event */ wrapAsync = (0, gerror_1.wrapAsyncError)((e) => this.emit('error', e)); /** * Huan(202110): keep constructor with `args: any[]` parameters * because mixins required it * * We will save args[0] to `this.options` so that all mixins can access it * @param args */ constructor(...args) { super(); config_js_1.log.verbose('PuppetSkeleton', 'constructor(%s)', args.length ? JSON.stringify(args[0]) : ''); this.id = UUID.v4(); this.options = args[0] || {}; /** * We will use RxJS & Redux to handle the state of the puppet * which means there will be lots of listeners will be registered later. */ this.setMaxListeners(1024); } /** * Huan(202110): Issue #156 - https://github.com/wechaty/puppet/issues/156 * * All mixins should implemente both `start()` and `stop()`, * and they must call `super.start()` and `super.stop()` * so that all start()/stop() calls can be chained through all mixins. */ async start() { config_js_1.log.verbose('PuppetSkeleton', 'start()'); } async stop() { config_js_1.log.verbose('PuppetSkeleton', 'stop()'); } /** * Convert any error payload to GError , * and re-emit a `error` event with EventErrorPayload(GError) */ emit(event, ...args) { if (event !== 'error') { return super.emit(event, ...args); } const arg0 = args[0]; let gerr; if (arg0 instanceof gerror_1.GError) { gerr = arg0; } else if (arg0 && typeof arg0 === 'object' && 'gerror' in arg0) { /** * in case of the `args` is an `EventErrorPayload` */ gerr = gerror_1.GError.from(arg0.gerror); } else { gerr = gerror_1.GError.from(arg0); } /** * Convert everything to a GError toJSON object * as EventErrorPayload */ const payload = { gerror: JSON.stringify(gerr), }; return super.emit('error', payload); } } exports.PuppetSkeleton = PuppetSkeleton; //# sourceMappingURL=puppet-skeleton.js.map