@jhh96/light-app-bridge 1.3.1 → 1.4.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.
@@ -99,6 +99,14 @@ export interface BridgeOptions {
99
99
  export interface SendOptions {
100
100
  /** 本次超时(ms),不传用实例默认值 */
101
101
  timeout?: number;
102
+ /**
103
+ * 定向发送的目标 webContentsId(round 钉扎投递)。
104
+ * 不传(或 null/undefined)= 发往当前绑定对端(dynamic 模式即当前激活窗口);
105
+ * 显式传历史对端 id = 向该窗口定向发送,绝不漂移到当前 peer——
106
+ * 用于"指令必须送达提问时刻的轻应用"语义。目标窗口已关闭时
107
+ * invoke 抛错快速失败(或按 timeout 超时),不会静默改投其他窗口。
108
+ */
109
+ to?: number | null;
102
110
  }
103
111
  /**
104
112
  * 请求消息处理器。返回值(含 Promise 解析值)会作为回复发回发送方。
@@ -251,6 +259,12 @@ export default class LightAppBridge {
251
259
  private _handleDisconnect;
252
260
  /** 监听对端失联(ping 与 hello 兜底探测均无响应时触发一次) */
253
261
  onDisconnect(cb: (peerId: number | null) => void): this;
262
+ /**
263
+ * 当前绑定对端 webContentsId(未连接/失联时为 null)。
264
+ * 调用方在业务时点(如用户提问)快照此值,之后经 send({to}) 定向发送,
265
+ * 即可保证指令送达该时点所在的轻应用窗口(round 钉扎)。
266
+ */
267
+ get peerId(): number | null;
254
268
  /**
255
269
  * 等待连接建立(握手完成)。构造时已自动发起握手,直接 await 即可。
256
270
  * @returns 对端 webContentsId
@@ -268,10 +268,13 @@ export default class LightAppBridge {
268
268
  if (!this._debug)
269
269
  return;
270
270
  const w = window;
271
- const sendFn = (data, timeout) => {
271
+ // 第二参兼容 number(超时毫秒)与 object(send 的 options 形态):
272
+ // 传 { timeout: 1500 } 时不再被当成数字拼进超时串(曾报"请求超时 ([object Object]ms)")
273
+ const sendFn = (data, opts) => {
272
274
  // eslint-disable-next-line no-console
273
275
  console.log(`[LightAppBridge:${this.role}] send→`, data);
274
- return this.send(data, timeout != null ? { timeout } : undefined);
276
+ const options = typeof opts === 'number' ? { timeout: opts } : opts;
277
+ return this.send(data, options);
275
278
  };
276
279
  const injectFn = (type, data, fromId, id) => {
277
280
  const envelope = this._envelope(type, data != null ? data : {}, id || genId() // res 场景必须传原请求 id 才能配对唤醒(未传则生成新 id,作为陌生 res 被忽略)
@@ -756,6 +759,14 @@ export default class LightAppBridge {
756
759
  this._disconnectCbs.push(cb);
757
760
  return this;
758
761
  }
762
+ /**
763
+ * 当前绑定对端 webContentsId(未连接/失联时为 null)。
764
+ * 调用方在业务时点(如用户提问)快照此值,之后经 send({to}) 定向发送,
765
+ * 即可保证指令送达该时点所在的轻应用窗口(round 钉扎)。
766
+ */
767
+ get peerId() {
768
+ return this._peer;
769
+ }
759
770
  /**
760
771
  * 等待连接建立(握手完成)。构造时已自动发起握手,直接 await 即可。
761
772
  * @returns 对端 webContentsId
@@ -829,12 +840,12 @@ export default class LightAppBridge {
829
840
  return Promise.reject(new Error('实例已销毁'));
830
841
  const timeout = options.timeout != null ? options.timeout : this.timeout;
831
842
  return this.ready().then(() => {
832
- // 发送目标取当前 _peer(而非 ready() resolve 的快照):
833
- // dynamic 模式迁移重绑后,后续 send 立即发往新对端。
834
- const targetId = this._peer;
843
+ // 发送目标:显式 to(round 钉扎)优先;否则取当前 _peer(而非 ready()
844
+ // resolve 的快照)——dynamic 模式迁移重绑后,后续 send 立即发往新对端。
845
+ const targetId = options.to != null ? options.to : this._peer;
835
846
  const id = genId();
836
847
  const envelope = this._envelope('req', data, id);
837
- this._log('send→ id=' + id + ' 目标=' + targetId + ' data=' + debugPayload(data));
848
+ this._log('send→ id=' + id + ' 目标=' + targetId + (options.to != null ? '(钉扎)' : '') + ' data=' + debugPayload(data));
838
849
  return new Promise((resolve, reject) => {
839
850
  const timer = setTimeout(() => {
840
851
  if (this._pending.has(id)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jhh96/light-app-bridge",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "type": "module",
5
5
  "description": "招商随行(WorkLink)插件窗口(plugin)与轻应用窗口(lightapp)之间的双向通信 SDK",
6
6
  "main": "./dist/index.js",