@hylid/call 4.0.0-alpha.2 → 4.0.0-alpha.21

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/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /// <reference types="miniprogram" />
2
2
  import { MPApi, PickMPArgs, PickMpReturns, Callback } from '@hylid/types';
3
3
  import { mpCall } from './mpCall';
4
- import { mpWebCall } from './mpWebCall';
5
- import { webCall } from './webCall';
4
+ import { mpWebCall, mpWebCallAsync } from './mpWebCall';
5
+ import { webCall, webCallAsync } from './webCall';
6
6
  import { notFound } from './notFound';
7
7
  import { Config } from './types';
8
8
  export declare function call<T extends keyof MPApi>(name: T | ({} & string), options?: PickMPArgs<T> | Common | Callback<PickMpReturns<T>>, config?: Config): any;
9
9
  export declare function createWebviewSrc(url: string): string;
10
10
  export { WebViewBridge } from './webviewBridge';
11
- export { mpCall, mpWebCall, webCall, notFound };
11
+ export { mpCall, mpWebCall, webCall, notFound, mpWebCallAsync, webCallAsync };
12
12
  export { promisify } from './promisify';
package/lib/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  var _a;
2
2
  import { platform, PLATFORM } from '@hylid/env';
3
3
  import { mpCall } from "./mpCall";
4
- import { mpWebCall } from "./mpWebCall";
5
- import { webCall } from "./webCall";
4
+ import { mpWebCall, mpWebCallAsync } from "./mpWebCall";
5
+ import { webCall, webCallAsync } from "./webCall";
6
6
  import { notFound } from "./notFound";
7
7
  var callMap = (_a = {}, _a[PLATFORM.MP] = mpCall, _a[PLATFORM.MPWEB] = mpWebCall, _a[PLATFORM.WEB] = webCall, _a);
8
8
  export function call(name, options, config) {
@@ -24,7 +24,6 @@ export function createWebviewSrc(url) {
24
24
  var newUrl = url + separator + '__app__=' + app + hash;
25
25
  return newUrl;
26
26
  }
27
- ;
28
27
  export { WebViewBridge } from "./webviewBridge";
29
- export { mpCall, mpWebCall, webCall, notFound };
28
+ export { mpCall, mpWebCall, webCall, notFound, mpWebCallAsync, webCallAsync };
30
29
  export { promisify } from "./promisify";
@@ -9,3 +9,8 @@ export declare namespace mpWebCall {
9
9
  var onReceiveGlobalData: (key: string, callback: (data: any, error: any, form: string) => void) => () => void;
10
10
  var onPageEvent: (event: ({} & string) | "onShow" | "onHide" | "onTitleClick" | "onOptionMenuClick" | "onTabItemTap", callback: (data: any, err?: any) => void) => () => void;
11
11
  }
12
+ export declare const mpWebCallAsync: <T = any>(apiName: string, options?: any, config?: {
13
+ /** 超时时长,默认 20000, 0 表示不判断超时 */
14
+ timeout: number;
15
+ isSync?: boolean;
16
+ }) => Promise<any>;
package/lib/mpWebCall.js CHANGED
@@ -1,4 +1,15 @@
1
+ var __assign = this && this.__assign || function () {
2
+ __assign = Object.assign || function (t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
6
+ }
7
+ return t;
8
+ };
9
+ return __assign.apply(this, arguments);
10
+ };
1
11
  import { mpWebOnMessage, mpWebPostMessage } from "./mpWebBridge";
12
+ import { alipayJSBridge } from "./webCall";
2
13
  // 关联 postMessage 和 onMessage,根据 serialId 执行回调
3
14
  var callMap = {};
4
15
  // 注册 jsapi 的 message 处理器
@@ -108,4 +119,143 @@ mpWebCall.onMessage = mpWebOnMessage;
108
119
  mpWebCall.postMessage = mpWebPostMessage;
109
120
  mpWebCall.broadcastGlobalData = broadcastGlobalData;
110
121
  mpWebCall.onReceiveGlobalData = onReceiveGlobalData;
111
- mpWebCall.onPageEvent = onPageEvent;
122
+ mpWebCall.onPageEvent = onPageEvent;
123
+ var callbackFn = function callbackFn(res, callback) {
124
+ var _a = callback || {},
125
+ success = _a.success,
126
+ fail = _a.fail,
127
+ complete = _a.complete;
128
+ // @ts-ignore
129
+ if (res === null || res === void 0 ? void 0 : res.error) {
130
+ fail === null || fail === void 0 ? void 0 : fail(res);
131
+ } else {
132
+ success === null || success === void 0 ? void 0 : success(res);
133
+ }
134
+ complete === null || complete === void 0 ? void 0 : complete(res);
135
+ };
136
+ export var mpWebCallAsync = function () {
137
+ var callMap = {};
138
+ var isListening = false;
139
+ function onMessage() {
140
+ var messageHandler = function messageHandler(message) {
141
+ var _a;
142
+ var serialId = message.serialId;
143
+ if (!serialId) return;
144
+ if (!callMap[serialId]) return;
145
+ var callback = callMap[serialId];
146
+ var data = message.result;
147
+ var type = (_a = message.config) === null || _a === void 0 ? void 0 : _a.type;
148
+ if (type === 'callback') {
149
+ var _b = data || {},
150
+ _data_2 = _b._data_,
151
+ _type_2 = _b._type_;
152
+ if (_type_2 === 'success') return callback(_data_2);
153
+ return callback(undefined, _data_2);
154
+ }
155
+ var _c = callback || {},
156
+ success = _c.success,
157
+ fail = _c.fail,
158
+ complete = _c.complete;
159
+ var _d = data || {},
160
+ _data_ = _d._data_,
161
+ _type_ = _d._type_;
162
+ // 新版本协议
163
+ if (_type_) {
164
+ if (_type_ === 'success') success === null || success === void 0 ? void 0 : success(_data_);
165
+ if (_type_ === 'fail') fail === null || fail === void 0 ? void 0 : fail(_data_);
166
+ complete === null || complete === void 0 ? void 0 : complete(_data_);
167
+ } else {
168
+ // 下面兼容老版本(老版本消息识别不明确,导致 H5 中走 success/fail 逻辑可能岔了)
169
+ // @ts-ignore
170
+ if (data === null || data === void 0 ? void 0 : data.error) {
171
+ fail === null || fail === void 0 ? void 0 : fail(data);
172
+ } else {
173
+ success === null || success === void 0 ? void 0 : success(data);
174
+ }
175
+ complete === null || complete === void 0 ? void 0 : complete(data);
176
+ }
177
+ delete callMap[serialId];
178
+ };
179
+ // 代码参考:https://alex.alipay.com/antcode/qianyu.fzy/af-appx/blob/r20191223-1.24.0/src/web-view/embed/index.tsx
180
+ document.addEventListener('onToWebViewMessage', function (e) {
181
+ var res = e.data.res;
182
+ if (res.type === 'message') {
183
+ messageHandler(JSON.parse(res.data));
184
+ }
185
+ // TODO: 参考的代码中还有 response 的类型,需要考虑一下是否实现,对应 api 为 'navigateTo','navigateBack','switchTab','reLaunch','redirectTo',
186
+ });
187
+ }
188
+ function fireMessage(_a) {
189
+ var _b;
190
+ var apiName = _a.apiName,
191
+ options = _a.options,
192
+ isSync = _a.isSync;
193
+ var env = window;
194
+ var random = Math.floor(Math.random() * 1000000);
195
+ var serialId = "".concat(apiName, "_").concat(random);
196
+ if (options) {
197
+ callMap[serialId] = options;
198
+ }
199
+ var params = {
200
+ type: 'apiCall',
201
+ api: apiName,
202
+ serialId: serialId,
203
+ options: options,
204
+ isSync: isSync,
205
+ source: 'hylid'
206
+ };
207
+ (_b = env.AlipayJSBridge) === null || _b === void 0 ? void 0 : _b.call('postWebViewMessage', {
208
+ type: 'message',
209
+ detail: params
210
+ });
211
+ }
212
+ return function mpWebCall(apiName, options, config) {
213
+ return new Promise(function (resolve, reject) {
214
+ var _a = options || {},
215
+ _success = _a.success,
216
+ _fail = _a.fail;
217
+ var _b = config || {},
218
+ _c = _b.timeout,
219
+ timeout = _c === void 0 ? 20000 : _c,
220
+ isSync = _b.isSync;
221
+ var timer;
222
+ if (timeout > 0) {
223
+ timer = setTimeout(function () {
224
+ var err = {
225
+ error: -101,
226
+ errorMessage: "[".concat(apiName, "]: Timeout")
227
+ };
228
+ reject(err);
229
+ _fail === null || _fail === void 0 ? void 0 : _fail(err);
230
+ }, timeout);
231
+ }
232
+ var params = {
233
+ apiName: apiName,
234
+ options: __assign(__assign({}, options), {
235
+ success: function success(res) {
236
+ if (timer) {
237
+ clearTimeout(timer);
238
+ }
239
+ resolve(res);
240
+ _success === null || _success === void 0 ? void 0 : _success(res);
241
+ },
242
+ fail: function fail(err) {
243
+ if (timer) {
244
+ clearTimeout(timer);
245
+ }
246
+ reject(err);
247
+ _fail === null || _fail === void 0 ? void 0 : _fail(err);
248
+ }
249
+ }),
250
+ isSync: isSync
251
+ };
252
+ alipayJSBridge(function () {
253
+ if (!isListening) {
254
+ onMessage();
255
+ isListening = true;
256
+ }
257
+ fireMessage(params);
258
+ });
259
+ });
260
+ };
261
+ }();
@@ -1,4 +1,5 @@
1
1
  export interface Options {
2
2
  success?: (res: any) => void;
3
+ fail?: (res: any) => void;
3
4
  }
4
- export declare const promisify: <T extends Options, P = Parameters<Required<Options> & T["success"]>[0]>(fn: (opt: T) => void) => (args?: Omit<T, "success" | "fail" | "complete"> | undefined) => Promise<P>;
5
+ export declare const promisify: <T extends Options, P = Parameters<Required<Options> & T["success"]>[0]>(fn: (opt: T) => void) => (args?: T | undefined) => Promise<P>;
package/lib/promisify.js CHANGED
@@ -12,8 +12,16 @@ export var promisify = function promisify(fn) {
12
12
  return function (args) {
13
13
  return new Promise(function (resolve, reject) {
14
14
  var options = __assign(__assign({}, args), {
15
- success: resolve,
16
- fail: reject
15
+ success: function success(opt) {
16
+ var _a;
17
+ resolve(opt);
18
+ (_a = args === null || args === void 0 ? void 0 : args.success) === null || _a === void 0 ? void 0 : _a.call(args, opt);
19
+ },
20
+ fail: function fail(opt) {
21
+ var _a;
22
+ reject(opt);
23
+ (_a = args === null || args === void 0 ? void 0 : args.fail) === null || _a === void 0 ? void 0 : _a.call(args, opt);
24
+ }
17
25
  });
18
26
  fn(options);
19
27
  });
package/lib/types.d.ts CHANGED
@@ -6,6 +6,10 @@ export interface JsApiConfig {
6
6
  * @deprecated 优先使用 type = "sync" 配置
7
7
  */
8
8
  isSync?: boolean;
9
+ /**
10
+ * timeout 配置,单位 ms
11
+ */
12
+ timeout?: number;
9
13
  }
10
14
  export declare type Config = JsApiConfig;
11
15
  export interface MpWebMessageResult<T = any> {
package/lib/webCall.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- import { AliJsApi } from '@hylid/types';
1
+ import { AliJsApi, AsyncCallback } from '@hylid/types';
2
2
  import { JsApiConfig } from './types';
3
3
  export declare const alipayJSBridge: (cb: Function) => any;
4
4
  export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: JsApiConfig): void;
5
+ export declare function webCallAsync<T = any>(apiName: string, options?: AsyncCallback<T> & any, config?: {
6
+ timeout?: number;
7
+ }): Promise<any>;
package/lib/webCall.js CHANGED
@@ -26,4 +26,42 @@ export function webCall(apiName, options, config) {
26
26
  window.AlipayJSBridge.call(apiName, options);
27
27
  }
28
28
  });
29
+ }
30
+ export function webCallAsync(apiName, options, config) {
31
+ return new Promise(function (resolve, reject) {
32
+ var _a = options || {},
33
+ success = _a.success,
34
+ fail = _a.fail,
35
+ complete = _a.complete;
36
+ var _b = (config || {}).timeout,
37
+ timeout = _b === void 0 ? 20000 : _b;
38
+ var timer;
39
+ if (timeout > 0) {
40
+ timer = setTimeout(function () {
41
+ var err = {
42
+ error: -101,
43
+ errorMessage: "[".concat(apiName, "]: Timeout")
44
+ };
45
+ reject(err);
46
+ fail === null || fail === void 0 ? void 0 : fail(err);
47
+ }, timeout);
48
+ }
49
+ window.AlipayJSBridge.call(apiName, options, function (res) {
50
+ var _a = options || {},
51
+ success = _a.success,
52
+ fail = _a.fail,
53
+ complete = _a.complete;
54
+ if (timer) {
55
+ clearTimeout(timer);
56
+ }
57
+ if (res === null || res === void 0 ? void 0 : res.error) {
58
+ reject(res);
59
+ fail === null || fail === void 0 ? void 0 : fail(res);
60
+ } else {
61
+ resolve(res);
62
+ success === null || success === void 0 ? void 0 : success(res);
63
+ }
64
+ complete === null || complete === void 0 ? void 0 : complete(res);
65
+ });
66
+ });
29
67
  }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@hylid/call",
3
- "version": "4.0.0-alpha.2",
3
+ "version": "4.0.0-alpha.21",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^4.0.0-alpha.2",
10
- "@hylid/types": "^4.0.0-alpha.2"
9
+ "@hylid/env": "^4.0.0-alpha.21",
10
+ "@hylid/types": "^4.0.0-alpha.21"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"