@wapri/gjsbridge 3.0.0 → 5.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 wapri contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @wapri/gjsbridge
2
+
3
+ iOS、Android 与鸿蒙 WebView 的统一桥接封装,负责请求 ID、超时、错误码与 Native 事件订阅;通道名与缓存键由容器通过 `createJSB()` 注入。
4
+
5
+ ## 在线文档
6
+
7
+ https://sdmu-gaoqi.github.io/wapri/jsbridges
@@ -47,7 +47,7 @@ export var BridgeCB = /*#__PURE__*/function () {
47
47
  };
48
48
  return new Promise(function (_resolve, reject) {
49
49
  var timer = window.setTimeout(function () {
50
- _this2.settleRequest(requestId, new Error('KMBridge 请求超时'));
50
+ _this2.settleRequest(requestId, new Error('Native 请求超时'));
51
51
  }, (options === null || options === void 0 ? void 0 : options.timeout) || DEFAULT_TIMEOUT);
52
52
  _this2.pendingRequests.set(requestId, {
53
53
  resolve: function resolve(data) {
@@ -59,7 +59,7 @@ export var BridgeCB = /*#__PURE__*/function () {
59
59
  try {
60
60
  transport(request);
61
61
  } catch (error) {
62
- _this2.settleRequest(requestId, error instanceof Error ? error : new Error('KMBridge 调用失败'));
62
+ _this2.settleRequest(requestId, error instanceof Error ? error : new Error('Native 调用失败'));
63
63
  }
64
64
  });
65
65
  }
@@ -118,7 +118,7 @@ export var BridgeCB = /*#__PURE__*/function () {
118
118
  value: function destroy() {
119
119
  this.pendingRequests.forEach(function (pendingRequest) {
120
120
  clearTimeout(pendingRequest.timer);
121
- pendingRequest.reject(new Error('KMBridge 已销毁'));
121
+ pendingRequest.reject(new Error('Native 已销毁'));
122
122
  });
123
123
  this.pendingRequests.clear();
124
124
  this.eventListeners.clear();
@@ -1,6 +1,8 @@
1
1
  import type { BridgeApiConfig } from '../registers';
2
2
  import { BridgeCB } from './bridgeCB';
3
3
  import type { JSBridgeConfig } from '../types/bridge';
4
+ /** 默认配置:通道名与缓存键都是通用值,业务可通过 `createJSB` 覆盖。 */
5
+ export declare const DEFAULT_JSBRIDGE_CONFIG: JSBridgeConfig;
4
6
  /** 只负责三端 Native transport,并复用 BridgeCB 的回调能力。 */
5
7
  export declare class JSBNative {
6
8
  /** 当前实例的运行时配置。 */
@@ -8,7 +10,7 @@ export declare class JSBNative {
8
10
  /** 可复用的请求响应与事件回调管理器。 */
9
11
  readonly cb: BridgeCB;
10
12
  /** 挂载供 Native 调用的全局响应与事件回调。 */
11
- constructor();
13
+ constructor(config?: Partial<JSBridgeConfig>);
12
14
  /** 设置是否打印当前实例与 Native 通信的原始数据。 */
13
15
  setDebug(debug: boolean): void;
14
16
  /**
@@ -32,7 +34,7 @@ export declare class JSBNative {
32
34
  * 按业务方法获取 iOS 消息通道,未注入时回退到 Android/鸿蒙通道。
33
35
  *
34
36
  * iOS 使用 `webkit.messageHandlers[method]`,报文中不重复携带 method;
35
- * Android/鸿蒙继续使用 `KMBridge.handle`,报文中携带完整请求。
37
+ * Android/鸿蒙使用 `window[nativeChannel].handle`,报文中携带完整请求。
36
38
  *
37
39
  * @param method 本次调用对应的 Native 方法名。
38
40
  * @returns 可调用的 Native 消息发送函数;容器未注入时返回 undefined。
@@ -1,4 +1,6 @@
1
1
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
2
4
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3
5
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
4
6
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
@@ -7,19 +9,27 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
7
9
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
8
10
  import { isFunction, isUndefined } from '@wapri/gbase';
9
11
  import { BridgeCB } from "./bridgeCB";
10
- import { getBridgeWindow } from "../utils";
12
+ import { getBridgeWindow, getNativeChannel } from "../utils";
13
+
14
+ /** 默认配置:通道名与缓存键都是通用值,业务可通过 `createJSB` 覆盖。 */
15
+ export var DEFAULT_JSBRIDGE_CONFIG = {
16
+ debug: false,
17
+ nativeChannel: 'JSBridge',
18
+ globalKey: '__sdkJSBridge'
19
+ };
11
20
 
12
21
  /** 只负责三端 Native transport,并复用 BridgeCB 的回调能力。 */
13
22
  export var JSBNative = /*#__PURE__*/function () {
14
23
  /** 挂载供 Native 调用的全局响应与事件回调。 */
15
24
  function JSBNative() {
25
+ var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
16
26
  _classCallCheck(this, JSBNative);
17
27
  /** 当前实例的运行时配置。 */
18
- _defineProperty(this, "config", {
19
- debug: false
20
- });
28
+ _defineProperty(this, "config", void 0);
21
29
  /** 可复用的请求响应与事件回调管理器。 */
22
- _defineProperty(this, "cb", new BridgeCB(this.config));
30
+ _defineProperty(this, "cb", void 0);
31
+ this.config = _objectSpread(_objectSpread({}, DEFAULT_JSBRIDGE_CONFIG), config);
32
+ this.cb = new BridgeCB(this.config);
23
33
  this.installNativeCallbacks();
24
34
  }
25
35
 
@@ -41,12 +51,13 @@ export var JSBNative = /*#__PURE__*/function () {
41
51
  value: function callNative(method, params, options) {
42
52
  var transport = this.getNativeTransport(method);
43
53
  if (isUndefined(transport)) {
44
- console.log('KMBridge 当前未就绪');
54
+ var message = "Native \u901A\u9053\u672A\u5C31\u7EEA\uFF08".concat(this.config.nativeChannel, "\uFF09");
55
+ console.log(message);
45
56
  console.dir({
46
57
  method: method,
47
58
  params: params
48
59
  });
49
- return Promise.reject(new Error('KMBridge 当前未就绪'));
60
+ return Promise.reject(new Error(message));
50
61
  }
51
62
  return this.cb.call(method, params, transport, options);
52
63
  }
@@ -75,7 +86,7 @@ export var JSBNative = /*#__PURE__*/function () {
75
86
  value: function webReady() {
76
87
  var _bridgeWindow$webkit;
77
88
  var bridgeWindow = getBridgeWindow();
78
- return !isUndefined(bridgeWindow === null || bridgeWindow === void 0 ? void 0 : bridgeWindow.KMBridge) || !isUndefined(bridgeWindow === null || bridgeWindow === void 0 || (_bridgeWindow$webkit = bridgeWindow.webkit) === null || _bridgeWindow$webkit === void 0 ? void 0 : _bridgeWindow$webkit.messageHandlers);
89
+ return !isUndefined(getNativeChannel(this.config.nativeChannel)) || !isUndefined(bridgeWindow === null || bridgeWindow === void 0 || (_bridgeWindow$webkit = bridgeWindow.webkit) === null || _bridgeWindow$webkit === void 0 ? void 0 : _bridgeWindow$webkit.messageHandlers);
79
90
  }
80
91
 
81
92
  /** 将 BridgeCB 的处理函数注册为 Native 回调入口。 */
@@ -94,7 +105,7 @@ export var JSBNative = /*#__PURE__*/function () {
94
105
  * 按业务方法获取 iOS 消息通道,未注入时回退到 Android/鸿蒙通道。
95
106
  *
96
107
  * iOS 使用 `webkit.messageHandlers[method]`,报文中不重复携带 method;
97
- * Android/鸿蒙继续使用 `KMBridge.handle`,报文中携带完整请求。
108
+ * Android/鸿蒙使用 `window[nativeChannel].handle`,报文中携带完整请求。
98
109
  *
99
110
  * @param method 本次调用对应的 Native 方法名。
100
111
  * @returns 可调用的 Native 消息发送函数;容器未注入时返回 undefined。
@@ -119,7 +130,7 @@ export var JSBNative = /*#__PURE__*/function () {
119
130
  postMessage(message);
120
131
  };
121
132
  }
122
- var nativeBridge = bridgeWindow === null || bridgeWindow === void 0 ? void 0 : bridgeWindow.KMBridge;
133
+ var nativeBridge = getNativeChannel(this.config.nativeChannel);
123
134
  if (isFunction(nativeBridge === null || nativeBridge === void 0 ? void 0 : nativeBridge.handle)) {
124
135
  var handle = nativeBridge.handle.bind(nativeBridge);
125
136
  return function (request) {
package/dist/index.d.ts CHANGED
@@ -1,6 +1,16 @@
1
- import type { JSBridge } from './types/bridge';
1
+ import type { JSBridge, JSBridgeConfig } from './types/bridge';
2
2
  export { BridgeError } from './errors';
3
+ export { DEFAULT_JSBRIDGE_CONFIG } from './core';
3
4
  export type { JSBridgeConfig } from './types/bridge';
4
- /** 业务侧唯一入口:使用 jsbridge.<具体 Native 方法名>(params)。 */
5
+ /**
6
+ * 创建并缓存业务侧唯一的 Bridge 代理。
7
+ *
8
+ * 代理会将未注册的属性访问转换为 Native 方法调用,同时屏蔽底层通道与回调实现。
9
+ * Native 通道名(`nativeChannel`)与缓存键(`globalKey`)由业务传入,库内只提供通用默认值。
10
+ *
11
+ * @param config 本次创建的运行时配置,只需传要覆盖的字段。
12
+ */
13
+ export declare const createJSB: (config?: Partial<JSBridgeConfig>) => JSBridge;
14
+ /** 业务侧默认入口:使用 jsbridge.<具体 Native 方法名>(params)。 */
5
15
  export declare const jsbridge: JSBridge;
6
16
  export default jsbridge;
package/dist/index.js CHANGED
@@ -1,17 +1,18 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
1
  var _excluded = ["nativeMethod", "initialParams"];
2
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
3
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
3
5
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
4
6
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
5
7
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
8
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
7
9
  function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
8
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
9
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
10
10
  import { isFunction, isObject, isString, isUndefined } from '@wapri/gbase';
11
- import { JSBNative } from "./core";
12
- import { getBridgeWindow } from "./utils";
11
+ import { DEFAULT_JSBRIDGE_CONFIG, JSBNative } from "./core";
12
+ import { getWindowValue, setWindowValue } from "./utils";
13
13
  import { apiRegistry } from "./registers";
14
14
  export { BridgeError } from "./errors";
15
+ export { DEFAULT_JSBRIDGE_CONFIG } from "./core";
15
16
  var EVENT_METHODS = new Set(['on', 'once', 'off']);
16
17
  var INTERNAL_METHODS = new Set(['handle', 'ready', 'request', 'callNative', 'onNativeMessage', 'onNativeEvent', 'installNativeCallbacks', 'cb']);
17
18
 
@@ -19,13 +20,21 @@ var INTERNAL_METHODS = new Set(['handle', 'ready', 'request', 'callNative', 'onN
19
20
  * 创建并缓存业务侧唯一的 Bridge 代理。
20
21
  *
21
22
  * 代理会将未注册的属性访问转换为 Native 方法调用,同时屏蔽底层通道与回调实现。
23
+ * Native 通道名(`nativeChannel`)与缓存键(`globalKey`)由业务传入,库内只提供通用默认值。
24
+ *
25
+ * @param config 本次创建的运行时配置,只需传要覆盖的字段。
22
26
  */
23
- var createJSB = function createJSB() {
24
- var bridgeWindow = getBridgeWindow();
25
- if (!isUndefined(bridgeWindow === null || bridgeWindow === void 0 ? void 0 : bridgeWindow.__kaimaiJSBridge)) {
26
- return bridgeWindow.__kaimaiJSBridge;
27
+ export var createJSB = function createJSB() {
28
+ var _config$globalKey;
29
+ var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
30
+ var globalKey = (_config$globalKey = config.globalKey) !== null && _config$globalKey !== void 0 ? _config$globalKey : DEFAULT_JSBRIDGE_CONFIG.globalKey;
31
+ var cached = getWindowValue(globalKey);
32
+ if (!isUndefined(cached)) {
33
+ return cached;
27
34
  }
28
- var nativeBridge = new JSBNative();
35
+ var nativeBridge = new JSBNative(_objectSpread(_objectSpread({}, config), {}, {
36
+ globalKey: globalKey
37
+ }));
29
38
  var bridge = new Proxy(nativeBridge, {
30
39
  /** 根据属性名分发事件订阅、销毁方法或 Native 业务方法。 */get: function get(target, property) {
31
40
  if (!isString(property)) {
@@ -55,8 +64,8 @@ var createJSB = function createJSB() {
55
64
  if (!isObject(params)) {
56
65
  return Promise.reject(new Error('jsbridge 参数必须是对象'));
57
66
  }
58
- var config = apiRegistry === null || apiRegistry === void 0 ? void 0 : apiRegistry[property];
59
- var _ref = config || {},
67
+ var apiConfig = apiRegistry === null || apiRegistry === void 0 ? void 0 : apiRegistry[property];
68
+ var _ref = apiConfig || {},
60
69
  nativeMethod = _ref.nativeMethod,
61
70
  _ref$initialParams = _ref.initialParams,
62
71
  initialParams = _ref$initialParams === void 0 ? {} : _ref$initialParams,
@@ -66,12 +75,10 @@ var createJSB = function createJSB() {
66
75
  };
67
76
  }
68
77
  });
69
- if (!isUndefined(bridgeWindow)) {
70
- bridgeWindow.__kaimaiJSBridge = bridge;
71
- }
78
+ setWindowValue(globalKey, bridge);
72
79
  return bridge;
73
80
  };
74
81
 
75
- /** 业务侧唯一入口:使用 jsbridge.<具体 Native 方法名>(params)。 */
82
+ /** 业务侧默认入口:使用 jsbridge.<具体 Native 方法名>(params)。 */
76
83
  export var jsbridge = createJSB();
77
84
  export default jsbridge;
@@ -8,5 +8,7 @@ export type BridgeApiConfig = {
8
8
  };
9
9
  /**
10
10
  * 对业务暴露api转换为调用原生的api
11
+ *
12
+ * `initialParams` 只放与业务无关的默认值,业务参数由调用方传入。
11
13
  * */
12
14
  export declare const apiRegistry: Partial<Record<keyof BridgeRegister, BridgeApiConfig>>;
@@ -1,14 +1,13 @@
1
1
  /** 单个业务 Bridge 方法的 Native 调用配置。 */
2
2
 
3
- /**
4
- * 对业务暴露api转换为调用原生的api
3
+ /**
4
+ * 对业务暴露api转换为调用原生的api
5
+ *
6
+ * `initialParams` 只放与业务无关的默认值,业务参数由调用方传入。
5
7
  * */
6
8
  export var apiRegistry = {
7
9
  canIUse: {
8
- nativeMethod: 'permission.caniuse',
9
- initialParams: {
10
- userType: 'kaimai'
11
- }
10
+ nativeMethod: 'permission.caniuse'
12
11
  },
13
12
  getSafeArea: {
14
13
  nativeMethod: 'getSafeArea'
@@ -2,6 +2,10 @@ import { BridgeRegister } from './register';
2
2
  export interface JSBridgeConfig {
3
3
  /** 是否在控制台打印 JS 与 Native 通信的原始数据。 */
4
4
  debug: boolean;
5
+ /** Android / 鸿蒙注入到 window 上的 Native 通道名,默认 `JSBridge`。 */
6
+ nativeChannel: string;
7
+ /** 代理实例缓存在 window 上的键名,默认 `__sdkJSBridge`。 */
8
+ globalKey: string;
5
9
  }
6
10
  export interface NativeRequest<TParams = Record<string, unknown>> {
7
11
  method: string;
@@ -31,11 +35,9 @@ export interface JSBridge extends BridgeRegister {
31
35
  destroy(): void;
32
36
  }
33
37
  export interface BridgeWindow extends Window {
34
- KMBridge?: NativeBridge;
35
38
  webkit?: {
36
39
  messageHandlers?: Record<string, IOSBridgeHandler | undefined>;
37
40
  };
38
41
  onNativeMessage?: (message: string) => void;
39
42
  onNativeEvent?: (eventName: string, data: unknown) => void;
40
- __kaimaiJSBridge?: JSBridge;
41
43
  }
@@ -1,3 +1,13 @@
1
- import type { BridgeWindow } from '../types/bridge';
1
+ import type { BridgeWindow, NativeBridge } from '../types/bridge';
2
2
  /** 获取当前浏览器环境的 window;非浏览器环境返回 undefined。 */
3
3
  export declare function getBridgeWindow(): BridgeWindow | undefined;
4
+ /**
5
+ * 读取注入在 window 上的 Native 通道。
6
+ *
7
+ * 通道名由业务通过 `createJSB({ nativeChannel })` 指定,因此按索引动态取值。
8
+ */
9
+ export declare function getNativeChannel(channel: string): NativeBridge | undefined;
10
+ /** 从 window 上读取缓存值,用于复用 Bridge 代理实例。 */
11
+ export declare function getWindowValue<T>(key: string): T | undefined;
12
+ /** 向 window 写入缓存值,用于复用 Bridge 代理实例。 */
13
+ export declare function setWindowValue(key: string, value: unknown): void;
@@ -3,4 +3,36 @@ import { isUndefined } from '@wapri/gbase';
3
3
  export function getBridgeWindow() {
4
4
  var bridgeWindow = globalThis.window;
5
5
  return isUndefined(bridgeWindow) ? undefined : bridgeWindow;
6
+ }
7
+
8
+ /**
9
+ * 读取注入在 window 上的 Native 通道。
10
+ *
11
+ * 通道名由业务通过 `createJSB({ nativeChannel })` 指定,因此按索引动态取值。
12
+ */
13
+ export function getNativeChannel(channel) {
14
+ var bridgeWindow = getBridgeWindow();
15
+ if (isUndefined(bridgeWindow)) {
16
+ return undefined;
17
+ }
18
+ return bridgeWindow[channel];
19
+ }
20
+
21
+ /** 从 window 上读取缓存值,用于复用 Bridge 代理实例。 */
22
+ export function getWindowValue(key) {
23
+ var bridgeWindow = getBridgeWindow();
24
+ if (isUndefined(bridgeWindow)) {
25
+ return undefined;
26
+ }
27
+ return bridgeWindow[key];
28
+ }
29
+
30
+ /** 向 window 写入缓存值,用于复用 Bridge 代理实例。 */
31
+ export function setWindowValue(key, value) {
32
+ var bridgeWindow = getBridgeWindow();
33
+ if (isUndefined(bridgeWindow)) {
34
+ return;
35
+ }
36
+ ;
37
+ bridgeWindow[key] = value;
6
38
  }
package/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "@wapri/gjsbridge",
3
- "version": "3.0.0",
4
- "description": "麦仓 App WebView JSBridge",
3
+ "version": "5.0.1",
4
+ "description": "App WebView JSBridge 封装",
5
5
  "keywords": [
6
- "jsbridge",
7
- "ios",
8
6
  "android",
9
- "harmonyos"
7
+ "harmonyos",
8
+ "ios",
9
+ "jsbridge"
10
+ ],
11
+ "license": "MIT",
12
+ "author": "wapri contributors",
13
+ "files": [
14
+ "dist"
10
15
  ],
11
- "license": "UNLICENSED",
12
- "author": "wa",
13
- "sideEffects": true,
14
16
  "type": "module",
17
+ "sideEffects": true,
15
18
  "module": "dist/index.js",
16
19
  "types": "dist/index.d.ts",
17
20
  "exports": {
@@ -21,25 +24,22 @@
21
24
  "default": "./dist/index.js"
22
25
  }
23
26
  },
24
- "files": [
25
- "dist"
26
- ],
27
- "dependencies": {
28
- "@wapri/gbase": "^1.0.0"
29
- },
30
- "devDependencies": {
31
- "@wapri/gbase": "^1.0.0"
32
- },
33
27
  "publishConfig": {
34
28
  "access": "public",
35
29
  "registry": "https://registry.npmjs.org/"
36
30
  },
37
- "peerDependenciesMeta": {},
31
+ "dependencies": {
32
+ "@wapri/gbase": "^2.0.1"
33
+ },
34
+ "devDependencies": {
35
+ "@wapri/gbase": "^2.0.1"
36
+ },
38
37
  "peerDependencies": {
39
- "@wapri/gbase": "*"
38
+ "@wapri/gbase": "2.0.1"
40
39
  },
40
+ "peerDependenciesMeta": {},
41
41
  "scripts": {
42
- "build": "father build",
42
+ "build": "node ../../scripts/clean-dist.mjs && father build",
43
43
  "dev": "father dev"
44
44
  }
45
45
  }