@jhh96/light-app-bridge 1.1.1 → 1.2.1
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/lightAppBridge.d.ts +22 -0
- package/dist/lightAppBridge.js +62 -0
- package/package.json +1 -1
package/dist/lightAppBridge.d.ts
CHANGED
|
@@ -139,6 +139,28 @@ export default class LightAppBridge {
|
|
|
139
139
|
constructor(role?: BridgeRole, options?: BridgeOptions);
|
|
140
140
|
/** debug 日志(options.debug 开启时打印,前缀带角色便于双端对照) */
|
|
141
141
|
private _log;
|
|
142
|
+
/**
|
|
143
|
+
* debug 模式下在 window 挂载控制台测试方法(挂在单一入口 __lightAppBridge 下,
|
|
144
|
+
* destroy 时注销):
|
|
145
|
+
*
|
|
146
|
+
* __lightAppBridge.send(data, timeout?) —— 以本实例身份向对端发一条请求,返回 Promise。
|
|
147
|
+
* 等价于业务代码里的 bridge.send(...)。
|
|
148
|
+
* await __lightAppBridge.send({ action: 'ping' })
|
|
149
|
+
* await __lightAppBridge.send({ action: 'scroll.to', payload: {...} }, 5000)
|
|
150
|
+
*
|
|
151
|
+
* __lightAppBridge.inject(type, data?, fromId?, id?) —— 模拟对端来一条消息,直接喂给
|
|
152
|
+
* 本实例的消息分发入口(绕过原生投递层),无需真实对端窗口即可测试收发逻辑。
|
|
153
|
+
* type 取信封类型:'hello' | 'ack' | 'req' | 'res' | 'ping'
|
|
154
|
+
* __lightAppBridge.inject('ping') // 看本端自动回 pong
|
|
155
|
+
* __lightAppBridge.inject('req', { action: 'test' }) // 触发本端 onMessage handler
|
|
156
|
+
* __lightAppBridge.inject('hello', null, 42) // 模拟 webContentsId=42 的对端握手
|
|
157
|
+
* __lightAppBridge.inject('res', {ok:1}, 77, reqId) // 回 res:第 4 参传原请求 id 配对唤醒
|
|
158
|
+
*
|
|
159
|
+
* 多实例同时开启 debug 时后注册者覆盖前者(控制台操作的是最后创建的实例)。
|
|
160
|
+
* 不挂实例本身(避免控制台误触内部状态)。
|
|
161
|
+
*/
|
|
162
|
+
private _registerDebugHelpers;
|
|
163
|
+
private _debugCleanup;
|
|
142
164
|
/** 校验依赖是否就绪,缺失抛出明确异常 */
|
|
143
165
|
private _checkDeps;
|
|
144
166
|
/** 绑定消息监听:优先 waltz;无 waltz 时定义/包装 window.handleMessageFromNative */
|
package/dist/lightAppBridge.js
CHANGED
|
@@ -202,6 +202,7 @@ export default class LightAppBridge {
|
|
|
202
202
|
this._probeOk = false; // 兜底探测是否收到 ack
|
|
203
203
|
this._probeTimers = []; // 防 destroy 后悬挂
|
|
204
204
|
this._disconnectCbs = [];
|
|
205
|
+
this._debugCleanup = null;
|
|
205
206
|
let r = role === 'plugin' || role === 'lightapp' ? role : 'lightapp';
|
|
206
207
|
if (role !== 'plugin' && role !== 'lightapp') {
|
|
207
208
|
const wl = window.worklink;
|
|
@@ -232,6 +233,7 @@ export default class LightAppBridge {
|
|
|
232
233
|
});
|
|
233
234
|
this._checkDeps(); // 依赖校验,缺失直接抛异常
|
|
234
235
|
this._startListener(); // 绑定消息监听(waltz 或定义 handleMessageFromNative)
|
|
236
|
+
this._registerDebugHelpers(); // debug 模式:注册控制台测试方法
|
|
235
237
|
this.connect(); // 构造即自动发起握手(内部会等监听就绪)
|
|
236
238
|
}
|
|
237
239
|
/** debug 日志(options.debug 开启时打印,前缀带角色便于双端对照) */
|
|
@@ -240,6 +242,58 @@ export default class LightAppBridge {
|
|
|
240
242
|
return;
|
|
241
243
|
console.log(`[LightAppBridge:${this.role}]`, ...args);
|
|
242
244
|
}
|
|
245
|
+
// —— debug 控制台测试方法(options.debug 开启时挂 window)—— //
|
|
246
|
+
/**
|
|
247
|
+
* debug 模式下在 window 挂载控制台测试方法(挂在单一入口 __lightAppBridge 下,
|
|
248
|
+
* destroy 时注销):
|
|
249
|
+
*
|
|
250
|
+
* __lightAppBridge.send(data, timeout?) —— 以本实例身份向对端发一条请求,返回 Promise。
|
|
251
|
+
* 等价于业务代码里的 bridge.send(...)。
|
|
252
|
+
* await __lightAppBridge.send({ action: 'ping' })
|
|
253
|
+
* await __lightAppBridge.send({ action: 'scroll.to', payload: {...} }, 5000)
|
|
254
|
+
*
|
|
255
|
+
* __lightAppBridge.inject(type, data?, fromId?, id?) —— 模拟对端来一条消息,直接喂给
|
|
256
|
+
* 本实例的消息分发入口(绕过原生投递层),无需真实对端窗口即可测试收发逻辑。
|
|
257
|
+
* type 取信封类型:'hello' | 'ack' | 'req' | 'res' | 'ping'
|
|
258
|
+
* __lightAppBridge.inject('ping') // 看本端自动回 pong
|
|
259
|
+
* __lightAppBridge.inject('req', { action: 'test' }) // 触发本端 onMessage handler
|
|
260
|
+
* __lightAppBridge.inject('hello', null, 42) // 模拟 webContentsId=42 的对端握手
|
|
261
|
+
* __lightAppBridge.inject('res', {ok:1}, 77, reqId) // 回 res:第 4 参传原请求 id 配对唤醒
|
|
262
|
+
*
|
|
263
|
+
* 多实例同时开启 debug 时后注册者覆盖前者(控制台操作的是最后创建的实例)。
|
|
264
|
+
* 不挂实例本身(避免控制台误触内部状态)。
|
|
265
|
+
*/
|
|
266
|
+
_registerDebugHelpers() {
|
|
267
|
+
if (!this._debug)
|
|
268
|
+
return;
|
|
269
|
+
const w = window;
|
|
270
|
+
const sendFn = (data, timeout) => {
|
|
271
|
+
// eslint-disable-next-line no-console
|
|
272
|
+
console.log(`[LightAppBridge:${this.role}] send→`, data);
|
|
273
|
+
return this.send(data, timeout != null ? { timeout } : undefined);
|
|
274
|
+
};
|
|
275
|
+
const injectFn = (type, data, fromId, id) => {
|
|
276
|
+
const envelope = this._envelope(type, data != null ? data : {}, id || genId() // res 场景必须传原请求 id 才能配对唤醒(未传则生成新 id,作为陌生 res 被忽略)
|
|
277
|
+
);
|
|
278
|
+
// eslint-disable-next-line no-console
|
|
279
|
+
console.log(`[LightAppBridge:${this.role}] inject←`, envelope);
|
|
280
|
+
this._onMessage({ envelope, fromWebContentsId: fromId != null ? fromId : 0 });
|
|
281
|
+
};
|
|
282
|
+
const facade = { send: sendFn, inject: injectFn };
|
|
283
|
+
const prev = w.__lightAppBridge; // 多实例 debug:保留被覆盖者的清理引用链
|
|
284
|
+
w.__lightAppBridge = facade;
|
|
285
|
+
// eslint-disable-next-line no-console
|
|
286
|
+
console.info(`[LightAppBridge:${this.role}] debug 方法已挂载: ` +
|
|
287
|
+
'await __lightAppBridge.send(data, timeout?) 发请求 / ' +
|
|
288
|
+
'__lightAppBridge.inject(type, data?, fromId?, id?) 模拟来消息');
|
|
289
|
+
this._debugCleanup = () => {
|
|
290
|
+
// 仅当仍是自己挂的 facade 才清理,避免误删后建实例的挂载
|
|
291
|
+
if (w.__lightAppBridge === facade)
|
|
292
|
+
delete w.__lightAppBridge;
|
|
293
|
+
else
|
|
294
|
+
w.__lightAppBridge = prev;
|
|
295
|
+
};
|
|
296
|
+
}
|
|
243
297
|
/** 校验依赖是否就绪,缺失抛出明确异常 */
|
|
244
298
|
_checkDeps() {
|
|
245
299
|
const w = window;
|
|
@@ -821,6 +875,14 @@ export default class LightAppBridge {
|
|
|
821
875
|
catch { /* ignore */ }
|
|
822
876
|
this._unlisten = null;
|
|
823
877
|
}
|
|
878
|
+
// 注销 debug 控制台测试方法
|
|
879
|
+
if (this._debugCleanup) {
|
|
880
|
+
try {
|
|
881
|
+
this._debugCleanup();
|
|
882
|
+
}
|
|
883
|
+
catch { /* ignore */ }
|
|
884
|
+
this._debugCleanup = null;
|
|
885
|
+
}
|
|
824
886
|
this._clearHelloTimer();
|
|
825
887
|
this._stopDynamicPoll();
|
|
826
888
|
this._stopPing();
|