@dino-dev/bridge-core-ts 0.0.1 → 0.0.2
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/README.md +45 -3
- package/dist/src/content-protocol.d.ts +50 -0
- package/dist/src/content-protocol.d.ts.map +1 -0
- package/dist/src/content-protocol.js +46 -0
- package/dist/src/content-protocol.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/package.json +1 -1
- package/src/content-protocol.ts +94 -0
- package/src/index.ts +17 -0
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ H5 端与 Flutter 容器通信的核心 TypeScript SDK(零框架依赖)。
|
|
|
9
9
|
|------|------|
|
|
10
10
|
| `src/mrp-bridge.ts` | 核心 Bridge SDK(消息通信 + 模块代理) |
|
|
11
11
|
| `src/system-module.ts` | 内置系统模块类型定义 + `getSystemModule()` 工厂 |
|
|
12
|
+
| `src/content-protocol.ts` | 标准渲染、交互与错误协议 |
|
|
12
13
|
| `src/index.ts` | 包入口,统一 re-export 所有 API |
|
|
13
14
|
| `generated/*_module.ts` | 各模块的 TypeScript 类型定义(由 build_runner 自动生成,纯类型) |
|
|
14
15
|
| `generated/index.ts` | 自动生成的模块类型 re-export + 工厂函数 |
|
|
@@ -128,9 +129,50 @@ const { modules } = await system.getAvailableModules();
|
|
|
128
129
|
const info = await system.getContainerInfo();
|
|
129
130
|
```
|
|
130
131
|
|
|
131
|
-
##
|
|
132
|
+
## 标准内容协议
|
|
132
133
|
|
|
133
|
-
|
|
134
|
+
SDK 为内容型 H5 固定定义以下协议域:
|
|
135
|
+
|
|
136
|
+
| 方向 | type | action | payload |
|
|
137
|
+
|---|---|---|---|
|
|
138
|
+
| Flutter → H5 | `render` | `set` | `{ data, options? }` |
|
|
139
|
+
| Flutter → H5 | `render` | `update` | `{ data, options? }` |
|
|
140
|
+
| Flutter → H5 | `render` | `clear` | `{}` |
|
|
141
|
+
| H5 → Flutter | `render` | `ready` | 应用能力信息 |
|
|
142
|
+
| H5 → Flutter | `render` | `applied` | 已执行操作信息 |
|
|
143
|
+
| H5 → Flutter | `interaction` | 业务动作 | `{ sourceId, value?, ... }` |
|
|
144
|
+
| H5 → Flutter | `error` | `report` | `{ source, code, message, details? }` |
|
|
145
|
+
|
|
146
|
+
H5 通过 `bindRenderProtocol()` 接收渲染消息,通过 `renderProtocol`、`interaction` 和 `bridgeError` 上报事件:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import {
|
|
150
|
+
bindRenderProtocol,
|
|
151
|
+
bridgeError,
|
|
152
|
+
interaction,
|
|
153
|
+
renderProtocol,
|
|
154
|
+
} from '@h5-container/bridge-core-ts';
|
|
155
|
+
|
|
156
|
+
const unsubscribe = bindRenderProtocol({
|
|
157
|
+
set: ({ data }) => renderCompleteContent(data),
|
|
158
|
+
update: ({ data, options }) => updateContent(data, options),
|
|
159
|
+
clear: () => clearContent(),
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
await renderProtocol.ready({ supportedOrientations: ['portrait'] });
|
|
163
|
+
await interaction.send('submit', { sourceId: 'choice-1', value: ['fast'] });
|
|
164
|
+
await bridgeError.report({
|
|
165
|
+
source: 'render',
|
|
166
|
+
code: 'INVALID_CONTENT',
|
|
167
|
+
message: 'Unsupported content type',
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
`payload.data` 的结构由当前 H5 package 定义和校验,Container SDK 不理解具体业务类型。`packageId` 负责区分当前加载的 H5 应用。
|
|
172
|
+
|
|
173
|
+
## 底层协议
|
|
174
|
+
|
|
175
|
+
所有消息遵循统一信封。除上述标准内容协议外,底层 `bridge.send()` 和 `bridge.on()` 仍允许业务扩展自由字符串:
|
|
134
176
|
|
|
135
177
|
```json
|
|
136
178
|
{
|
|
@@ -142,4 +184,4 @@ const info = await system.getContainerInfo();
|
|
|
142
184
|
}
|
|
143
185
|
```
|
|
144
186
|
|
|
145
|
-
>
|
|
187
|
+
> 新的内容型 H5 应优先使用标准内容协议;自由字符串只用于无法归入标准域的扩展能力。
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type BridgeMessage, type Unsubscribe } from './mrp-bridge';
|
|
2
|
+
export declare const bridgeMessageTypes: {
|
|
3
|
+
readonly render: "render";
|
|
4
|
+
readonly interaction: "interaction";
|
|
5
|
+
readonly error: "error";
|
|
6
|
+
};
|
|
7
|
+
export declare const renderActions: {
|
|
8
|
+
readonly set: "set";
|
|
9
|
+
readonly update: "update";
|
|
10
|
+
readonly clear: "clear";
|
|
11
|
+
readonly ready: "ready";
|
|
12
|
+
readonly applied: "applied";
|
|
13
|
+
};
|
|
14
|
+
export interface RenderPayload<T = unknown> {
|
|
15
|
+
data: T;
|
|
16
|
+
options?: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
export interface RenderHandlers<T = unknown> {
|
|
19
|
+
set(payload: RenderPayload<T>, message: BridgeMessage<RenderPayload<T>>): void;
|
|
20
|
+
update?(payload: RenderPayload<T>, message: BridgeMessage<RenderPayload<T>>): void;
|
|
21
|
+
clear(message: BridgeMessage<Record<string, unknown>>): void;
|
|
22
|
+
}
|
|
23
|
+
export interface InteractionPayload<T = unknown> {
|
|
24
|
+
sourceId: string;
|
|
25
|
+
value?: T;
|
|
26
|
+
[key: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
export interface BridgeErrorPayload {
|
|
29
|
+
source: string;
|
|
30
|
+
code: string;
|
|
31
|
+
message: string;
|
|
32
|
+
details?: unknown;
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
}
|
|
35
|
+
export interface ProtocolTransport {
|
|
36
|
+
on(type: string, action: string | null, handler: (message: BridgeMessage<any>) => void): Unsubscribe;
|
|
37
|
+
send(type: string, action: string, payload: Record<string, unknown>): Promise<unknown>;
|
|
38
|
+
}
|
|
39
|
+
export declare function bindRenderProtocol<T>(handlers: RenderHandlers<T>, transport?: ProtocolTransport): Unsubscribe;
|
|
40
|
+
export declare const renderProtocol: {
|
|
41
|
+
readonly ready: (payload?: Record<string, unknown>) => Promise<unknown>;
|
|
42
|
+
readonly applied: (payload?: Record<string, unknown>) => Promise<unknown>;
|
|
43
|
+
};
|
|
44
|
+
export declare const interaction: {
|
|
45
|
+
readonly send: <T>(action: string, payload: InteractionPayload<T>) => Promise<unknown>;
|
|
46
|
+
};
|
|
47
|
+
export declare const bridgeError: {
|
|
48
|
+
readonly report: (payload: BridgeErrorPayload) => Promise<unknown>;
|
|
49
|
+
};
|
|
50
|
+
//# sourceMappingURL=content-protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content-protocol.d.ts","sourceRoot":"","sources":["../../src/content-protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,aAAa,EAAE,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAE5E,eAAO,MAAM,kBAAkB;;;;CAIrB,CAAC;AAEX,eAAO,MAAM,aAAa;;;;;;CAMhB,CAAC;AAEX,MAAM,WAAW,aAAa,CAAC,CAAC,GAAG,OAAO;IACxC,IAAI,EAAE,CAAC,CAAC;IACR,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAED,MAAM,WAAW,cAAc,CAAC,CAAC,GAAG,OAAO;IACzC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IAC/E,MAAM,CAAC,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IACnF,KAAK,CAAC,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,IAAI,CA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7C,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { bridge } from './mrp-bridge';
|
|
2
|
+
export const bridgeMessageTypes = {
|
|
3
|
+
render: 'render',
|
|
4
|
+
interaction: 'interaction',
|
|
5
|
+
error: 'error',
|
|
6
|
+
};
|
|
7
|
+
export const renderActions = {
|
|
8
|
+
set: 'set',
|
|
9
|
+
update: 'update',
|
|
10
|
+
clear: 'clear',
|
|
11
|
+
ready: 'ready',
|
|
12
|
+
applied: 'applied',
|
|
13
|
+
};
|
|
14
|
+
export function bindRenderProtocol(handlers, transport = bridge) {
|
|
15
|
+
const subscriptions = [
|
|
16
|
+
transport.on(bridgeMessageTypes.render, renderActions.set, (message) => {
|
|
17
|
+
handlers.set(message.payload, message);
|
|
18
|
+
}),
|
|
19
|
+
transport.on(bridgeMessageTypes.render, renderActions.update, (message) => {
|
|
20
|
+
handlers.update?.(message.payload, message);
|
|
21
|
+
}),
|
|
22
|
+
transport.on(bridgeMessageTypes.render, renderActions.clear, (message) => {
|
|
23
|
+
handlers.clear(message);
|
|
24
|
+
}),
|
|
25
|
+
];
|
|
26
|
+
return () => subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
27
|
+
}
|
|
28
|
+
export const renderProtocol = {
|
|
29
|
+
ready(payload = {}) {
|
|
30
|
+
return bridge.send(bridgeMessageTypes.render, renderActions.ready, payload);
|
|
31
|
+
},
|
|
32
|
+
applied(payload = {}) {
|
|
33
|
+
return bridge.send(bridgeMessageTypes.render, renderActions.applied, payload);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
export const interaction = {
|
|
37
|
+
send(action, payload) {
|
|
38
|
+
return bridge.send(bridgeMessageTypes.interaction, action, payload);
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
export const bridgeError = {
|
|
42
|
+
report(payload) {
|
|
43
|
+
return bridge.send(bridgeMessageTypes.error, 'report', payload);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL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C;AACrE,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,KAAK,CAAC,UAAmC,EAAE;QACzC,OAAO,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,MAAM,EAAE,aAAa,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IAC9E,CAAC;IAED,OAAO,CAAC,UAAmC,EAAE;QAC3C,OAAO,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,MAAM,EAAE,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAChF,CAAC;CACO,CAAC;AAEX,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,IAAI,CAAI,MAAc,EAAE,OAA8B;QACpD,OAAO,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IACtE,CAAC;CACO,CAAC;AAEX,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,MAAM,CAAC,OAA2B;QAChC,OAAO,MAAM,CAAC,IAAI,CAAC,kBAAkB,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;IAClE,CAAC;CACO,CAAC"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -14,4 +14,6 @@ export type { SystemModule, ModuleAllowedResult, AvailableModulesResult, Contain
|
|
|
14
14
|
export * from '../generated/index';
|
|
15
15
|
export { lifecycle } from './lifecycle';
|
|
16
16
|
export type { RouteChangedPayload, AppMountedPayload } from './lifecycle';
|
|
17
|
+
export { bindRenderProtocol, bridgeError, bridgeMessageTypes, interaction, renderActions, renderProtocol, } from './content-protocol';
|
|
18
|
+
export type { BridgeErrorPayload, InteractionPayload, ProtocolTransport, RenderHandlers, RenderPayload, } from './content-protocol';
|
|
17
19
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACjD,YAAY,EACV,aAAa,EACb,cAAc,EACd,WAAW,GACZ,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,YAAY,EACV,YAAY,EACZ,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AAGzB,cAAc,oBAAoB,CAAC;AAGnC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,YAAY,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACjD,YAAY,EACV,aAAa,EACb,cAAc,EACd,WAAW,GACZ,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,YAAY,EACV,YAAY,EACZ,mBAAmB,EACnB,sBAAsB,EACtB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC;AAGzB,cAAc,oBAAoB,CAAC;AAGnC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,YAAY,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAG1E,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,aAAa,EACb,cAAc,GACf,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACV,kBAAkB,EAClB,kBAAkB,EAClB,iBAAiB,EACjB,cAAc,EACd,aAAa,GACd,MAAM,oBAAoB,CAAC"}
|
package/dist/src/index.js
CHANGED
|
@@ -15,4 +15,6 @@ export { getSystemModule } from './system-module';
|
|
|
15
15
|
export * from '../generated/index';
|
|
16
16
|
// 生命周期上报工具
|
|
17
17
|
export { lifecycle } from './lifecycle';
|
|
18
|
+
// 标准内容渲染、交互与错误协议
|
|
19
|
+
export { bindRenderProtocol, bridgeError, bridgeMessageTypes, interaction, renderActions, renderProtocol, } from './content-protocol';
|
|
18
20
|
//# sourceMappingURL=index.js.map
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,YAAY;AACZ,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAOjD,0BAA0B;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAQlD,yBAAyB;AACzB,cAAc,oBAAoB,CAAC;AAEnC,WAAW;AACX,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,YAAY;AACZ,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAOjD,0BAA0B;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAQlD,yBAAyB;AACzB,cAAc,oBAAoB,CAAC;AAEnC,WAAW;AACX,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAGxC,iBAAiB;AACjB,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,aAAa,EACb,cAAc,GACf,MAAM,oBAAoB,CAAC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { bridge, type BridgeMessage, type Unsubscribe } from './mrp-bridge';
|
|
2
|
+
|
|
3
|
+
export const bridgeMessageTypes = {
|
|
4
|
+
render: 'render',
|
|
5
|
+
interaction: 'interaction',
|
|
6
|
+
error: 'error',
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
export const renderActions = {
|
|
10
|
+
set: 'set',
|
|
11
|
+
update: 'update',
|
|
12
|
+
clear: 'clear',
|
|
13
|
+
ready: 'ready',
|
|
14
|
+
applied: 'applied',
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
export interface RenderPayload<T = unknown> {
|
|
18
|
+
data: T;
|
|
19
|
+
options?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RenderHandlers<T = unknown> {
|
|
23
|
+
set(payload: RenderPayload<T>, message: BridgeMessage<RenderPayload<T>>): void;
|
|
24
|
+
update?(payload: RenderPayload<T>, message: BridgeMessage<RenderPayload<T>>): void;
|
|
25
|
+
clear(message: BridgeMessage<Record<string, unknown>>): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface InteractionPayload<T = unknown> {
|
|
29
|
+
sourceId: string;
|
|
30
|
+
value?: T;
|
|
31
|
+
[key: string]: unknown;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface BridgeErrorPayload {
|
|
35
|
+
source: string;
|
|
36
|
+
code: string;
|
|
37
|
+
message: string;
|
|
38
|
+
details?: unknown;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ProtocolTransport {
|
|
43
|
+
on(
|
|
44
|
+
type: string,
|
|
45
|
+
action: string | null,
|
|
46
|
+
handler: (message: BridgeMessage<any>) => void,
|
|
47
|
+
): Unsubscribe;
|
|
48
|
+
send(
|
|
49
|
+
type: string,
|
|
50
|
+
action: string,
|
|
51
|
+
payload: Record<string, unknown>,
|
|
52
|
+
): Promise<unknown>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function bindRenderProtocol<T>(
|
|
56
|
+
handlers: RenderHandlers<T>,
|
|
57
|
+
transport: ProtocolTransport = bridge,
|
|
58
|
+
): Unsubscribe {
|
|
59
|
+
const subscriptions = [
|
|
60
|
+
transport.on(bridgeMessageTypes.render, renderActions.set, (message: BridgeMessage<RenderPayload<T>>) => {
|
|
61
|
+
handlers.set(message.payload, message);
|
|
62
|
+
}),
|
|
63
|
+
transport.on(bridgeMessageTypes.render, renderActions.update, (message: BridgeMessage<RenderPayload<T>>) => {
|
|
64
|
+
handlers.update?.(message.payload, message);
|
|
65
|
+
}),
|
|
66
|
+
transport.on(bridgeMessageTypes.render, renderActions.clear, (message) => {
|
|
67
|
+
handlers.clear(message);
|
|
68
|
+
}),
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
return () => subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const renderProtocol = {
|
|
75
|
+
ready(payload: Record<string, unknown> = {}): Promise<unknown> {
|
|
76
|
+
return bridge.send(bridgeMessageTypes.render, renderActions.ready, payload);
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
applied(payload: Record<string, unknown> = {}): Promise<unknown> {
|
|
80
|
+
return bridge.send(bridgeMessageTypes.render, renderActions.applied, payload);
|
|
81
|
+
},
|
|
82
|
+
} as const;
|
|
83
|
+
|
|
84
|
+
export const interaction = {
|
|
85
|
+
send<T>(action: string, payload: InteractionPayload<T>): Promise<unknown> {
|
|
86
|
+
return bridge.send(bridgeMessageTypes.interaction, action, payload);
|
|
87
|
+
},
|
|
88
|
+
} as const;
|
|
89
|
+
|
|
90
|
+
export const bridgeError = {
|
|
91
|
+
report(payload: BridgeErrorPayload): Promise<unknown> {
|
|
92
|
+
return bridge.send(bridgeMessageTypes.error, 'report', payload);
|
|
93
|
+
},
|
|
94
|
+
} as const;
|
package/src/index.ts
CHANGED
|
@@ -31,3 +31,20 @@ export * from '../generated/index';
|
|
|
31
31
|
// 生命周期上报工具
|
|
32
32
|
export { lifecycle } from './lifecycle';
|
|
33
33
|
export type { RouteChangedPayload, AppMountedPayload } from './lifecycle';
|
|
34
|
+
|
|
35
|
+
// 标准内容渲染、交互与错误协议
|
|
36
|
+
export {
|
|
37
|
+
bindRenderProtocol,
|
|
38
|
+
bridgeError,
|
|
39
|
+
bridgeMessageTypes,
|
|
40
|
+
interaction,
|
|
41
|
+
renderActions,
|
|
42
|
+
renderProtocol,
|
|
43
|
+
} from './content-protocol';
|
|
44
|
+
export type {
|
|
45
|
+
BridgeErrorPayload,
|
|
46
|
+
InteractionPayload,
|
|
47
|
+
ProtocolTransport,
|
|
48
|
+
RenderHandlers,
|
|
49
|
+
RenderPayload,
|
|
50
|
+
} from './content-protocol';
|