UNPKG

@juzi/wechaty

Version:

Wechaty is a RPA SDK for Chatbot Makers.

56 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pluginMixin = void 0; const wechaty_puppet_1 = require("@juzi/wechaty-puppet"); const plugin_js_1 = require("../plugin.js"); const pluginMixin = (mixinBase) => { wechaty_puppet_1.log.verbose('WechatyPluginMixin', 'pluginMixin(%s)', mixinBase.name); class PluginMixin extends mixinBase { constructor(...args) { super(...args); } /** * @param {WechatyPlugin[]} plugins - The plugins you want to use * * @return {WechatyInterface} - this for chaining, * * @desc * For wechaty ecosystem, allow user to define a 3rd party plugin for the all wechaty instances * * @example * // Report all chat message to my server. * * function WechatyReportPlugin(options: { url: string }) { * return function (this: Wechaty) { * this.on('message', message => http.post(options.url, { data: message })) * } * } * * bot.use(WechatyReportPlugin({ url: 'http://somewhere.to.report.your.data.com' }) */ use(...plugins) { const pluginList = plugins.flat(); wechaty_puppet_1.log.verbose('WechatyPluginMixin', 'use() total %d plugins', pluginList.length); const uninstallerList = []; for (const plugin of pluginList) { wechaty_puppet_1.log.verbose('WechatyPluginMixin', 'use() installing Plugin %s on Wechaty %s ...', plugin.name, this.name()); const uninstaller = plugin(this); // <- Huan(202110): TODO: remove any if ((0, plugin_js_1.isWechatyPluginUninstaller)(uninstaller)) { wechaty_puppet_1.log.verbose('WechatyPluginMixin', 'use() saving uninstaller for Plugin %s on Wechaty %s ...', plugin.name, this.name()); uninstallerList.push(uninstaller); } } /** * Return the function to uninstall all plugins */ return () => uninstallerList.forEach(uninstaller => { wechaty_puppet_1.log.verbose('WechatyPluginMixin', 'use() uninstalling Plugin %s on Wechaty %s ...', uninstaller.name, this.name()); uninstaller(); wechaty_puppet_1.log.verbose('WechatyPluginMixin', 'use() uninstalling Plugin %s on Wechaty %s ... done', uninstaller.name, this.name()); }); } } return PluginMixin; }; exports.pluginMixin = pluginMixin; //# sourceMappingURL=plugin-mixin.js.map