@hylid/call 4.0.0-alpha.7 → 4.0.0-alpha.9

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) {
@@ -26,5 +26,5 @@ export function createWebviewSrc(url) {
26
26
  }
27
27
  ;
28
28
  export { WebViewBridge } from "./webviewBridge";
29
- export { mpCall, mpWebCall, webCall, notFound };
29
+ export { mpCall, mpWebCall, webCall, notFound, mpWebCallAsync, webCallAsync };
30
30
  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,113 @@ 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 serialId = message.serialId;
142
+ var data = message.result;
143
+ if (serialId) {
144
+ var callback = callMap[serialId];
145
+ callbackFn(data, callback);
146
+ delete callMap[serialId];
147
+ }
148
+ };
149
+ // 代码参考:https://alex.alipay.com/antcode/qianyu.fzy/af-appx/blob/r20191223-1.24.0/src/web-view/embed/index.tsx
150
+ document.addEventListener('onToWebViewMessage', function (e) {
151
+ var res = e.data.res;
152
+ if (res.type === 'message') {
153
+ messageHandler(JSON.parse(res.data));
154
+ }
155
+ // TODO: 参考的代码中还有 response 的类型,需要考虑一下是否实现,对应 api 为 'navigateTo','navigateBack','switchTab','reLaunch','redirectTo',
156
+ });
157
+ }
158
+ function fireMessage(_a) {
159
+ var _b;
160
+ var apiName = _a.apiName,
161
+ options = _a.options,
162
+ isSync = _a.isSync;
163
+ var env = window;
164
+ var random = Math.floor(Math.random() * 1000000);
165
+ var serialId = "".concat(apiName, "_").concat(random);
166
+ if (options) {
167
+ callMap[serialId] = options;
168
+ }
169
+ var params = {
170
+ type: 'apiCall',
171
+ api: apiName,
172
+ serialId: serialId,
173
+ options: options,
174
+ isSync: isSync,
175
+ source: 'hylid'
176
+ };
177
+ (_b = env.AlipayJSBridge) === null || _b === void 0 ? void 0 : _b.call('postWebViewMessage', {
178
+ type: 'message',
179
+ detail: params
180
+ });
181
+ }
182
+ return function mpWebCall(apiName, options, config) {
183
+ return new Promise(function (resolve, reject) {
184
+ var _a = options || {},
185
+ _success = _a.success,
186
+ _fail = _a.fail;
187
+ var _b = config || {},
188
+ _c = _b.timeout,
189
+ timeout = _c === void 0 ? 20000 : _c,
190
+ isSync = _b.isSync;
191
+ var timer;
192
+ if (timeout > 0) {
193
+ timer = setTimeout(function () {
194
+ var err = {
195
+ error: -101,
196
+ errorMessage: "[".concat(apiName, "]: Timeout")
197
+ };
198
+ reject(err);
199
+ _fail === null || _fail === void 0 ? void 0 : _fail(err);
200
+ }, timeout);
201
+ }
202
+ var params = {
203
+ apiName: apiName,
204
+ options: __assign(__assign({}, options), {
205
+ success: function success(res) {
206
+ if (timer) {
207
+ clearTimeout(timer);
208
+ }
209
+ resolve(res);
210
+ _success === null || _success === void 0 ? void 0 : _success(res);
211
+ },
212
+ fail: function fail(err) {
213
+ if (timer) {
214
+ clearTimeout(timer);
215
+ }
216
+ reject(err);
217
+ _fail === null || _fail === void 0 ? void 0 : _fail(err);
218
+ }
219
+ }),
220
+ isSync: isSync
221
+ };
222
+ alipayJSBridge(function () {
223
+ if (!isListening) {
224
+ onMessage();
225
+ isListening = true;
226
+ }
227
+ fireMessage(params);
228
+ });
229
+ });
230
+ };
231
+ }();
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,8 @@
1
1
  import { AliJsApi } from '@hylid/types';
2
2
  import { JsApiConfig } from './types';
3
+ import { AsyncCallback } from '@hylid/types';
3
4
  export declare const alipayJSBridge: (cb: Function) => any;
4
5
  export declare function webCall<T extends keyof AliJsApi.Api>(apiName: T | ({} & string), options?: any, config?: JsApiConfig): void;
6
+ export declare function webCallAsync<T = any>(apiName: string, options?: AsyncCallback<T> & any, config?: {
7
+ timeout?: number;
8
+ }): 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.7",
3
+ "version": "4.0.0-alpha.9",
4
4
  "main": "lib/index.js",
5
5
  "files": [
6
6
  "lib"
7
7
  ],
8
8
  "dependencies": {
9
- "@hylid/env": "^4.0.0-alpha.7",
10
- "@hylid/types": "^4.0.0-alpha.7"
9
+ "@hylid/env": "^4.0.0-alpha.9",
10
+ "@hylid/types": "^4.0.0-alpha.9"
11
11
  },
12
12
  "publishConfig": {
13
13
  "access": "public"