UNPKG

vue-app-sdk

Version:
148 lines (147 loc) 3.5 kB
import { IfUnknown, SetRequired } from '@rhao/types-base'; import { Router } from 'vue-router'; import { InferPlugin, Plugin } from './Plugin'; import { RouterOptions } from './Router'; import { App, InjectionKey } from 'vue'; import { AppSDKHookable } from './Hookable'; /** * AppSDK 内部实例 */ export type AppSDKInternalInstance = SetRequired<AppSDK, 'app' | 'router'>; export interface AppSDKHooks { /** * AppSDK 执行 `cleanup` 和 `destroy` 时触发,用于清理内存占用,推荐清理不影响插件或功能正常运行的数据 */ 'sdk:cleanup': () => void; } export interface AppSDKOptions { /** * 路由配置 */ router?: RouterOptions; } /** * Vue Instance Injection Key */ export declare const APP_KEY: InjectionKey<App<any>>; /** * AppSDK Injection Key */ export declare const APP_SDK_KEY: InjectionKey<AppSDKInternalInstance>; /** * 组件 `setup()` 中获取 AppSDK 实例 */ export declare function useAppSDK(): AppSDKInternalInstance; /** * SDK for VueApp */ export declare class AppSDK extends AppSDKHookable<AppSDKHooks> { /** * 配置项 */ options: AppSDKOptions; constructor( /** * 配置项 */ options?: AppSDKOptions); /** * 是否已初始化 */ isInitialed: boolean; /** * Vue 实例 */ app?: App; /** * VueRouter 实例 */ router?: Router; /** * 插件列表 */ private _plugins; /** * 插件列表 * @readonly */ get plugins(): Plugin[]; /** * 内置插件列表 * @readonly */ private _builtinPlugins; /** * 全部插件列表 * @readonly */ private get _allPlugins(); /** * 注册插件 * @param plugin 插件 * * @example * ```ts * sdk.use(Plugin1).use(Plugin2) * ``` */ use: (plugin: Plugin) => this; /** * 根据插件 ID 获取插件实例 * @param id 插件 ID * * @example * ```ts * const Plugin1 = sdk.getPlugin(Plugin1ID)! * Plugin1.run() * ``` */ getPlugin: <T, ID extends Plugin["id"] = string | import('./Plugin').PluginID<unknown>>(id: ID) => IfUnknown<T, InferPlugin<ID>, T> | undefined; /** * 根据插件 ID 列表获取插件实例列表 * @param ids 插件 ID 列表 * * @example * ```ts * const [Plugin1, Plugin2] = sdk.getPlugins(Plugin1ID, Plugin2ID) * * Plugin1?.run() * Plugin2?.test() * ``` */ getPlugins: <IDs extends Plugin["id"][] = (string | import('./Plugin').PluginID<unknown>)[]>(...ids: IDs) => { [K in keyof IDs]: InferPlugin<IDs[K]> | undefined; }; /** * 集中清理插件缓存,不会影响插件正常运行 */ cleanup: () => void; /** * 安装插件列表 */ private _installPlugins; /** * 卸载插件列表 */ private _uninstallPlugins; /** * 手动初始化 AppSDK * @param app Vue 应用实例 */ init: (app: App) => void; /** * 手动销毁 AppSDK,会依次执行清理缓存、卸载插件、移除全部事件监听并释放内部数据。 */ destroy: () => void; /** * 安装到 Vue 实例 * @param app Vue 应用实例 */ install: (app: App) => void; } declare module 'vue' { interface ComponentCustomProperties { /** * AppSDK 实例 */ $appSDK: AppSDK; } }