tencentcloud-webar 1.0.29-2 → 1.0.29-3

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.
@@ -0,0 +1,177 @@
1
+ # EventBus 实例级改造指南
2
+
3
+ ## 核心思路
4
+
5
+ 每个SDK实例创建自己的EventBus实例,通过构造函数参数传递给需要的模块。
6
+
7
+ ## 已完成的改造
8
+
9
+ ### 1. SDK主文件 (`src/h5/index.ts`)
10
+
11
+ ```typescript
12
+ export class ArSdk extends AuthedWrap implements ARSdkInterface {
13
+ // 实例级EventBus
14
+ private instanceEventBus: EventObj;
15
+
16
+ constructor(config: Config) {
17
+ super(auth, domain, initReport, reportRegion, language);
18
+
19
+ // 创建实例级EventBus
20
+ this.instanceEventBus = new EventObj();
21
+
22
+ // 传递给ArMainImpl
23
+ this.core = new ArMainImpl(this.input_type, resizeCallback, this.instanceEventBus);
24
+ }
25
+
26
+ private _bindEvent() {
27
+ // 使用实例级EventBus
28
+ const EventBus = this.instanceEventBus;
29
+ EventBus.on("error", (data) => { ... });
30
+ // ... 其他事件监听
31
+ }
32
+ }
33
+ ```
34
+
35
+ ### 2. ArMainImpl (`src/h5/impls/MainImpl.ts`)
36
+
37
+ ```typescript
38
+ export class ArMainImpl extends ArBase {
39
+ // 实例级EventBus
40
+ private eventBus: EventObj;
41
+
42
+ constructor(
43
+ type: EntryType,
44
+ resizeCallback: (...args: any) => any,
45
+ eventBus?: EventObj // 接收实例级EventBus
46
+ ) {
47
+ super();
48
+ // 保存实例级EventBus,如果没传则使用全局EventBus(向后兼容)
49
+ this.eventBus = eventBus || EventBus;
50
+
51
+ // 使用实例EventBus
52
+ this.eventBus.trigger("error", { ... });
53
+ }
54
+ }
55
+ ```
56
+
57
+ ## 待改造的文件
58
+
59
+ 以下文件需要接收EventBus参数并使用实例级EventBus:
60
+
61
+ ### 高优先级(核心模块)
62
+
63
+ 1. **`src/h5/input/Camera.ts`** - CameraInput类
64
+ - 构造函数添加 `eventBus?: EventObj` 参数
65
+ - 替换所有 `EventBus.trigger(...)` 为 `this.eventBus.trigger(...)`
66
+ - 约15+处需要修改
67
+
68
+ 2. **`src/h5/detector/DetectorWorker.ts`** - DetectorWorker类
69
+ - 构造函数添加 `eventBus?: EventObj` 参数
70
+ - 替换约4处EventBus调用
71
+
72
+ 3. **`src/h5/detector/DetectorBase.ts`** - DetectorBase类
73
+ - 构造函数添加 `eventBus?: EventObj` 参数
74
+ - 替换EventBus调用
75
+
76
+ 4. **`src/h5/utils.ts`** - 工具函数
77
+ - 需要将eventBus作为参数传入相关函数
78
+ - 约5处需要修改
79
+
80
+ ### 中优先级
81
+
82
+ 5. **`src/auth/index.ts`** - Auth类
83
+ - 约6处EventBus调用
84
+
85
+ 6. **`src/core/renderer/Effect.ts`** - Effect类
86
+ - 渲染相关事件触发
87
+
88
+ 7. **`src/core/loaders/EncLoader.ts`** - 加载器
89
+ - 加载事件触发
90
+
91
+ ## 改造步骤(针对每个文件)
92
+
93
+ ### 步骤1: 添加eventBus属性
94
+
95
+ ```typescript
96
+ export class YourClass {
97
+ private eventBus: EventObj;
98
+
99
+ constructor(...existingParams, eventBus?: EventObj) {
100
+ // 保存实例级EventBus,如果没传则使用全局EventBus(向后兼容)
101
+ this.eventBus = eventBus || EventBus;
102
+ }
103
+ }
104
+ ```
105
+
106
+ ### 步骤2: 替换EventBus调用
107
+
108
+ **查找:**
109
+ ```typescript
110
+ EventBus.trigger("eventName", data);
111
+ EventBus.on("eventName", callback);
112
+ ```
113
+
114
+ **替换为:**
115
+ ```typescript
116
+ this.eventBus.trigger("eventName", data);
117
+ this.eventBus.on("eventName", callback);
118
+ ```
119
+
120
+ ### 步骤3: 传递eventBus给子模块
121
+
122
+ 如果该类创建了其他需要EventBus的子模块:
123
+
124
+ ```typescript
125
+ // 创建子模块时传递eventBus
126
+ this.subModule = new SubModule(...params, this.eventBus);
127
+ ```
128
+
129
+ ## 注意事项
130
+
131
+ 1. **向后兼容**:所有eventBus参数都设为可选 `eventBus?: EventObj`,默认使用全局EventBus
132
+ 2. **不要删除全局EventBus**:保留 `import { EventBus } from "src/utils/event"` 作为fallback
133
+ 3. **逐步迁移**:可以一个文件一个文件地改,不影响其他模块
134
+ 4. **测试验证**:每改完一个模块,测试该模块功能是否正常
135
+
136
+ ## 示例:改造Camera.ts
137
+
138
+ ```typescript
139
+ // 改造前
140
+ export class CameraInput {
141
+ constructor(config: CameraConfig) {
142
+ // ...
143
+ EventBus.trigger("cameraReady", this.camera);
144
+ }
145
+ }
146
+
147
+ // 改造后
148
+ export class CameraInput {
149
+ private eventBus: EventObj;
150
+
151
+ constructor(config: CameraConfig, eventBus?: EventObj) {
152
+ this.eventBus = eventBus || EventBus;
153
+ // ...
154
+ this.eventBus.trigger("cameraReady", this.camera);
155
+ }
156
+ }
157
+
158
+ // 调用处(在ArMainImpl中)
159
+ this.inputNode = new CameraInput(config, this.eventBus);
160
+ ```
161
+
162
+ ## 验证方法
163
+
164
+ 1. 创建多个SDK实例
165
+ 2. 每个实例监听相同的事件
166
+ 3. 触发其中一个实例的事件
167
+ 4. 验证只有对应实例的监听器被触发,其他实例不受影响
168
+
169
+ ```typescript
170
+ const sdk1 = new ArSdk(config1);
171
+ const sdk2 = new ArSdk(config2);
172
+
173
+ sdk1.on("error", (data) => console.log("SDK1 error:", data));
174
+ sdk2.on("error", (data) => console.log("SDK2 error:", data));
175
+
176
+ // 只应该触发sdk1的error监听器
177
+ ```