UNPKG

vue-app-sdk

Version:
177 lines (176 loc) 4.84 kB
"use strict"; var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const index = require("../utils/index2.cjs"); const vue = require("vue"); const Hookable = require("./Hookable.cjs"); const Router = require("./Router.cjs"); const APP_KEY = Symbol("Vue App"); const APP_SDK_KEY = Symbol("App SDK"); function useAppSDK() { return vue.inject(APP_SDK_KEY); } class AppSDK extends Hookable.AppSDKHookable { constructor(options = {}) { super(); /** * 是否已初始化 */ __publicField(this, "isInitialed", false); /** * Vue 实例 */ __publicField(this, "app"); /** * VueRouter 实例 */ __publicField(this, "router"); /** * 插件列表 */ __publicField(this, "_plugins", []); /** * 内置插件列表 * @readonly */ __publicField(this, "_builtinPlugins", []); /** * 注册插件 * @param plugin 插件 * * @example * ```ts * sdk.use(Plugin1).use(Plugin2) * ``` */ __publicField(this, "use", (plugin) => { const index$1 = this._plugins.findIndex((p) => p.id === plugin.id); if (index$1 > -1) { index.logger.warn(`插件 "${plugin.id}" 已注册,将被覆盖!`); this._plugins.splice(index$1, 1); } this._plugins.push(plugin); return this; }); /** * 根据插件 ID 获取插件实例 * @param id 插件 ID * * @example * ```ts * const Plugin1 = sdk.getPlugin(Plugin1ID)! * Plugin1.run() * ``` */ __publicField(this, "getPlugin", (id) => { return this._allPlugins.find((p) => p.id === id); }); /** * 根据插件 ID 列表获取插件实例列表 * @param ids 插件 ID 列表 * * @example * ```ts * const [Plugin1, Plugin2] = sdk.getPlugins(Plugin1ID, Plugin2ID) * * Plugin1?.run() * Plugin2?.test() * ``` */ __publicField(this, "getPlugins", (...ids) => { return ids.map((id) => this.getPlugin(id)); }); /** * 集中清理插件缓存,不会影响插件正常运行 */ __publicField(this, "cleanup", () => { this.callHookSync("sdk:cleanup"); }); /** * 安装插件列表 */ __publicField(this, "_installPlugins", () => { this._uninstallPlugins(); this._allPlugins.forEach((plugin) => { var _a; const uninstall = plugin.install(this); plugin._un = uninstall || ((_a = plugin.uninstall) == null ? void 0 : _a.bind(plugin, this)); }); }); /** * 卸载插件列表 */ __publicField(this, "_uninstallPlugins", () => { this._allPlugins.forEach((p) => { var _a; (_a = p._un) == null ? void 0 : _a.call(p); p._un = void 0; }); }); /** * 手动初始化 AppSDK * @param app Vue 应用实例 */ __publicField(this, "init", (app) => { if (this.isInitialed) return; this.isInitialed = true; const { unmount } = app; app.unmount = (...args) => { this.destroy(); return unmount(...args); }; const props = app.config.globalProperties; const router = props.$router; if (!router) throw new Error(index.logger.format("请先安装 vue-router!")); this.app = app; this.router = router; props.$appSDK = this; app.provide(APP_KEY, app); app.provide(APP_SDK_KEY, this); this._installPlugins(); }); /** * 手动销毁 AppSDK,会依次执行清理缓存、卸载插件、移除全部事件监听并释放内部数据。 */ __publicField(this, "destroy", () => { this.cleanup(); this._uninstallPlugins(); this.removeAllHooks(); this._plugins.length = 0; this.app = void 0; this.router = void 0; this.isInitialed = false; }); /** * 安装到 Vue 实例 * @param app Vue 应用实例 */ __publicField(this, "install", (app) => { this.init(app); }); this.options = options; this._builtinPlugins = [new Router.Router(this.options.router)]; } /** * 插件列表 * @readonly */ get plugins() { return this._plugins; } /** * 全部插件列表 * @readonly */ get _allPlugins() { return [...this._builtinPlugins, ...this._plugins]; } } exports.APP_KEY = APP_KEY; exports.APP_SDK_KEY = APP_SDK_KEY; exports.AppSDK = AppSDK; exports.useAppSDK = useAppSDK;