@jhh96/light-app-bridge 1.0.1 → 1.0.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.
package/README.md CHANGED
@@ -19,10 +19,12 @@
19
19
 
20
20
  | 监听后端 | 优先级 | 适用环境 | 说明 |
21
21
  |---------|--------|---------|------|
22
- | `window.waltz.event.on('native-call-js')` | 高(优先) | 有 waltz 注入的环境 | 推荐路径 |
23
- | `window.handleMessageFromNative` | 低(回退) | 无 waltz 的环境 | hook 此全局方法(waltzBridge 投递消息的统一入口,`sendMessageToPluginLightAppById` 最终会调它) |
22
+ | `window.waltz.event.on('native-call-js')` | 高(优先) | 有 waltz 注入的环境 | 推荐路径,立即可用 |
23
+ | `window.handleMessageFromNative` | 低(回退) | 无 waltz 的环境 | hook 此全局方法(waltzBridge 投递消息的统一入口)。**可能晚于构造注入**,SDK 会轮询等待(上限 10s) |
24
24
 
25
- 两者都不可用时构造抛异常。SDK 内部按 waltz → handleMessageFromNative 顺序探测,无需手动指定。
25
+ - 两者都始终不可用时,`connect()`/`ready()` reject(构造不抛,因监听后端允许稍后出现)
26
+ - SDK 内部按 waltz → handleMessageFromNative 顺序探测,无需手动指定
27
+ - 握手会等监听就绪后才开始,避免早到的 ack/hello 丢失
26
28
 
27
29
  ## 安装(两个项目)
28
30
 
@@ -68,16 +68,17 @@ export default class LightAppBridge {
68
68
  private _connectDeferred;
69
69
  private _connectPromise;
70
70
  private _unlisten;
71
+ private _listenerReady;
71
72
  /**
72
73
  * @param role 角色,省略时按 window.worklink.plugin 是否存在自动探测
73
74
  * @param options 超时配置
74
- * @throws 依赖未就绪时抛出(window.worklink / 监听后端)
75
+ * @throws 依赖未就绪时抛出(window.worklink;监听后端可稍后出现,不算构造失败)
75
76
  */
76
77
  constructor(role?: BridgeRole, options?: BridgeOptions);
77
78
  /** 校验依赖是否就绪,缺失抛出明确异常 */
78
79
  private _checkDeps;
79
- /** 绑定消息监听:优先 waltz,回退 ipc(自动选择) */
80
- private _bindListener;
80
+ /** 异步绑定消息监听:优先 waltz,回退 hook handleMessageFromNative(可能需等待其注入) */
81
+ private _startListener;
81
82
  /** 构造消息信封 */
82
83
  private _envelope;
83
84
  /** 处理一条归一化后的消息 */
@@ -52,58 +52,77 @@ function getInvoke() {
52
52
  }
53
53
  return null;
54
54
  }
55
+ /** 等待 handleMessageFromNative 出现的轮询间隔(ms) */
56
+ const BACKEND_POLL_INTERVAL = 100;
57
+ /** 等待监听后端就绪的超时(ms) */
58
+ const BACKEND_POLL_TIMEOUT = 10000;
55
59
  /** waltz 路径是否可用 */
56
60
  function hasWaltz() {
57
61
  const w = window.waltz;
58
62
  return !!(w && w.event && typeof w.event.on === 'function');
59
63
  }
64
+ /** handleMessageFromNative 是否可用 */
65
+ function hasHandleMessageFromNative() {
66
+ return typeof window.handleMessageFromNative === 'function';
67
+ }
68
+ /** 轮询等待条件成立 */
69
+ function waitFor(check, timeout) {
70
+ return new Promise((resolve, reject) => {
71
+ const start = Date.now();
72
+ const tick = () => {
73
+ if (check())
74
+ return resolve();
75
+ if (Date.now() - start > timeout) {
76
+ return reject(new Error(`等待超时 (${timeout}ms)`));
77
+ }
78
+ setTimeout(tick, BACKEND_POLL_INTERVAL);
79
+ };
80
+ tick();
81
+ });
82
+ }
60
83
  /**
61
- * 选择可用的监听后端:优先 waltz,回退 hook window.handleMessageFromNative。
62
- * 两者都不可用时抛异常。
84
+ * 异步解析可用的监听后端并绑定:优先 waltz,回退 hook window.handleMessageFromNative。
85
+ * 无 waltz 且 handleMessageFromNative 尚未注入时,轮询等待其出现(上限 BACKEND_POLL_TIMEOUT)。
86
+ * 两者始终不可用则 reject。
63
87
  */
64
- function pickMessageBackend() {
65
- // 1. waltz 路径:有 waltz 的环境
88
+ function bindMessageBackend(cb) {
89
+ const w = window;
90
+ // 1. waltz 路径:有 waltz 的环境(立即可用)
66
91
  if (hasWaltz()) {
67
- return (cb) => {
68
- const handler = (raw) => {
69
- const msg = parseHandleMessageFromNative(raw);
70
- if (msg)
71
- cb(msg);
72
- };
73
- window.waltz.event.on('native-call-js', handler);
74
- return () => {
75
- const ev = window.waltz?.event;
76
- if (ev && typeof ev.off === 'function')
77
- ev.off('native-call-js', handler);
78
- else if (ev && typeof ev.removeListener === 'function')
79
- ev.removeListener('native-call-js', handler);
80
- };
92
+ const handler = (raw) => {
93
+ const msg = parseHandleMessageFromNative(raw);
94
+ if (msg)
95
+ cb(msg);
81
96
  };
97
+ w.waltz.event.on('native-call-js', handler);
98
+ return Promise.resolve(() => {
99
+ var _a;
100
+ const ev = (_a = w.waltz) === null || _a === void 0 ? void 0 : _a.event;
101
+ if (ev && typeof ev.off === 'function')
102
+ ev.off('native-call-js', handler);
103
+ else if (ev && typeof ev.removeListener === 'function')
104
+ ev.removeListener('native-call-js', handler);
105
+ });
82
106
  }
83
- // 2. hook handleMessageFromNative 路径:无 waltz 时
84
- // waltzBridge 投递消息的统一入口:handleMessageFromNative(JSON.stringify({responseId, responseData}))
85
- // 其中 responseData = {code, body:{eventName, params:{message, fromWebContentsId}}, msg}
86
- if (typeof window.handleMessageFromNative === 'function') {
87
- return (cb) => {
88
- const w = window;
89
- const prev = w.handleMessageFromNative;
90
- const wrapped = function (raw) {
91
- const msg = parseHandleMessageFromNative(raw);
92
- if (msg)
93
- cb(msg);
94
- // 透传给原 handler(若存在),保持 waltz/业务自身逻辑不被破坏
95
- if (typeof prev === 'function')
96
- return prev.apply(this, arguments);
97
- };
98
- w.handleMessageFromNative = wrapped;
99
- return () => {
100
- // 还原:仅当仍为自身 wrapped 时才还原,避免覆盖后续可能的重新赋值
101
- if (w.handleMessageFromNative === wrapped)
102
- w.handleMessageFromNative = prev;
103
- };
107
+ // 2. hook handleMessageFromNative 路径:无 waltz 时。
108
+ // 它可能晚于本 SDK 构造才被 preload/waltzBridge 注册,故轮询等待。
109
+ return waitFor(hasHandleMessageFromNative, BACKEND_POLL_TIMEOUT).then(() => {
110
+ const prev = w.handleMessageFromNative;
111
+ const wrapped = function (raw) {
112
+ const msg = parseHandleMessageFromNative(raw);
113
+ if (msg)
114
+ cb(msg);
115
+ // 透传给原 handler(若存在),保持 waltz/业务自身逻辑不被破坏
116
+ if (typeof prev === 'function')
117
+ return prev.apply(this, arguments);
104
118
  };
105
- }
106
- throw new Error('LightAppBridge 无可用监听后端:window.waltz.event 与 window.handleMessageFromNative 均不可用');
119
+ w.handleMessageFromNative = wrapped;
120
+ return () => {
121
+ // 还原:仅当仍为自身 wrapped 时才还原,避免覆盖后续可能的重新赋值
122
+ if (w.handleMessageFromNative === wrapped)
123
+ w.handleMessageFromNative = prev;
124
+ };
125
+ });
107
126
  }
108
127
  /**
109
128
  * 解析 handleMessageFromNative / waltz native-call-js 的统一数据格式。
@@ -136,7 +155,7 @@ export default class LightAppBridge {
136
155
  /**
137
156
  * @param role 角色,省略时按 window.worklink.plugin 是否存在自动探测
138
157
  * @param options 超时配置
139
- * @throws 依赖未就绪时抛出(window.worklink / 监听后端)
158
+ * @throws 依赖未就绪时抛出(window.worklink;监听后端可稍后出现,不算构造失败)
140
159
  */
141
160
  constructor(role, options = {}) {
142
161
  this._pending = new Map();
@@ -150,6 +169,7 @@ export default class LightAppBridge {
150
169
  this._handshakeTimer = null;
151
170
  this._started = false;
152
171
  this._unlisten = null;
172
+ this._listenerReady = null;
153
173
  let r = role === 'plugin' || role === 'lightapp' ? role : 'lightapp';
154
174
  if (role !== 'plugin' && role !== 'lightapp') {
155
175
  const wl = window.worklink;
@@ -165,8 +185,8 @@ export default class LightAppBridge {
165
185
  return peerId;
166
186
  });
167
187
  this._checkDeps(); // 依赖校验,缺失直接抛异常
168
- this._bindListener(); // 绑定消息监听(waltz 或 ipc,自动选择)
169
- this.connect(); // 构造即自动发起握手
188
+ this._startListener(); // 异步绑定消息监听(waltz 或 handleMessageFromNative)
189
+ this.connect(); // 构造即自动发起握手(内部会等监听就绪)
170
190
  }
171
191
  /** 校验依赖是否就绪,缺失抛出明确异常 */
172
192
  _checkDeps() {
@@ -177,17 +197,23 @@ export default class LightAppBridge {
177
197
  if (this.role === 'plugin' && typeof (w.worklink.plugin && w.worklink.plugin.getContentContext) !== 'function') {
178
198
  throw new Error('LightAppBridge(plugin) 依赖 window.worklink.plugin.getContentContext,当前环境不可用');
179
199
  }
180
- if (!hasWaltz() && typeof w.handleMessageFromNative !== 'function') {
181
- throw new Error('LightAppBridge 无可用监听后端:window.waltz.event 与 window.handleMessageFromNative 均不可用');
182
- }
200
+ // 监听后端(waltz / handleMessageFromNative)允许稍后出现,由 _startListener 异步等待,
201
+ // 不在构造时强校验;两者始终未出现时 connect()/ready() 会 reject。
183
202
  }
184
- /** 绑定消息监听:优先 waltz,回退 ipc(自动选择) */
185
- _bindListener() {
203
+ /** 异步绑定消息监听:优先 waltz,回退 hook handleMessageFromNative(可能需等待其注入) */
204
+ _startListener() {
186
205
  if (this._started)
187
206
  return;
188
207
  this._started = true;
189
- const backend = pickMessageBackend();
190
- this._unlisten = backend((msg) => this._onMessage(msg));
208
+ this._listenerReady = bindMessageBackend((msg) => this._onMessage(msg))
209
+ .then((unlisten) => {
210
+ this._unlisten = unlisten;
211
+ })
212
+ .catch((e) => {
213
+ // 监听后端不可用:让连接失败并给出明确原因
214
+ this._connectDeferred.reject(new Error('LightAppBridge 无可用监听后端:window.waltz.event 与 window.handleMessageFromNative 均不可用 (' +
215
+ (e && e.message ? e.message : String(e)) + ')'));
216
+ });
191
217
  }
192
218
  /** 构造消息信封 */
193
219
  _envelope(type, data, id = genId()) {
@@ -224,34 +250,42 @@ export default class LightAppBridge {
224
250
  * @returns 对端 webContentsId
225
251
  */
226
252
  connect() {
253
+ var _a;
227
254
  if (this._connectStarted)
228
255
  return this._connectPromise;
229
256
  this._connectStarted = true;
230
- // 握手超时保护
257
+ // 等监听后端就绪(handleMessageFromNative 可能需等待注入)后再握手,
258
+ // 避免对端的 ack/hello 在监听绑定前到达而丢失
259
+ const listenerReady = (_a = this._listenerReady) !== null && _a !== void 0 ? _a : Promise.resolve();
260
+ // 握手超时保护(含监听等待时间)
231
261
  this._handshakeTimer = setTimeout(() => {
232
262
  if (!this._connected) {
233
263
  this._connectDeferred.reject(new Error(`握手超时 (${this.handshakeTimeout}ms),对端未响应`));
234
264
  this._clearHelloTimer();
235
265
  }
236
266
  }, this.handshakeTimeout);
237
- if (this.role === 'plugin') {
238
- // 插件:解析目标后发 hello,并重试直到收到 ack
239
- ;
240
- window.worklink.plugin.getContentContext()
241
- .then((ctx) => {
242
- if (!ctx || ctx.webContentsId == null) {
243
- throw new Error('getContentContext 未返回 webContentsId');
244
- }
245
- this._target = ctx.webContentsId;
246
- this._sendHello();
247
- this._helloTimer = setInterval(() => {
248
- if (!this._connected)
249
- this._sendHello();
250
- }, HELLO_RETRY_INTERVAL);
251
- })
252
- .catch((e) => this._connectDeferred.reject(e));
253
- }
254
- // lightapp 被动等待 hello(listener 已绑定),收到后在 _onHello 中回 ack 并 resolve
267
+ listenerReady
268
+ .then(() => {
269
+ if (this._connected)
270
+ return;
271
+ if (this.role === 'plugin') {
272
+ // 插件:解析目标后发 hello,并重试直到收到 ack
273
+ return window.worklink.plugin.getContentContext()
274
+ .then((ctx) => {
275
+ if (!ctx || ctx.webContentsId == null) {
276
+ throw new Error('getContentContext 未返回 webContentsId');
277
+ }
278
+ this._target = ctx.webContentsId;
279
+ this._sendHello();
280
+ this._helloTimer = setInterval(() => {
281
+ if (!this._connected)
282
+ this._sendHello();
283
+ }, HELLO_RETRY_INTERVAL);
284
+ });
285
+ }
286
+ // lightapp 被动等待 hello(listener 已绑定),收到后在 _onHello 中回 ack 并 resolve
287
+ })
288
+ .catch((e) => this._connectDeferred.reject(e));
255
289
  return this._connectPromise;
256
290
  }
257
291
  /** 发送 hello(携带自身 webContentsId) */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jhh96/light-app-bridge",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "description": "招商随行(WorkLink)插件窗口(plugin)与轻应用窗口(lightapp)之间的双向通信 SDK",
6
6
  "main": "./dist/index.js",