@yagolive/event-kit 1.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/CHANGELOG.md +70 -0
- package/README.md +651 -0
- package/build/js/action_bridge.umd.js +744 -0
- package/build/js/action_creator.umd.js +479 -0
- package/package.json +84 -0
- package/src/adapters/MockAdapter.js +44 -0
- package/src/adapters/RNWebViewAdapter.js +76 -0
- package/src/adapters/index.d.ts +44 -0
- package/src/adapters/index.js +2 -0
- package/src/bridge/ActionBridge.js +164 -0
- package/src/bridge/index.d.ts +59 -0
- package/src/bridge/index.js +1 -0
- package/src/core/ActionCreator.js +121 -0
- package/src/core/index.d.ts +182 -0
- package/src/core/index.js +8 -0
- package/src/core/types/Action.js +28 -0
- package/src/core/types/EnterRoom.js +92 -0
- package/src/core/types/Jump.js +27 -0
- package/src/core/types/LiveDetect.js +16 -0
- package/src/core/types/Navigate.js +31 -0
- package/src/core/types/Onelink.js +28 -0
- package/src/core/types/Replace.js +9 -0
- package/src/index.d.ts +4 -0
- package/src/index.js +10 -0
- package/src/integrations/react/index.d.ts +13 -0
- package/src/integrations/react/index.js +41 -0
- package/src/integrations/vue/index.d.ts +13 -0
- package/src/integrations/vue/index.js +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
# @yagolive/event-kit
|
|
2
|
+
|
|
3
|
+
YAGO App Action 创建与通信工具库,支持浏览器、React、Vue 等多种环境。
|
|
4
|
+
|
|
5
|
+
## 架构
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
┌─────────────────────────────────────────────────────┐
|
|
9
|
+
│ 应用层 (Frameworks) │
|
|
10
|
+
├───────────────────┬───────────────────┬─────────────┤
|
|
11
|
+
│ React │ Vue 3 │ 原生 JS │
|
|
12
|
+
├───────────────────┴───────────────────┴─────────────┤
|
|
13
|
+
│ 桥接层 (Bridge) │
|
|
14
|
+
│ ActionBridge (注入适配器) │
|
|
15
|
+
├─────────────────────────────────────────────────────┤
|
|
16
|
+
│ 适配器层 (Adapters) │
|
|
17
|
+
│ RNWebViewAdapter │ MockAdapter │
|
|
18
|
+
├─────────────────────────────────────────────────────┤
|
|
19
|
+
│ 核心层 (Core) │
|
|
20
|
+
│ ActionCreator │ ActionType │ ActionData │
|
|
21
|
+
└─────────────────────────────────────────────────────┘
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 安装
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @yagolive/event-kit
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## 快速开始
|
|
31
|
+
|
|
32
|
+
### 浏览器(Script 标签)
|
|
33
|
+
|
|
34
|
+
```html
|
|
35
|
+
<!-- 引入完整功能包 -->
|
|
36
|
+
<script src="https://unpkg.com/@yagolive/event-kit/build/js/action_bridge.umd.js"></script>
|
|
37
|
+
<script>
|
|
38
|
+
var { ActionBridge } = window.__action_bridge__;
|
|
39
|
+
|
|
40
|
+
// 自动检测环境并创建桥接实例
|
|
41
|
+
var bridge = ActionBridge.createDefault();
|
|
42
|
+
|
|
43
|
+
// 发送导航 Action
|
|
44
|
+
bridge.navigate('profile', { userId: 123 })
|
|
45
|
+
.then(function(result) { console.log('导航成功', result); })
|
|
46
|
+
.catch(function(error) { console.error('导航失败', error); });
|
|
47
|
+
</script>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```html
|
|
51
|
+
<!-- 仅引入核心包(不需要通信功能时) -->
|
|
52
|
+
<script src="https://unpkg.com/@yagolive/event-kit/build/js/action_creator.umd.js"></script>
|
|
53
|
+
<script>
|
|
54
|
+
var { ActionCreator } = window.__action_creator__;
|
|
55
|
+
|
|
56
|
+
var action = ActionCreator.createNavigateAction('home');
|
|
57
|
+
console.log(action.toJson());
|
|
58
|
+
// {"type":"navigate","data":{"screen":"home"}}
|
|
59
|
+
</script>
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
也可使用 [jsdelivr](https://www.jsdelivr.com/package/npm/@yagolive/event-kit):
|
|
63
|
+
|
|
64
|
+
```html
|
|
65
|
+
<script src="https://cdn.jsdelivr.net/npm/@yagolive/event-kit/build/js/action_bridge.umd.js"></script>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### React
|
|
69
|
+
|
|
70
|
+
```jsx
|
|
71
|
+
import { ActionBridgeProvider, useActionBridge } from '@yagolive/event-kit/react';
|
|
72
|
+
import { RNWebViewAdapter } from '@yagolive/event-kit/adapters';
|
|
73
|
+
|
|
74
|
+
// 1. 在根组件配置 Provider
|
|
75
|
+
function App() {
|
|
76
|
+
return (
|
|
77
|
+
<ActionBridgeProvider adapter={new RNWebViewAdapter()}>
|
|
78
|
+
<MyPage />
|
|
79
|
+
</ActionBridgeProvider>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 2. 在子组件中使用 Hook
|
|
84
|
+
function MyPage() {
|
|
85
|
+
const bridge = useActionBridge();
|
|
86
|
+
|
|
87
|
+
const goToProfile = async () => {
|
|
88
|
+
try {
|
|
89
|
+
const result = await bridge.navigate('profile', { userId: 123 });
|
|
90
|
+
console.log('导航成功', result);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error('导航失败', error);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return <button onClick={goToProfile}>查看个人资料</button>;
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Vue 3
|
|
101
|
+
|
|
102
|
+
```javascript
|
|
103
|
+
import { createActionBridgePlugin, useActionBridge } from '@yagolive/event-kit/vue';
|
|
104
|
+
import { RNWebViewAdapter } from '@yagolive/event-kit/adapters';
|
|
105
|
+
|
|
106
|
+
// 1. 安装插件
|
|
107
|
+
const adapter = new RNWebViewAdapter();
|
|
108
|
+
app.use(createActionBridgePlugin(adapter));
|
|
109
|
+
|
|
110
|
+
// 2. 在组件中使用 Composition API
|
|
111
|
+
export default {
|
|
112
|
+
setup() {
|
|
113
|
+
const bridge = useActionBridge();
|
|
114
|
+
|
|
115
|
+
const goToProfile = async () => {
|
|
116
|
+
try {
|
|
117
|
+
const result = await bridge.navigate('profile', { userId: 123 });
|
|
118
|
+
console.log('导航成功', result);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
console.error('导航失败', error);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return { goToProfile };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 3. 也可通过 $actionBridge 访问(Options API)
|
|
129
|
+
export default {
|
|
130
|
+
methods: {
|
|
131
|
+
async goToProfile() {
|
|
132
|
+
const result = await this.$actionBridge.navigate('profile', { userId: 123 });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### ESM / CommonJS
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
// ESM
|
|
142
|
+
import { ActionBridge, RNWebViewAdapter } from '@yagolive/event-kit';
|
|
143
|
+
const adapter = new RNWebViewAdapter();
|
|
144
|
+
const bridge = new ActionBridge(adapter);
|
|
145
|
+
|
|
146
|
+
// CommonJS
|
|
147
|
+
const { ActionBridge, RNWebViewAdapter } = require('@yagolive/event-kit');
|
|
148
|
+
const adapter = new RNWebViewAdapter();
|
|
149
|
+
const bridge = new ActionBridge(adapter);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## API 参考
|
|
155
|
+
|
|
156
|
+
### ActionCreator
|
|
157
|
+
|
|
158
|
+
纯逻辑工厂,无环境依赖。用于创建各种 Action 对象。
|
|
159
|
+
|
|
160
|
+
```javascript
|
|
161
|
+
import { ActionCreator } from '@yagolive/event-kit';
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### 属性
|
|
165
|
+
|
|
166
|
+
| 属性 | 类型 | 说明 |
|
|
167
|
+
|------|------|------|
|
|
168
|
+
| `ActionData` | Object | 包含所有数据类型类:Jump, Navigate, Replace, EnterRoom, LiveDetect, Onelink |
|
|
169
|
+
| `ActionType` | Object | Action 类型枚举:JUMP, NAVIGATE, REPLACE, ENTER_ROOM, LIVE_DETECT, ONELINK |
|
|
170
|
+
|
|
171
|
+
#### 工厂方法
|
|
172
|
+
|
|
173
|
+
| 方法 | 参数 | 返回值 | 说明 |
|
|
174
|
+
|------|------|--------|------|
|
|
175
|
+
| `buildJump(path?)` | path?: string | Jump | 构建 Jump 对象(链式配置) |
|
|
176
|
+
| `createJumpAction(path)` | path: string | Action | 创建跳转 Action |
|
|
177
|
+
| `createJumpActionWithData(jump)` | jump: Jump | Action | 从 Jump 对象创建 Action |
|
|
178
|
+
| `buildNavigate(screen?, params?)` | screen?: string, params?: Object | Navigate | 构建 Navigate 对象(链式配置) |
|
|
179
|
+
| `createNavigateAction(screen)` | screen: string | Action | 创建导航 Action(无参数) |
|
|
180
|
+
| `createNavigateActionWithParams(screen, params)` | screen: string, params: Object | Action | 创建导航 Action(带参数) |
|
|
181
|
+
| `createNavigateActionWithData(navigate)` | navigate: Navigate | Action | 从 Navigate 对象创建 Action |
|
|
182
|
+
| `buildReplace(screen?, params?)` | screen?: string, params?: Object | Replace | 构建 Replace 对象(链式配置) |
|
|
183
|
+
| `createReplaceAction(screen)` | screen: string | Action | 创建替换导航 Action |
|
|
184
|
+
| `createReplaceActionWithParams(screen, params)` | screen: string, params: Object | Action | 创建替换导航 Action(带参数) |
|
|
185
|
+
| `createReplaceActionWithData(replace)` | replace: Replace | Action | 从 Replace 对象创建 Action |
|
|
186
|
+
| `buildEnterRoom()` | - | EnterRoom | 构建 EnterRoom 对象(链式配置) |
|
|
187
|
+
| `createEnterRoomAction(id)` | id: number | Action | 创建进房 Action |
|
|
188
|
+
| `createEnterRoomActionWithData(enterRoom)` | enterRoom: EnterRoom | Action | 从 EnterRoom 对象创建 Action |
|
|
189
|
+
| `buildLiveDetect()` | - | LiveDetect | 构建 LiveDetect 对象(链式配置) |
|
|
190
|
+
| `createLiveDetectAction()` | - | Action | 创建活体检测 Action |
|
|
191
|
+
| `createLiveDetectActionWithData(liveDetect)` | liveDetect: LiveDetect | Action | 从 LiveDetect 对象创建 Action |
|
|
192
|
+
| `buildOnelink(url?)` | url?: string | Onelink | 构建 Onelink 对象(链式配置) |
|
|
193
|
+
| `createOnelinkAction(url)` | url: string | Action | 创建 Onelink Action |
|
|
194
|
+
| `createOnelinkActionWithData(onelink)` | onelink: Onelink | Action | 从 Onelink 对象创建 Action |
|
|
195
|
+
|
|
196
|
+
#### 使用示例
|
|
197
|
+
|
|
198
|
+
```javascript
|
|
199
|
+
// 简单导航
|
|
200
|
+
const action = ActionCreator.createNavigateAction('home');
|
|
201
|
+
// {"type":"navigate","data":{"screen":"home"}}
|
|
202
|
+
|
|
203
|
+
// 带参数导航
|
|
204
|
+
const action = ActionCreator.createNavigateActionWithParams('profile', { userId: 123 });
|
|
205
|
+
// {"type":"navigate","data":{"screen":"profile","params":{"userId":123}}}
|
|
206
|
+
|
|
207
|
+
// 进房(带回调配置)
|
|
208
|
+
const enterRoom = ActionCreator.buildEnterRoom()
|
|
209
|
+
.setId(1001)
|
|
210
|
+
.setText('我的房间')
|
|
211
|
+
.addOpenGiftPopupCallback(
|
|
212
|
+
new ActionCreator.ActionData.EnterRoom.OpenGiftPopupCallback().setId(1)
|
|
213
|
+
);
|
|
214
|
+
const action = ActionCreator.createEnterRoomActionWithData(enterRoom);
|
|
215
|
+
// {"type":"enter_room","data":{"text":"我的房间","id":1001,"callbacks":{"open_gift_popup":{"id":1}}}}
|
|
216
|
+
|
|
217
|
+
// Onelink
|
|
218
|
+
const action = ActionCreator.createOnelinkAction('https://yago.app/room/123');
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### ActionType
|
|
222
|
+
|
|
223
|
+
Action 类型枚举。
|
|
224
|
+
|
|
225
|
+
```javascript
|
|
226
|
+
import { ActionType } from '@yagolive/event-kit';
|
|
227
|
+
|
|
228
|
+
ActionType.JUMP // 'jump'
|
|
229
|
+
ActionType.NAVIGATE // 'navigate'
|
|
230
|
+
ActionType.REPLACE // 'replace'
|
|
231
|
+
ActionType.ENTER_ROOM // 'enter_room'
|
|
232
|
+
ActionType.LIVE_DETECT // 'live_detect'
|
|
233
|
+
ActionType.ONELINK // 'onelink'
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### ActionBridge
|
|
237
|
+
|
|
238
|
+
统一桥接接口,通过注入适配器实现跨环境通信。
|
|
239
|
+
|
|
240
|
+
```javascript
|
|
241
|
+
import { ActionBridge, RNWebViewAdapter } from '@yagolive/event-kit';
|
|
242
|
+
|
|
243
|
+
const adapter = new RNWebViewAdapter();
|
|
244
|
+
const bridge = new ActionBridge(adapter);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
#### 构造函数
|
|
248
|
+
|
|
249
|
+
```javascript
|
|
250
|
+
new ActionBridge(adapter, options?)
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
| 参数 | 类型 | 必填 | 说明 |
|
|
254
|
+
|------|------|------|------|
|
|
255
|
+
| adapter | Adapter | 是 | 适配器实例,必须实现 `sendAction` 方法 |
|
|
256
|
+
| options | Object | 否 | 配置项(保留) |
|
|
257
|
+
|
|
258
|
+
#### 属性
|
|
259
|
+
|
|
260
|
+
| 属性 | 类型 | 说明 |
|
|
261
|
+
|------|------|------|
|
|
262
|
+
| `ActionCreator` | Object | ActionCreator 工厂引用 |
|
|
263
|
+
| `ActionType` | Object | ActionType 枚举引用 |
|
|
264
|
+
|
|
265
|
+
#### 核心方法
|
|
266
|
+
|
|
267
|
+
| 方法 | 参数 | 返回值 | 说明 |
|
|
268
|
+
|------|------|--------|------|
|
|
269
|
+
| `sendAction(action, callback?)` | action: Action, callback?: Function | Promise | 发送 Action,支持回调和 Promise 双模式 |
|
|
270
|
+
| `sendActionNoReply(action)` | action: Action | void | 发送 Action 不期待回复 |
|
|
271
|
+
| `destroy()` | - | void | 销毁桥接实例,清理适配器资源 |
|
|
272
|
+
|
|
273
|
+
#### 便捷方法
|
|
274
|
+
|
|
275
|
+
每个便捷方法返回 Promise,内部自动创建对应 Action 并发送。
|
|
276
|
+
|
|
277
|
+
| 方法 | 参数 | 返回值 | 说明 |
|
|
278
|
+
|------|------|--------|------|
|
|
279
|
+
| `jump(path)` | path: string | Promise | 跳转 |
|
|
280
|
+
| `buildJump(path?)` | path?: string | Jump | 构建 Jump 对象(链式配置) |
|
|
281
|
+
| `createJumpActionWithData(jump)` | jump: Jump | Action | 从 Jump 对象创建 Action(不自动发送) |
|
|
282
|
+
| `navigate(screen, params?)` | screen: string, params?: Object | Promise | 导航到指定页面 |
|
|
283
|
+
| `buildNavigate(screen?, params?)` | screen?: string, params?: Object | Navigate | 构建 Navigate 对象(链式配置) |
|
|
284
|
+
| `createNavigateActionWithData(navigate)` | navigate: Navigate | Action | 从 Navigate 对象创建 Action(不自动发送) |
|
|
285
|
+
| `replace(screen, params?)` | screen: string, params?: Object | Promise | 替换当前页面 |
|
|
286
|
+
| `buildReplace(screen?, params?)` | screen?: string, params?: Object | Replace | 构建 Replace 对象(链式配置) |
|
|
287
|
+
| `createReplaceActionWithData(replace)` | replace: Replace | Action | 从 Replace 对象创建 Action(不自动发送) |
|
|
288
|
+
| `enterRoom(id)` | id: number | Promise | 进入房间 |
|
|
289
|
+
| `buildEnterRoom()` | - | EnterRoom | 构建 EnterRoom 对象(链式配置) |
|
|
290
|
+
| `createEnterRoomAction(id)` | id: number | Action | 创建进房 Action(不自动发送) |
|
|
291
|
+
| `createEnterRoomActionWithData(enterRoom)` | enterRoom: EnterRoom | Action | 从 EnterRoom 对象创建 Action(不自动发送) |
|
|
292
|
+
| `liveDetect()` | - | Promise | 触发活体检测 |
|
|
293
|
+
| `buildLiveDetect()` | - | LiveDetect | 构建 LiveDetect 对象(链式配置) |
|
|
294
|
+
| `createLiveDetectActionWithData(liveDetect)` | liveDetect: LiveDetect | Action | 从 LiveDetect 对象创建 Action(不自动发送) |
|
|
295
|
+
| `onelink(url)` | url: string | Promise | 打开 Onelink |
|
|
296
|
+
| `buildOnelink(url?)` | url?: string | Onelink | 构建 Onelink 对象(链式配置) |
|
|
297
|
+
| `createOnelinkActionWithData(onelink)` | onelink: Onelink | Action | 从 Onelink 对象创建 Action(不自动发送) |
|
|
298
|
+
|
|
299
|
+
#### 静态方法
|
|
300
|
+
|
|
301
|
+
| 方法 | 参数 | 返回值 | 说明 |
|
|
302
|
+
|------|------|--------|------|
|
|
303
|
+
| `createDefault(options?)` | options?: Object | ActionBridge | 使用 RNWebViewAdapter 创建默认桥接实例 |
|
|
304
|
+
| `createWithMock(options?)` | options?: Object | ActionBridge | 使用 MockAdapter 创建测试用桥接实例 |
|
|
305
|
+
|
|
306
|
+
#### 使用示例
|
|
307
|
+
|
|
308
|
+
```javascript
|
|
309
|
+
// Promise 风格(推荐)
|
|
310
|
+
const result = await bridge.navigate('profile', { userId: 123 });
|
|
311
|
+
|
|
312
|
+
// 回调风格(向后兼容)
|
|
313
|
+
bridge.sendAction(action, function(result, error) {
|
|
314
|
+
if (error) console.error(error);
|
|
315
|
+
else console.log(result);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
// 复杂进房配置
|
|
319
|
+
const enterRoom = bridge.buildEnterRoom();
|
|
320
|
+
enterRoom.setId(1001).setUseReplace(true);
|
|
321
|
+
enterRoom.addOpenGiftPopupCallback(
|
|
322
|
+
new bridge.ActionCreator.ActionData.EnterRoom.OpenGiftPopupCallback().setId(1)
|
|
323
|
+
);
|
|
324
|
+
const action = bridge.createEnterRoomActionWithData(enterRoom);
|
|
325
|
+
const result = await bridge.sendAction(action);
|
|
326
|
+
|
|
327
|
+
// 销毁
|
|
328
|
+
bridge.destroy();
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### 适配器
|
|
332
|
+
|
|
333
|
+
#### RNWebViewAdapter
|
|
334
|
+
|
|
335
|
+
用于 React Native WebView 环境,通过 `window.ReactNativeWebView.postMessage` 与原生端通信。
|
|
336
|
+
|
|
337
|
+
```javascript
|
|
338
|
+
import { RNWebViewAdapter } from '@yagolive/event-kit/adapters';
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**构造函数选项:**
|
|
342
|
+
|
|
343
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
344
|
+
|------|------|--------|------|
|
|
345
|
+
| `timeout` | number | 30000 | 请求超时时间(毫秒) |
|
|
346
|
+
|
|
347
|
+
**示例:**
|
|
348
|
+
|
|
349
|
+
```javascript
|
|
350
|
+
const adapter = new RNWebViewAdapter({ timeout: 15000 });
|
|
351
|
+
const bridge = new ActionBridge(adapter);
|
|
352
|
+
|
|
353
|
+
// 或使用快捷方法
|
|
354
|
+
const bridge = ActionBridge.createDefault({ timeout: 15000 });
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
**工作原理:**
|
|
358
|
+
|
|
359
|
+
1. `init()` 时自动检测 Android/iOS 平台并注册对应的 `message` 事件监听
|
|
360
|
+
2. `sendAction()` 通过 `window.ReactNativeWebView.postMessage()` 发送 JSON 数据
|
|
361
|
+
3. 原生端处理完毕后通过 `postMessage` 回传结果,适配器自动匹配回调/Promise
|
|
362
|
+
4. 超时未收到回复自动 reject Promise
|
|
363
|
+
|
|
364
|
+
#### MockAdapter
|
|
365
|
+
|
|
366
|
+
用于测试和开发环境,无需真实 WebView 即可模拟通信。
|
|
367
|
+
|
|
368
|
+
```javascript
|
|
369
|
+
import { MockAdapter } from '@yagolive/event-kit/adapters';
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
**构造函数选项:**
|
|
373
|
+
|
|
374
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
375
|
+
|------|------|--------|------|
|
|
376
|
+
| `delay` | number | 0 | 模拟响应延迟(毫秒) |
|
|
377
|
+
|
|
378
|
+
**方法:**
|
|
379
|
+
|
|
380
|
+
| 方法 | 参数 | 说明 |
|
|
381
|
+
|------|------|------|
|
|
382
|
+
| `mockResult(actionType, result)` | actionType: string, result: any | Mock 指定 Action 类型的返回值 |
|
|
383
|
+
| `mockError(actionType, error)` | actionType: string, error: any | Mock 指定 Action 类型的错误返回 |
|
|
384
|
+
| `getCalls()` | - | 获取所有调用记录 |
|
|
385
|
+
| `clearCalls()` | - | 清空调用记录 |
|
|
386
|
+
| `clearMocks()` | - | 清空所有 Mock 配置 |
|
|
387
|
+
|
|
388
|
+
**示例:**
|
|
389
|
+
|
|
390
|
+
```javascript
|
|
391
|
+
const mock = new MockAdapter({ delay: 100 });
|
|
392
|
+
const bridge = new ActionBridge(mock);
|
|
393
|
+
|
|
394
|
+
// Mock 返回值
|
|
395
|
+
mock.mockResult('navigate', { success: true, screen: 'profile' });
|
|
396
|
+
|
|
397
|
+
// Mock 错误
|
|
398
|
+
mock.mockError('enter_room', new Error('房间不存在'));
|
|
399
|
+
|
|
400
|
+
// 验证调用
|
|
401
|
+
await bridge.navigate('profile');
|
|
402
|
+
const calls = mock.getCalls();
|
|
403
|
+
console.log(calls[0]);
|
|
404
|
+
// { type: 'navigate', data: { screen: 'profile' }, messageId: 'msg_...', timestamp: 1716364800000 }
|
|
405
|
+
|
|
406
|
+
// 清理
|
|
407
|
+
mock.clearCalls();
|
|
408
|
+
mock.clearMocks();
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
#### 自定义适配器
|
|
412
|
+
|
|
413
|
+
实现以下接口即可创建自定义适配器:
|
|
414
|
+
|
|
415
|
+
```javascript
|
|
416
|
+
class CustomAdapter {
|
|
417
|
+
// 必须实现
|
|
418
|
+
sendAction(action) {
|
|
419
|
+
// action.type - Action 类型
|
|
420
|
+
// action.data - Action 数据对象(有 toMap() 方法)
|
|
421
|
+
// action.messageId - 消息 ID
|
|
422
|
+
// 必须返回 Promise
|
|
423
|
+
return new Promise((resolve, reject) => {
|
|
424
|
+
// 发送 action 到目标环境
|
|
425
|
+
// 收到回复后 resolve(result) 或 reject(error)
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
sendActionNoReply(action) {
|
|
430
|
+
// 发送 action 不期待回复
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// 可选实现
|
|
434
|
+
init() {
|
|
435
|
+
// 初始化逻辑(ActionBridge 构造时自动调用)
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
destroy() {
|
|
439
|
+
// 清理逻辑(bridge.destroy() 时自动调用)
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const bridge = new ActionBridge(new CustomAdapter());
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
---
|
|
447
|
+
|
|
448
|
+
## React 集成
|
|
449
|
+
|
|
450
|
+
```javascript
|
|
451
|
+
import { ActionBridgeProvider, useActionBridge } from '@yagolive/event-kit/react';
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
### ActionBridgeProvider
|
|
455
|
+
|
|
456
|
+
Context Provider 组件,为子组件提供 ActionBridge 实例。
|
|
457
|
+
|
|
458
|
+
**Props:**
|
|
459
|
+
|
|
460
|
+
| Prop | 类型 | 说明 |
|
|
461
|
+
|------|------|------|
|
|
462
|
+
| `adapter` | Adapter | 适配器实例(与 `bridge` 二选一) |
|
|
463
|
+
| `bridge` | ActionBridge | 已创建的桥接实例(与 `adapter` 二选一) |
|
|
464
|
+
| `children` | ReactNode | 子组件 |
|
|
465
|
+
|
|
466
|
+
**行为:**
|
|
467
|
+
- 传入 `adapter` 时,Provider 内部创建 ActionBridge 实例,卸载时自动 destroy
|
|
468
|
+
- 传入 `bridge` 时,Provider 直接使用该实例,卸载时不会 destroy
|
|
469
|
+
- 都不传时,自动使用 `ActionBridge.createDefault()` 创建
|
|
470
|
+
|
|
471
|
+
### useActionBridge
|
|
472
|
+
|
|
473
|
+
Hook,获取当前 ActionBridge 实例。必须在 `ActionBridgeProvider` 内使用,否则抛出错误。
|
|
474
|
+
|
|
475
|
+
**示例:**
|
|
476
|
+
|
|
477
|
+
```jsx
|
|
478
|
+
function ProfileButton() {
|
|
479
|
+
const bridge = useActionBridge();
|
|
480
|
+
|
|
481
|
+
const handleClick = async () => {
|
|
482
|
+
await bridge.navigate('profile', { userId: 123 });
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
return <button onClick={handleClick}>查看资料</button>;
|
|
486
|
+
}
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
---
|
|
490
|
+
|
|
491
|
+
## Vue 3 集成
|
|
492
|
+
|
|
493
|
+
```javascript
|
|
494
|
+
import { createActionBridgePlugin, useActionBridge } from '@yagolive/event-kit/vue';
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
### createActionBridgePlugin
|
|
498
|
+
|
|
499
|
+
创建 Vue 插件,安装后提供 ActionBridge 实例。
|
|
500
|
+
|
|
501
|
+
**参数:**
|
|
502
|
+
|
|
503
|
+
| 参数 | 类型 | 说明 |
|
|
504
|
+
|------|------|------|
|
|
505
|
+
| `adapterOrBridge` | Adapter \| ActionBridge | 传入适配器或已创建的桥接实例 |
|
|
506
|
+
|
|
507
|
+
**安装后提供:**
|
|
508
|
+
- `inject(BRIDGE_KEY)` — 通过依赖注入获取
|
|
509
|
+
- `app.config.globalProperties.$actionBridge` — 全局属性访问
|
|
510
|
+
|
|
511
|
+
### useActionBridge
|
|
512
|
+
|
|
513
|
+
Composition API Hook,获取当前 ActionBridge 实例。必须在安装 `createActionBridgePlugin` 后使用。
|
|
514
|
+
|
|
515
|
+
**示例:**
|
|
516
|
+
|
|
517
|
+
```javascript
|
|
518
|
+
// Composition API
|
|
519
|
+
export default {
|
|
520
|
+
setup() {
|
|
521
|
+
const bridge = useActionBridge();
|
|
522
|
+
|
|
523
|
+
const enterRoom = async (roomId) => {
|
|
524
|
+
await bridge.enterRoom(roomId);
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
return { enterRoom };
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Options API(通过 $actionBridge)
|
|
532
|
+
export default {
|
|
533
|
+
methods: {
|
|
534
|
+
async enterRoom(roomId) {
|
|
535
|
+
await this.$actionBridge.enterRoom(roomId);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
---
|
|
542
|
+
|
|
543
|
+
## 数据类型
|
|
544
|
+
|
|
545
|
+
### Jump
|
|
546
|
+
|
|
547
|
+
跳转 Action 数据。
|
|
548
|
+
|
|
549
|
+
| 方法 | 参数 | 说明 |
|
|
550
|
+
|------|------|------|
|
|
551
|
+
| `constructor(path)` | path: string (必填) | 创建跳转数据 |
|
|
552
|
+
| `setText(text)` | text: string | 设置显示文本,返回 this |
|
|
553
|
+
| `setPath(path)` | path: string (必填) | 设置跳转路径,返回 this |
|
|
554
|
+
|
|
555
|
+
### Navigate
|
|
556
|
+
|
|
557
|
+
导航 Action 数据。
|
|
558
|
+
|
|
559
|
+
| 方法 | 参数 | 说明 |
|
|
560
|
+
|------|------|------|
|
|
561
|
+
| `constructor(screen, params?)` | screen: string (必填), params?: Object | 创建导航数据 |
|
|
562
|
+
| `setText(text)` | text: string | 设置显示文本,返回 this |
|
|
563
|
+
| `setScreen(screen)` | screen: string (必填) | 设置目标页面,返回 this |
|
|
564
|
+
| `setParams(params)` | params: Object | 设置导航参数,返回 this |
|
|
565
|
+
|
|
566
|
+
### Replace
|
|
567
|
+
|
|
568
|
+
替换导航 Action 数据,继承自 Navigate,API 相同。
|
|
569
|
+
|
|
570
|
+
### EnterRoom
|
|
571
|
+
|
|
572
|
+
进房 Action 数据。
|
|
573
|
+
|
|
574
|
+
| 方法 | 参数 | 说明 |
|
|
575
|
+
|------|------|------|
|
|
576
|
+
| `constructor()` | - | 创建进房数据 |
|
|
577
|
+
| `setText(text)` | text: string | 设置显示文本,返回 this |
|
|
578
|
+
| `setId(id)` | id: number | 设置房间 ID,返回 this |
|
|
579
|
+
| `setMatch(match)` | match: string | 设置匹配信息,返回 this |
|
|
580
|
+
| `setUseReplace(useReplace)` | useReplace: boolean | 设置是否替换模式,返回 this |
|
|
581
|
+
| `addOpenGiftPopupCallback(cb)` | cb: OpenGiftPopupCallback | 添加打开礼物弹窗回调,返回 this |
|
|
582
|
+
| `addOpenGameCallback(cb)` | cb: OpenGameCallback | 添加打开游戏回调,返回 this |
|
|
583
|
+
| `addOpenActivityCallback(cb)` | cb: OpenActivityCallback | 添加打开活动回调,返回 this |
|
|
584
|
+
| `addSwitchRoomTypeCallback(cb)` | cb: SwitchRoomTypeCallback | 添加切换房间类型回调,返回 this |
|
|
585
|
+
|
|
586
|
+
**静态属性:**
|
|
587
|
+
|
|
588
|
+
| 属性 | 说明 |
|
|
589
|
+
|------|------|
|
|
590
|
+
| `EnterRoom.CallbackType` | 回调类型枚举:OPEN_GIFT_POPUP, OPEN_GAME, OPEN_ACTIVITY, SWITCH_ROOM_TYPE |
|
|
591
|
+
| `EnterRoom.OpenGiftPopupCallback` | 礼物弹窗回调类 |
|
|
592
|
+
| `EnterRoom.OpenGameCallback` | 游戏回调类 |
|
|
593
|
+
| `EnterRoom.OpenActivityCallback` | 活动回调类 |
|
|
594
|
+
| `EnterRoom.SwitchRoomTypeCallback` | 切换房间类型回调类 |
|
|
595
|
+
|
|
596
|
+
### LiveDetect
|
|
597
|
+
|
|
598
|
+
活体检测 Action 数据。
|
|
599
|
+
|
|
600
|
+
| 方法 | 参数 | 说明 |
|
|
601
|
+
|------|------|------|
|
|
602
|
+
| `constructor()` | - | 创建活体检测数据 |
|
|
603
|
+
| `setText(text)` | text: string | 设置显示文本,返回 this |
|
|
604
|
+
|
|
605
|
+
### Onelink
|
|
606
|
+
|
|
607
|
+
Onelink Action 数据。
|
|
608
|
+
|
|
609
|
+
| 方法 | 参数 | 说明 |
|
|
610
|
+
|------|------|------|
|
|
611
|
+
| `constructor(url?)` | url?: string | 创建 Onelink 数据 |
|
|
612
|
+
| `setText(text)` | text: string | 设置显示文本,返回 this |
|
|
613
|
+
| `setUrl(url)` | url: string (必填) | 设置链接 URL,返回 this |
|
|
614
|
+
| `setShare(share)` | share: boolean | 设置是否显示分享弹窗,返回 this |
|
|
615
|
+
|
|
616
|
+
### Action
|
|
617
|
+
|
|
618
|
+
Action 对象,封装类型和数据。
|
|
619
|
+
|
|
620
|
+
| 方法 | 参数 | 说明 |
|
|
621
|
+
|------|------|------|
|
|
622
|
+
| `constructor(type, data)` | type: ActionType, data: ActionData | 创建 Action |
|
|
623
|
+
| `setType(type)` | type: ActionType | 设置 Action 类型,返回 this |
|
|
624
|
+
| `setData(data)` | data: ActionData | 设置 Action 数据,返回 this |
|
|
625
|
+
| `setBaseData(text)` | text: string | 设置数据的 text 字段(便捷方法),返回 this |
|
|
626
|
+
| `toJson()` | - | 序列化为 JSON 字符串 |
|
|
627
|
+
|
|
628
|
+
---
|
|
629
|
+
|
|
630
|
+
## 构建
|
|
631
|
+
|
|
632
|
+
```bash
|
|
633
|
+
# 构建所有 UMD 文件
|
|
634
|
+
npm run build
|
|
635
|
+
|
|
636
|
+
# 仅构建核心包
|
|
637
|
+
npm run build:core
|
|
638
|
+
|
|
639
|
+
# 仅构建完整包
|
|
640
|
+
npm run build:bridge
|
|
641
|
+
|
|
642
|
+
# 监听模式(开发时使用)
|
|
643
|
+
npm run build:watch
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
**产出文件:**
|
|
647
|
+
|
|
648
|
+
| 文件 | 说明 | 全局变量 |
|
|
649
|
+
|------|------|----------|
|
|
650
|
+
| `build/js/action_creator.umd.js` | 核心包(仅 ActionCreator) | `__action_creator__` |
|
|
651
|
+
| `build/js/action_bridge.umd.js` | 完整包(ActionCreator + Bridge + Adapters) | `__action_bridge__` |
|