@hylid/call 3.2.0-alpha.0 → 3.2.0-alpha.10

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/lib/mpCall.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  import { MPApi, PickMPArgs, Callback, PickMpReturns } from '@hylid/types';
2
- export declare function mpCall<T extends keyof MPApi>(apiName: T | ({} & string), options?: PickMPArgs<T> | Callback<PickMpReturns<T>>, config?: {
3
- type?: string;
4
- }): any;
2
+ import { JsApiConfig } from './types';
3
+ export declare function mpCall<T extends keyof MPApi>(apiName: T | ({} & string), options?: PickMPArgs<T> | Callback<PickMpReturns<T>>, config?: JsApiConfig): any;
@@ -1,3 +1,3 @@
1
1
  import { Message, MessageHandler } from './types';
2
2
  export declare function mpWebOnMessage(cb: MessageHandler): () => void;
3
- export declare function mpWebPostMessage(message: Message): void;
3
+ export declare function mpWebPostMessage(message: Message): any;
@@ -1,75 +1,30 @@
1
- var __rest = this && this.__rest || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
4
- if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
5
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
6
- }
7
- return t;
8
- };
9
- /** 加载 appx 脚本 */
10
- var loadAppxBridge = function loadAppxBridge(cb) {
11
- var _a;
12
- try {
13
- // umd 包用的 IFF 模式,会立即向 window.my 上注入 jsapi,所以不能使用 window.my 判断是否已经加载了 appx 脚本
14
- // umd 版本和 modules 版本不能混用
15
- if ((_a = window.my) === null || _a === void 0 ? void 0 : _a.__hy_mounted__) {
16
- cb && cb("ready" /* BridgeReady.READY */);
17
- return;
18
- }
19
- var hyMy_1 = window.my || {};
20
- var script_1 = document.createElement('script');
21
- script_1.src = 'https://appx/web-view.min.js';
22
- script_1.onload = function onload() {
23
- var readyState = script_1['readyState'];
24
- if (typeof readyState === 'undefined' || /^(loaded|complete)$/.test(readyState)) {
25
- script_1.onload = null;
26
- // appx 脚本覆盖了 window 的 my 属性,这里再把 hylid-bridge 的 API 挂上去
27
- var postMessage_1 = hyMy_1.postMessage,
28
- api = __rest(hyMy_1, ["postMessage"]);
29
- Object.assign(window.my, api, {
30
- __hy_mounted__: true
31
- });
32
- cb && cb("loaded" /* BridgeReady.LOADED */);
33
- }
34
- };
35
- var head = document.getElementsByTagName('head')[0] || document.body;
36
- head.appendChild(script_1);
37
- } catch (e) {
38
- cb && cb(null);
39
- }
40
- };
1
+ import { alipayJSBridge } from "./webCall";
41
2
  var isListening = false;
42
- var WAITING_QUEUE = [];
43
3
  var MESSAGE_HANDLER = [];
44
4
  export function mpWebOnMessage(cb) {
45
5
  MESSAGE_HANDLER.push(cb);
46
6
  return function () {
47
- var index = MESSAGE_HANDLER.indexOf(cb);
48
- if (index > -1) {
49
- MESSAGE_HANDLER.splice(index, 1);
50
- }
7
+ MESSAGE_HANDLER = MESSAGE_HANDLER.filter(function (o) {
8
+ return o !== cb;
9
+ });
51
10
  };
52
11
  }
53
12
  export function mpWebPostMessage(message) {
54
- return loadAppxBridge(function (bridgeReady) {
55
- if (bridgeReady) {
56
- if (!isListening) {
57
- var messageHandler_1 = window.my.onMessage;
58
- window.my.onMessage = function (message) {
59
- MESSAGE_HANDLER.forEach(function (fn) {
60
- return fn(message);
61
- });
62
- if (messageHandler_1) messageHandler_1(message);
63
- };
64
- isListening = true;
65
- }
66
- if (WAITING_QUEUE.length) {
67
- WAITING_QUEUE.forEach(window.my.postMessage);
68
- WAITING_QUEUE = [];
69
- }
70
- window.my.postMessage(message);
71
- } else {
72
- WAITING_QUEUE.push(message);
73
- }
13
+ if (!isListening) {
14
+ document.addEventListener('onToWebViewMessage', function (e) {
15
+ var res = e.data.res;
16
+ if (!res || res.type !== 'message') return;
17
+ var data = JSON.parse(res.data);
18
+ MESSAGE_HANDLER.forEach(function (fn) {
19
+ return fn(data);
20
+ });
21
+ });
22
+ isListening = true;
23
+ }
24
+ return alipayJSBridge(function () {
25
+ window.AlipayJSBridge.call('postWebViewMessage', {
26
+ type: 'message',
27
+ detail: message
28
+ });
74
29
  });
75
30
  }
@@ -1,5 +1,11 @@
1
- import { PickMPArgs, PickMpReturns, MPApi } from '@hylid/types';
2
- import { MpWebJsApiConfig } from './types';
3
- declare type Callback<T> = (result?: T) => void;
4
- export declare const mpWebCall: <T extends keyof MPApi>(api: ({} & string) | T, options?: PickMPArgs<T> | Callback<ReturnType<MPApi[T]>> | undefined, config?: MpWebJsApiConfig) => void;
5
- export {};
1
+ import { MPApi } from '@hylid/types';
2
+ import { JsApiConfig, MpWebJsApiOptions } from './types';
3
+ import { mpWebOnMessage, mpWebPostMessage } from './mpWebBridge';
4
+ export declare function mpWebCall<T extends keyof MPApi>(api: T | ({} & string), options?: MpWebJsApiOptions<T>, config?: JsApiConfig): void;
5
+ export declare namespace mpWebCall {
6
+ var onMessage: typeof mpWebOnMessage;
7
+ var postMessage: typeof mpWebPostMessage;
8
+ var broadcastGlobalData: (key: string, result: any) => void;
9
+ var onReceiveGlobalData: (key: string, callback: (data: any, error: any, form: string) => void) => () => void;
10
+ var onPageEvent: (event: ({} & string) | "onShow" | "onHide" | "onTitleClick" | "onOptionMenuClick" | "onTabItemTap", callback: (data: any, err?: any) => void) => () => void;
11
+ }
package/lib/mpWebCall.js CHANGED
@@ -1,118 +1,111 @@
1
- var loadAppxBridge = function loadAppxBridge(cb) {
1
+ import { mpWebOnMessage, mpWebPostMessage } from "./mpWebBridge";
2
+ // 关联 postMessage 和 onMessage,根据 serialId 执行回调
3
+ var callMap = {};
4
+ // 注册 jsapi 的 message 处理器
5
+ mpWebOnMessage(function (msg) {
2
6
  var _a;
3
- try {
4
- // umd 包用的 IFF 模式,会立即向 window.my 上注入 jsapi,所以不能使用 window.my 判断是否已经加载了 appx 脚本
5
- // umd 版本和 modules 版本不能混用
6
- if ((_a = window.my) === null || _a === void 0 ? void 0 : _a.__hy_mounted__) {
7
- cb && cb("ready" /* BridgeReady.READY */);
8
- return;
9
- }
10
- var hyMy_1 = window.my || {};
11
- var script_1 = document.createElement('script');
12
- script_1.src = 'https://appx/web-view.min.js';
13
- script_1.onload = function onload() {
14
- var readyState = script_1['readyState'];
15
- if (typeof readyState === 'undefined' || /^(loaded|complete)$/.test(readyState)) {
16
- script_1.onload = null;
17
- Object.assign(window.my, hyMy_1, {
18
- __hy_mounted__: true
19
- });
20
- cb && cb("loaded" /* BridgeReady.LOADED */);
21
- }
22
- };
23
- var head = document.getElementsByTagName('head')[0] || document.body;
24
- head.appendChild(script_1);
25
- } catch (e) {
26
- cb && cb(null);
7
+ var message = msg;
8
+ var serialId = message.serialId;
9
+ // 不是 jsapi 的调用的消息
10
+ if (message.type && message.type !== 'apiCall') return;
11
+ if (!serialId) return;
12
+ if (!callMap[serialId]) return;
13
+ var callback = callMap[serialId];
14
+ var data = message.result;
15
+ var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
16
+ if (type === 'callback') {
17
+ var _b = data || {},
18
+ _data_1 = _b._data_,
19
+ _type_1 = _b._type_;
20
+ if (_type_1 === 'success') return callback(_data_1);
21
+ return callback(undefined, _data_1);
27
22
  }
28
- };
29
- var callbackFn = function callbackFn(res, callback) {
30
- var _a = callback || {},
31
- success = _a.success,
32
- fail = _a.fail,
33
- complete = _a.complete;
34
- var _b = res || {},
35
- _data_ = _b._data_,
36
- _type_ = _b._type_;
23
+ var _c = callback || {},
24
+ success = _c.success,
25
+ fail = _c.fail,
26
+ complete = _c.complete;
27
+ var _d = data || {},
28
+ _data_ = _d._data_,
29
+ _type_ = _d._type_;
37
30
  // 新版本协议
38
31
  if (_type_) {
39
32
  if (_type_ === 'success') success === null || success === void 0 ? void 0 : success(_data_);
40
33
  if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
41
- return complete === null || complete === void 0 ? void 0 : complete(_data_);
42
- }
43
- // 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
44
- // @ts-ignore
45
- if (res === null || res === void 0 ? void 0 : res.error) {
46
- fail === null || fail === void 0 ? void 0 : fail(res);
34
+ complete === null || complete === void 0 ? void 0 : complete(_data_);
47
35
  } else {
48
- success === null || success === void 0 ? void 0 : success(res);
49
- }
50
- complete === null || complete === void 0 ? void 0 : complete(res);
51
- };
52
- export var mpWebCall = function () {
53
- var callMap = {};
54
- var WAITING_QUEUE = [];
55
- var isListening = false;
56
- function onMessage() {
57
- var messageHandler = window.my.onMessage;
58
- window.my.onMessage = function (message) {
59
- var _a;
60
- var serialId = message.serialId;
61
- var data = message.result;
62
- var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type; // JsApi 的类型
63
- if (serialId) {
64
- var callback = callMap[serialId];
65
- if (!callback) return;
66
- if (type === 'callback') {
67
- callback(data);
68
- } else {
69
- callbackFn(data, callback);
70
- delete callMap[serialId];
71
- }
72
- } else if (messageHandler) {
73
- messageHandler(message);
74
- }
75
- };
76
- }
77
- function fireMessage(_a) {
78
- var api = _a.api,
79
- options = _a.options,
80
- config = _a.config;
81
- var random = Math.floor(Math.random() * 1000000);
82
- var serialId = api + '_' + random;
83
- if (options) {
84
- callMap[serialId] = options;
36
+ // 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
37
+ // @ts-ignore
38
+ if (data === null || data === void 0 ? void 0 : data.error) {
39
+ fail === null || fail === void 0 ? void 0 : fail(data);
40
+ } else {
41
+ success === null || success === void 0 ? void 0 : success(data);
85
42
  }
86
- var params = {
87
- source: 'hylid',
88
- type: 'apiCall',
89
- api: api,
90
- serialId: serialId,
91
- options: options,
92
- config: config
93
- };
94
- window.my.postMessage(params);
43
+ complete === null || complete === void 0 ? void 0 : complete(data);
44
+ }
45
+ delete callMap[serialId];
46
+ });
47
+ export function mpWebCall(api, options, config) {
48
+ var random = Math.floor(Math.random() * 1000000);
49
+ var serialId = api + '_' + random;
50
+ // 只调用,没有参数,不需要接收 jsapi 执行结果
51
+ if (options) {
52
+ callMap[serialId] = options;
95
53
  }
96
- return function call(api, options, config) {
97
- loadAppxBridge(function (bridgeReady) {
98
- var params = {
99
- api: api,
100
- options: options,
101
- config: config
102
- };
103
- if (bridgeReady) {
104
- if (!isListening) {
105
- onMessage();
106
- isListening = true;
107
- }
108
- if (WAITING_QUEUE.length) {
109
- WAITING_QUEUE.forEach(fireMessage);
110
- WAITING_QUEUE = [];
111
- }
112
- fireMessage(params);
113
- } else {
114
- WAITING_QUEUE.push(params);
115
- }
116
- });
54
+ var message = {
55
+ source: 'hylid',
56
+ type: 'apiCall',
57
+ api: api,
58
+ serialId: serialId,
59
+ options: options,
60
+ config: config
61
+ };
62
+ mpWebPostMessage(message);
63
+ }
64
+ function broadcastGlobalData(key, result) {
65
+ var message = {
66
+ source: 'hylid',
67
+ type: 'broadcast',
68
+ from: window.location.href,
69
+ key: key,
70
+ result: result
117
71
  };
118
- }();
72
+ mpWebPostMessage(message);
73
+ }
74
+ function onReceiveGlobalData(key, callback) {
75
+ return mpWebOnMessage(function (msg) {
76
+ var message = msg;
77
+ if (message.source !== 'hylid') return;
78
+ if (message.type !== 'broadcast') return;
79
+ // 不是接收的目标消息
80
+ if (message.key !== key) return;
81
+ var _a = message.result || {},
82
+ _data_ = _a._data_,
83
+ _type_ = _a._type_;
84
+ if (_type_ === 'success') return callback(_data_, undefined, message.from);
85
+ return callback(undefined, _data_, message.from);
86
+ });
87
+ }
88
+ function onPageEvent(event, callback) {
89
+ // 告诉小程序侧,监听 event 事件
90
+ mpWebPostMessage({
91
+ source: 'hylid',
92
+ type: 'pageEvent',
93
+ event: event
94
+ });
95
+ return mpWebOnMessage(function (msg) {
96
+ var message = msg;
97
+ if (message.source !== 'hylid') return;
98
+ if (message.type !== 'pageEvent') return;
99
+ if (message.event !== event) return;
100
+ var _a = message.result || {},
101
+ _data_ = _a._data_,
102
+ _type_ = _a._type_;
103
+ if (_type_ === 'success') return callback(_data_);
104
+ return callback(undefined, _data_);
105
+ });
106
+ }
107
+ mpWebCall.onMessage = mpWebOnMessage;
108
+ mpWebCall.postMessage = mpWebPostMessage;
109
+ mpWebCall.broadcastGlobalData = broadcastGlobalData;
110
+ mpWebCall.onReceiveGlobalData = onReceiveGlobalData;
111
+ mpWebCall.onPageEvent = onPageEvent;
package/lib/notFound.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createError } from "./utils";
1
2
  export var notFound = function notFound(name, opt, config) {
2
3
  var _a;
3
4
  if (config === void 0) {
@@ -5,10 +6,7 @@ export var notFound = function notFound(name, opt, config) {
5
6
  }
6
7
  var _b = config.type,
7
8
  type = _b === void 0 ? 'async' : _b;
8
- var response = {
9
- error: -1,
10
- errorMessage: "".concat(name, " is not found")
11
- };
9
+ var response = createError('NOTFOUND', name);
12
10
  if (type === 'sync') return response;
13
11
  if (type === 'callback') {
14
12
  opt === null || opt === void 0 ? void 0 : opt(response);
package/lib/types.d.ts CHANGED
@@ -1,20 +1,51 @@
1
- import { JsApiType, MPApi } from '@hylid/types';
1
+ import { JsApiType, MPApi, PickMPArgs, PickMpReturns } from '@hylid/types';
2
2
  export declare type JsApi = keyof MPApi;
3
- export interface MpWebJsApiConfig {
3
+ export interface JsApiConfig {
4
4
  type?: JsApiType;
5
5
  }
6
- export declare type Config = MpWebJsApiConfig;
7
- export interface MpWebMessageResult {
6
+ export declare type Config = JsApiConfig;
7
+ export interface MpWebMessageResult<T = any> {
8
8
  _type_: 'success' | 'fail';
9
- _data_: any;
9
+ _data_: T;
10
10
  }
11
- export interface MpWebMessage {
11
+ export interface MpWebJsApiMessage {
12
12
  source: 'hylid';
13
13
  type: 'apiCall';
14
14
  api: string;
15
15
  serialId: string;
16
16
  options: any;
17
- config?: MpWebJsApiConfig;
17
+ config?: JsApiConfig;
18
18
  result?: MpWebMessageResult;
19
19
  }
20
- export declare type JsApiInfo = Pick<MpWebMessage, 'api' | 'options' | 'config'>;
20
+ export interface MpWebBroadcastMessage {
21
+ source: 'hylid';
22
+ type: 'broadcast';
23
+ from: string;
24
+ key: string;
25
+ result?: MpWebMessageResult;
26
+ }
27
+ export interface MpWebPageEventMessage {
28
+ source: 'hylid';
29
+ type: 'pageEvent';
30
+ event: 'onShow' | 'onHide' | 'onTitleClick' | 'onOptionMenuClick' | 'onTabItemTap' | ({} & string);
31
+ result?: MpWebMessageResult;
32
+ }
33
+ export declare type Message = MpWebJsApiMessage | MpWebBroadcastMessage | MpWebPageEventMessage;
34
+ export declare type MessageHandler = (message: Message) => void;
35
+ declare type Callback<T> = (result?: T) => void;
36
+ export declare type MpWebJsApiOptions<T extends keyof MPApi> = PickMPArgs<T> | Callback<PickMpReturns<T>>;
37
+ export interface WebviewBridgeConfig {
38
+ /** 允许调用 jsApi 的白名单 */
39
+ whitelist?: string[];
40
+ /** 禁止调用 jsApi 的黑名单 */
41
+ blacklist?: string[];
42
+ /** 自定义 jsApi [ 优先级比较高,可以覆盖原生的 jsApi ] */
43
+ customApi?: CustomApi;
44
+ }
45
+ export interface CustomApi {
46
+ [key: string]: (params: {
47
+ success(v: any): void;
48
+ fail(v: any): void;
49
+ }) => void;
50
+ }
51
+ export {};
package/lib/utils.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { MpWebMessageResult } from './types';
2
+ declare const ERROR: {
3
+ NOTFOUND: {
4
+ code: number;
5
+ message: (name: string) => string;
6
+ };
7
+ NOTALLOWED: {
8
+ code: number;
9
+ message: (name: string) => string;
10
+ };
11
+ INVALID: {
12
+ code: number;
13
+ message: (name: string) => string;
14
+ };
15
+ NOTBIND: {
16
+ code: number;
17
+ message: (name: string) => string;
18
+ };
19
+ PARAMS_ERROR: {
20
+ code: number;
21
+ message: (name: string) => string;
22
+ };
23
+ };
24
+ export declare const createError: (type: keyof typeof ERROR, name?: string) => {
25
+ errorCode: number;
26
+ errorMessage: string;
27
+ errorSouce: string;
28
+ };
29
+ export declare function createMessage(message: any, type?: 'success' | 'fail'): MpWebMessageResult;
30
+ export {};
package/lib/utils.js ADDED
@@ -0,0 +1,54 @@
1
+ var ERROR = {
2
+ NOTFOUND: {
3
+ code: -1,
4
+ message: function message(name) {
5
+ return "".concat(name, " is not found");
6
+ }
7
+ },
8
+ NOTALLOWED: {
9
+ code: -2,
10
+ message: function message(name) {
11
+ return "".concat(name, " is not allowed");
12
+ }
13
+ },
14
+ INVALID: {
15
+ code: -3,
16
+ message: function message(name) {
17
+ return "".concat(name, " is invalid");
18
+ }
19
+ },
20
+ NOTBIND: {
21
+ code: -4,
22
+ message: function message(name) {
23
+ return "".concat(name, " exec fail, webviewBridge not bind context");
24
+ }
25
+ },
26
+ PARAMS_ERROR: {
27
+ code: -5,
28
+ message: function message(name) {
29
+ return "".concat(name, " exec fail, params error");
30
+ }
31
+ }
32
+ };
33
+ export var createError = function createError(type, name) {
34
+ if (name === void 0) {
35
+ name = '';
36
+ }
37
+ var _a = ERROR[type],
38
+ code = _a.code,
39
+ message = _a.message;
40
+ return {
41
+ errorCode: code,
42
+ errorMessage: message(name),
43
+ errorSouce: 'hylid'
44
+ };
45
+ };
46
+ export function createMessage(message, type) {
47
+ if (type === void 0) {
48
+ type = 'success';
49
+ }
50
+ return {
51
+ _type_: type,
52
+ _data_: message
53
+ };
54
+ }
package/lib/webCall.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { AliJsApi } from '@hylid/types';
2
- export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: {
3
- type?: string;
4
- }): void;
2
+ import { JsApiConfig } from './types';
3
+ export declare const alipayJSBridge: (cb: Function) => any;
4
+ export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: JsApiConfig): void;
package/lib/webCall.js CHANGED
@@ -1,11 +1,11 @@
1
- var alipayJSBridgeReady = function alipayJSBridgeReady(cb) {
1
+ export var alipayJSBridge = function alipayJSBridge(cb) {
2
2
  if (window.AlipayJSBridge) return cb();
3
3
  document.addEventListener('AlipayJSBridgeReady', function () {
4
4
  return cb();
5
5
  }, false);
6
6
  };
7
7
  export function webCall(apiName, options, config) {
8
- alipayJSBridgeReady(function () {
8
+ alipayJSBridge(function () {
9
9
  var _a = (config || {}).type,
10
10
  type = _a === void 0 ? 'async' : _a;
11
11
  if (type === 'async') {
@@ -0,0 +1,26 @@
1
+ import { Message, WebviewBridgeConfig } from '../types';
2
+ interface WebViewContext {
3
+ postMessage(message: Message): void;
4
+ }
5
+ export declare class WebViewBridge {
6
+ private webview;
7
+ private config;
8
+ private context;
9
+ private MESSAGE_HANDLER;
10
+ /**
11
+ *
12
+ * @param webviewId webview 组件的 id,可传字符串,或者 my.createWebViewContext 创建出的实例
13
+ * @param config Config 配置
14
+ * @param context 页面上下文,传 this 即可。传入后,H5 侧可以使用广播、可以监听页面事件
15
+ */
16
+ constructor(webviewId: string | WebViewContext, config?: WebviewBridgeConfig, context?: any);
17
+ listen: (data: {
18
+ detail: Message;
19
+ }) => void;
20
+ private jsApiHandler;
21
+ private broadcastHandler;
22
+ private pageEventHandler;
23
+ private bindCtxEvent;
24
+ private sendMessage;
25
+ }
26
+ export {};