@tarojs/taro-h5 3.5.11 → 3.6.0-beta.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.
@@ -544,7 +544,7 @@ declare function onSocketOpen(): void;
544
544
  declare function onSocketMessage(): void;
545
545
  declare function onSocketError(): void;
546
546
  declare function onSocketClose(): void;
547
- declare function connectSocket(options: any): Promise<unknown>;
547
+ declare function connectSocket(options?: Taro.connectSocket.Option): Promise<unknown>;
548
548
  declare function closeSocket(): void;
549
549
  // 帐号信息
550
550
  declare const getAccountInfoSync: (option?: {}, ...args: any[]) => Promise<Partial<TaroGeneral.CallbackResult> & Record<string, unknown> & TaroGeneral.CallbackResult>;
@@ -679,7 +679,7 @@ declare const disableAlertBeforeUnload: (option?: {}, ...args: any[]) => Promise
679
679
  declare const getMenuButtonBoundingClientRect: (option?: {}, ...args: any[]) => Promise<Partial<TaroGeneral.CallbackResult> & Record<string, unknown> & TaroGeneral.CallbackResult>;
680
680
  // 导航栏
681
681
  declare const showNavigationBarLoading: (option?: {}, ...args: any[]) => Promise<Partial<TaroGeneral.CallbackResult> & Record<string, unknown> & TaroGeneral.CallbackResult>;
682
- declare function setNavigationBarTitle(options: any): Promise<Partial<TaroGeneral.CallbackResult> & Record<string, unknown> & TaroGeneral.CallbackResult>;
682
+ declare function setNavigationBarTitle(options?: Taro.setNavigationBarTitle.Option): Promise<Partial<TaroGeneral.CallbackResult> & Record<string, unknown> & TaroGeneral.CallbackResult>;
683
683
  /**
684
684
  * 设置页面导航条颜色
685
685
  */
@@ -1,18 +1,23 @@
1
1
  declare type TCallback<T = Partial<TaroGeneral.CallbackResult>> = (res: T) => Promise<void> | void;
2
2
  interface IMethodParam<T = Partial<TaroGeneral.CallbackResult>> {
3
3
  name: string;
4
- success?: TCallback<T>;
4
+ success?: TCallback<T & TaroGeneral.CallbackResult>;
5
5
  fail?: TCallback;
6
6
  complete?: TCallback;
7
7
  }
8
+ interface IMockPromise {
9
+ resolve?: typeof Promise.resolve | TFunc;
10
+ reject?: typeof Promise.reject | TFunc;
11
+ }
8
12
  export declare class MethodHandler<T = Partial<TaroGeneral.CallbackResult>> {
9
13
  methodName: string;
10
14
  protected __success?: TCallback<T>;
11
15
  protected __fail?: TCallback;
12
16
  protected __complete?: TCallback;
17
+ protected isHandlerError: boolean;
13
18
  constructor({ name, success, fail, complete }: IMethodParam<T>);
14
- success<U = Record<string, unknown>>(res?: Partial<T> & Partial<TaroGeneral.CallbackResult>, resolve?: any): Promise<T & U & TaroGeneral.CallbackResult>;
15
- fail<U = Record<string, unknown>>(res?: Partial<T> & Partial<TaroGeneral.CallbackResult>, reject?: any): Promise<T & U & TaroGeneral.CallbackResult>;
19
+ success<U = Record<string, unknown>>(res?: Partial<T> & Partial<TaroGeneral.CallbackResult>, promise?: IMockPromise): Promise<T & U & TaroGeneral.CallbackResult>;
20
+ fail<U = Record<string, unknown>>(res?: Partial<T> & Partial<TaroGeneral.CallbackResult>, promise?: IMockPromise): Promise<T & U & TaroGeneral.CallbackResult>;
16
21
  }
17
22
  declare type TCallbackManagerParam = (...arr: unknown[]) => void;
18
23
  interface ICallbackManagerOption {
@@ -1,29 +1,39 @@
1
+ import { isFunction } from '@tarojs/shared';
2
+ import { isProd } from './index';
1
3
  export class MethodHandler {
2
4
  constructor({ name, success, fail, complete }) {
5
+ this.isHandlerError = false;
3
6
  this.methodName = name;
4
7
  this.__success = success;
5
8
  this.__fail = fail;
6
9
  this.__complete = complete;
10
+ this.isHandlerError = isFunction(this.__complete) || isFunction(this.__fail);
7
11
  }
8
- success(res = {}, resolve = Promise.resolve.bind(Promise)) {
12
+ success(res = {}, promise = {}) {
9
13
  if (!res.errMsg) {
10
14
  res.errMsg = `${this.methodName}:ok`;
11
15
  }
12
- typeof this.__success === 'function' && this.__success(res);
13
- typeof this.__complete === 'function' && this.__complete(res);
16
+ isFunction(this.__success) && this.__success(res);
17
+ isFunction(this.__complete) && this.__complete(res);
18
+ const { resolve = Promise.resolve.bind(Promise) } = promise;
14
19
  return resolve(res);
15
20
  }
16
- fail(res = {}, reject = Promise.reject.bind(Promise)) {
21
+ fail(res = {}, promise = {}) {
17
22
  if (!res.errMsg) {
18
23
  res.errMsg = `${this.methodName}:fail`;
19
24
  }
20
25
  else {
21
26
  res.errMsg = `${this.methodName}:fail ${res.errMsg}`;
22
27
  }
23
- console.error(res.errMsg);
24
- typeof this.__fail === 'function' && this.__fail(res);
25
- typeof this.__complete === 'function' && this.__complete(res);
26
- return reject(res);
28
+ if (!isProd) {
29
+ console.error(res.errMsg);
30
+ }
31
+ isFunction(this.__fail) && this.__fail(res);
32
+ isFunction(this.__complete) && this.__complete(res);
33
+ const { resolve = Promise.resolve.bind(Promise), reject = Promise.reject.bind(Promise) } = promise;
34
+ return this.isHandlerError
35
+ ? resolve(res)
36
+ : reject(res);
27
37
  }
28
38
  }
29
39
  export class CallbackManager {
@@ -67,12 +77,12 @@ export class CallbackManager {
67
77
  */
68
78
  this.trigger = (...args) => {
69
79
  this.callbacks.forEach(opt => {
70
- if (typeof opt === 'function') {
80
+ if (isFunction(opt)) {
71
81
  opt(...args);
72
82
  }
73
83
  else {
74
84
  const { callback, ctx } = opt;
75
- typeof callback === 'function' && callback.call(ctx, ...args);
85
+ isFunction(callback) && callback.call(ctx, ...args);
76
86
  }
77
87
  });
78
88
  };
@@ -2,6 +2,7 @@
2
2
  import Taro from '@tarojs/api';
3
3
  import { addLeadingSlash, getHomePage, stripBasename } from '@tarojs/router/dist/utils';
4
4
  import { Current, hooks } from '@tarojs/runtime';
5
+ import { isFunction } from '@tarojs/shared';
5
6
  import { MethodHandler } from './handler';
6
7
  export const isProd = process.env.NODE_ENV === 'production';
7
8
  export function shouldBeObject(target) {
@@ -141,7 +142,7 @@ export function processOpenApi({ name, defaultOptions, standardMethod, formatOpt
141
142
  // @ts-ignore
142
143
  const targetApi = (_a = window === null || window === void 0 ? void 0 : window.wx) === null || _a === void 0 ? void 0 : _a[name];
143
144
  const opts = formatOptions(Object.assign({}, defaultOptions, options));
144
- if (typeof targetApi === 'function') {
145
+ if (isFunction(targetApi)) {
145
146
  return new Promise((resolve, reject) => {
146
147
  ['fail', 'success', 'complete'].forEach(k => {
147
148
  opts[k] = preRef => {
@@ -158,7 +159,7 @@ export function processOpenApi({ name, defaultOptions, standardMethod, formatOpt
158
159
  });
159
160
  });
160
161
  }
161
- else if (typeof standardMethod === 'function') {
162
+ else if (isFunction(standardMethod)) {
162
163
  return standardMethod(opts);
163
164
  }
164
165
  else {
@@ -1,2 +1 @@
1
- export declare function isFunction(obj: any): boolean;
2
- export declare const isValidColor: (color: any) => boolean;
1
+ export declare const isValidColor: (color: string) => boolean;
@@ -1,6 +1,3 @@
1
- export function isFunction(obj) {
2
- return typeof obj === 'function';
3
- }
4
1
  const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
5
2
  export const isValidColor = (color) => {
6
3
  return VALID_COLOR_REG.test(color);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarojs/taro-h5",
3
- "version": "3.5.11",
3
+ "version": "3.6.0-beta.1",
4
4
  "description": "Taro h5 framework",
5
5
  "browser": "dist/index.esm.js",
6
6
  "main:h5": "dist/index.js",
@@ -31,11 +31,16 @@
31
31
  "jsonp-retry": "^1.0.3",
32
32
  "query-string": "^7.1.1",
33
33
  "whatwg-fetch": "^3.4.0",
34
- "@tarojs/api": "3.5.11",
35
- "@tarojs/router": "3.5.11",
36
- "@tarojs/runtime": "3.5.11"
34
+ "@tarojs/api": "3.6.0-beta.1",
35
+ "@tarojs/router": "3.6.0-beta.1",
36
+ "@tarojs/runtime": "3.6.0-beta.1",
37
+ "@tarojs/shared": "3.6.0-beta.1"
37
38
  },
38
39
  "devDependencies": {
40
+ "jest": "^29.3.1",
41
+ "jest-fetch-mock": "^3.0.3",
42
+ "jest-localstorage-mock": "^2.4.0",
43
+ "jest-mock-console": "^1.0.0",
39
44
  "react": "^18.2.0",
40
45
  "react-test-renderer": "^18.2.0"
41
46
  },
@@ -0,0 +1 @@
1
+ declare type TFunc = (...args: any[]) => any