@dlient/api-bridge 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +35 -0
- package/dist/index.mjs +11 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +27 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dlient/api-bridge - 渲染进程「插件实例 API」共享桥。
|
|
3
|
+
*
|
|
4
|
+
* 这是插件与宿主之间唯一需要共享实例的模块:PluginApiContext + useDientApi。
|
|
5
|
+
* 必须作为 MF shared(singleton)由宿主提供,否则插件的 useDientApi 读到的是
|
|
6
|
+
* 插件自己打包的一份 Context,读不到宿主 PluginView 注入的值。
|
|
7
|
+
*
|
|
8
|
+
* 注意:本包必须保持极简 —— 只依赖 react 的命名导出(createContext/useContext),
|
|
9
|
+
* 不允许引入 JSX / 其它 CJS 依赖,否则会被 vite-plugin-federation 的 shared 预构建
|
|
10
|
+
* 打包出引用未定义 `require$$0` 的产物(见 package-ui.md 加载注意事项)。
|
|
11
|
+
*/
|
|
12
|
+
/** request / listen 的选项:plugin 缺省 = 自调用本插件 worker;data 为大块数据(transfer 零拷贝) */
|
|
13
|
+
export interface RequestOptions {
|
|
14
|
+
plugin?: string;
|
|
15
|
+
data?: ArrayBuffer;
|
|
16
|
+
}
|
|
17
|
+
/** listen 返回的取消控制器(abort() 通知 worker 发 cancel) */
|
|
18
|
+
export interface ListenController {
|
|
19
|
+
abort(): void;
|
|
20
|
+
readonly signal: {
|
|
21
|
+
readonly aborted: boolean;
|
|
22
|
+
addEventListener(cb: () => void): () => void;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface PluginApi {
|
|
26
|
+
/** 单响应请求:plugin 缺省连自己的 worker,传 { plugin } 跨插件直连目标 worker */
|
|
27
|
+
request: <T = unknown>(method: string, args?: unknown[], opts?: RequestOptions) => Promise<T>;
|
|
28
|
+
/** 流式请求:listener 逐块回调,返回 { abort, signal } 取消控制器 */
|
|
29
|
+
listen: (method: string, args: unknown[], listener: (chunk: unknown, data?: ArrayBuffer) => void, opts?: RequestOptions) => ListenController;
|
|
30
|
+
/** 推送订阅(worker push → 渲染层) */
|
|
31
|
+
onEvent: (name: string, cb: (data: unknown) => void) => () => void;
|
|
32
|
+
}
|
|
33
|
+
export declare const PluginApiContext: import("react").Context<PluginApi | null>;
|
|
34
|
+
/** 插件组件访问「绑定本视图」的实例 API(连自己的 worker) */
|
|
35
|
+
export declare function useDientApi(): PluginApi;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
const PluginApiContext = createContext(null);
|
|
4
|
+
function useDientApi() {
|
|
5
|
+
const api = useContext(PluginApiContext);
|
|
6
|
+
if (!api) throw new Error("useDientApi must be used inside <PluginView>");
|
|
7
|
+
return api;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { PluginApiContext, useDientApi };
|
|
11
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["/**\n * @dlient/api-bridge - 渲染进程「插件实例 API」共享桥。\n *\n * 这是插件与宿主之间唯一需要共享实例的模块:PluginApiContext + useDientApi。\n * 必须作为 MF shared(singleton)由宿主提供,否则插件的 useDientApi 读到的是\n * 插件自己打包的一份 Context,读不到宿主 PluginView 注入的值。\n *\n * 注意:本包必须保持极简 —— 只依赖 react 的命名导出(createContext/useContext),\n * 不允许引入 JSX / 其它 CJS 依赖,否则会被 vite-plugin-federation 的 shared 预构建\n * 打包出引用未定义 `require$$0` 的产物(见 package-ui.md 加载注意事项)。\n */\n\nimport { createContext, useContext } from 'react'\n\n/** request / listen 的选项:plugin 缺省 = 自调用本插件 worker;data 为大块数据(transfer 零拷贝) */\nexport interface RequestOptions {\n plugin?: string\n data?: ArrayBuffer\n}\n\n/** listen 返回的取消控制器(abort() 通知 worker 发 cancel) */\nexport interface ListenController {\n abort(): void\n readonly signal: {\n readonly aborted: boolean\n addEventListener(cb: () => void): () => void\n }\n}\n\nexport interface PluginApi {\n /** 单响应请求:plugin 缺省连自己的 worker,传 { plugin } 跨插件直连目标 worker */\n request: <T = unknown>(method: string, args?: unknown[], opts?: RequestOptions) => Promise<T>\n /** 流式请求:listener 逐块回调,返回 { abort, signal } 取消控制器 */\n listen: (\n method: string,\n args: unknown[],\n listener: (chunk: unknown, data?: ArrayBuffer) => void,\n opts?: RequestOptions,\n ) => ListenController\n /** 推送订阅(worker push → 渲染层) */\n onEvent: (name: string, cb: (data: unknown) => void) => () => void\n}\n\nexport const PluginApiContext = createContext<PluginApi | null>(null)\n\n/** 插件组件访问「绑定本视图」的实例 API(连自己的 worker) */\nexport function useDientApi(): PluginApi {\n const api = useContext(PluginApiContext)\n if (!api) throw new Error('useDientApi must be used inside <PluginView>')\n return api\n}\n"],"names":[],"mappings":";;AA2CO,MAAM,gBAAA,GAAmB,cAAgC,IAAI;AAG7D,SAAS,WAAA,GAAyB;AACvC,EAAA,MAAM,GAAA,GAAM,WAAW,gBAAgB,CAAA;AACvC,EAAA,IAAI,CAAC,GAAA,EAAK,MAAM,IAAI,MAAM,8CAA8C,CAAA;AACxE,EAAA,OAAO,GAAA;AACT;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dlient/api-bridge",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.mjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "vite build && tsc -p tsconfig.json"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"react": "^18.2.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/react": "^18.2.0",
|
|
24
|
+
"typescript": "^5.4.0",
|
|
25
|
+
"vite": "^5.1.6"
|
|
26
|
+
}
|
|
27
|
+
}
|