@tarojs/taro-h5 3.4.7 → 3.4.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.
@@ -1,3 +1,4 @@
1
+ import { throttle } from '../utils';
1
2
  import { CallbackManager, MethodHandler } from '../utils/handler';
2
3
  const callbackManager = new CallbackManager();
3
4
  let devicemotionListener;
@@ -30,22 +31,6 @@ const INTERVAL_MAP = {
30
31
  frequency: 5
31
32
  }
32
33
  };
33
- const getDevicemotionListener = interval => {
34
- let lock;
35
- let timer;
36
- return evt => {
37
- if (lock)
38
- return;
39
- lock = true;
40
- timer && clearTimeout(timer);
41
- callbackManager.trigger({
42
- x: evt.acceleration.x || 0,
43
- y: evt.acceleration.y || 0,
44
- z: evt.acceleration.z || 0
45
- });
46
- timer = setTimeout(() => { lock = false; }, interval);
47
- };
48
- };
49
34
  /**
50
35
  * 开始监听加速度数据。
51
36
  */
@@ -57,7 +42,14 @@ export const startAccelerometer = ({ interval = 'normal', success, fail, complet
57
42
  if (devicemotionListener) {
58
43
  stopAccelerometer();
59
44
  }
60
- devicemotionListener = getDevicemotionListener(intervalObj.interval);
45
+ devicemotionListener = throttle((evt) => {
46
+ var _a, _b, _c;
47
+ callbackManager.trigger({
48
+ x: ((_a = evt.acceleration) === null || _a === void 0 ? void 0 : _a.x) || 0,
49
+ y: ((_b = evt.acceleration) === null || _b === void 0 ? void 0 : _b.y) || 0,
50
+ z: ((_c = evt.acceleration) === null || _c === void 0 ? void 0 : _c.z) || 0
51
+ });
52
+ }, intervalObj.interval);
61
53
  window.addEventListener('devicemotion', devicemotionListener, true);
62
54
  }
63
55
  else {
@@ -1,45 +1,59 @@
1
+ import { getDeviceInfo } from '../base/system';
2
+ import { throttle } from '../utils';
1
3
  import { CallbackManager, MethodHandler } from '../utils/handler';
2
4
  const callbackManager = new CallbackManager();
3
5
  let compassListener;
6
+ /**
7
+ * Note: 按系统类型获取对应绝对 orientation 事件名,因为安卓系统中直接监听 deviceorientation 事件得到的不是绝对 orientation
8
+ */
9
+ const deviceorientationEventName = ['absolutedeviceorientation', 'deviceorientationabsolute', 'deviceorientation'].find(item => {
10
+ if ('on' + item in window) {
11
+ return item;
12
+ }
13
+ }) || '';
4
14
  /**
5
15
  * 停止监听罗盘数据
6
16
  */
7
17
  export const stopCompass = ({ success, fail, complete } = {}) => {
8
18
  const handle = new MethodHandler({ name: 'stopCompass', success, fail, complete });
9
19
  try {
10
- window.removeEventListener('deviceorientation', compassListener, true);
20
+ window.removeEventListener(deviceorientationEventName, compassListener, true);
11
21
  return handle.success();
12
22
  }
13
23
  catch (e) {
14
24
  return handle.fail({ errMsg: e.message });
15
25
  }
16
26
  };
17
- const getDeviceOrientationListener = interval => {
18
- let lock;
19
- let timer;
20
- return evt => {
21
- if (lock)
22
- return;
23
- lock = true;
24
- timer && clearTimeout(timer);
25
- callbackManager.trigger({
26
- direction: 360 - evt.alpha
27
- });
28
- timer = setTimeout(() => { lock = false; }, interval);
29
- };
30
- };
27
+ let CompassChangeTrigger = false;
31
28
  /**
32
29
  * 开始监听罗盘数据
33
30
  */
34
31
  export const startCompass = ({ success, fail, complete } = {}) => {
35
32
  const handle = new MethodHandler({ name: 'startCompass', success, fail, complete });
36
33
  try {
37
- if (window.DeviceOrientationEvent) {
34
+ if (deviceorientationEventName !== '') {
38
35
  if (compassListener) {
39
36
  stopCompass();
40
37
  }
41
- compassListener = getDeviceOrientationListener(200);
42
- window.addEventListener('deviceorientation', compassListener, true);
38
+ compassListener = throttle((evt) => {
39
+ const isAndroid = getDeviceInfo().system === 'AndroidOS';
40
+ if (isAndroid && !evt.absolute && !CompassChangeTrigger) {
41
+ CompassChangeTrigger = true;
42
+ console.warn('Warning: In \'onCompassChange\', your browser is not supported to get the orientation relative to the earth, the orientation data will be related to the initial orientation of the device .');
43
+ }
44
+ const alpha = evt.alpha || 0;
45
+ /**
46
+ * 由于平台差异,accuracy 在 iOS/Android 的值不同。
47
+ * - iOS:accuracy 是一个 number 类型的值,表示相对于磁北极的偏差。0 表示设备指向磁北,90 表示指向东,180 表示指向南,依此类推。
48
+ * - Android:accuracy 是一个 string 类型的枚举值。
49
+ */
50
+ const accuracy = isAndroid ? evt.absolute ? 'high' : 'medium' : alpha;
51
+ callbackManager.trigger({
52
+ direction: 360 - alpha,
53
+ accuracy: accuracy
54
+ });
55
+ }, 5000);
56
+ window.addEventListener(deviceorientationEventName, compassListener, true);
43
57
  }
44
58
  else {
45
59
  throw new Error('compass is not supported');
@@ -1,3 +1,4 @@
1
+ import { throttle } from '../utils';
1
2
  import { CallbackManager, MethodHandler } from '../utils/handler';
2
3
  const callbackManager = new CallbackManager();
3
4
  let deviceMotionListener;
@@ -28,22 +29,6 @@ export const stopDeviceMotionListening = ({ success, fail, complete } = {}) => {
28
29
  return handle.fail({ errMsg: e.message });
29
30
  }
30
31
  };
31
- const getDeviceOrientationListener = interval => {
32
- let lock;
33
- let timer;
34
- return evt => {
35
- if (lock)
36
- return;
37
- lock = true;
38
- timer && clearTimeout(timer);
39
- callbackManager.trigger({
40
- alpha: evt.alpha,
41
- beta: evt.beta,
42
- gamma: evt.gamma
43
- });
44
- timer = setTimeout(() => { lock = false; }, interval);
45
- };
46
- };
47
32
  /**
48
33
  * 开始监听设备方向的变化。
49
34
  */
@@ -55,7 +40,13 @@ export const startDeviceMotionListening = ({ interval = 'normal', success, fail,
55
40
  if (deviceMotionListener) {
56
41
  stopDeviceMotionListening();
57
42
  }
58
- deviceMotionListener = getDeviceOrientationListener(intervalObj.interval);
43
+ deviceMotionListener = throttle((evt) => {
44
+ callbackManager.trigger({
45
+ alpha: evt.alpha,
46
+ beta: evt.beta,
47
+ gamma: evt.gamma
48
+ });
49
+ }, intervalObj.interval);
59
50
  window.addEventListener('deviceorientation', deviceMotionListener, true);
60
51
  }
61
52
  else {
@@ -0,0 +1,14 @@
1
+ /**
2
+ * ease-in-out的函数
3
+ * @param t 0-1的数字
4
+ */
5
+ export const easeInOut = (t) => (t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1);
6
+ export const getTimingFunc = (easeFunc, frameCnt) => {
7
+ return x => {
8
+ if (frameCnt <= 1) {
9
+ return easeFunc(1);
10
+ }
11
+ const t = x / (frameCnt - 1);
12
+ return easeFunc(t);
13
+ };
14
+ };
@@ -122,13 +122,6 @@ export function permanentlyNotSupport(apiName) {
122
122
  }
123
123
  };
124
124
  }
125
- export function isFunction(obj) {
126
- return typeof obj === 'function';
127
- }
128
- const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
129
- export const isValidColor = (color) => {
130
- return VALID_COLOR_REG.test(color);
131
- };
132
125
  export function processOpenApi({ name, defaultOptions, standardMethod, formatOptions = options => options, formatResult = res => res }) {
133
126
  const notSupported = weixinCorpSupport(name);
134
127
  return (options = {}) => {
@@ -161,17 +154,6 @@ export function processOpenApi({ name, defaultOptions, standardMethod, formatOpt
161
154
  }
162
155
  };
163
156
  }
164
- /**
165
- * ease-in-out的函数
166
- * @param t 0-1的数字
167
- */
168
- export const easeInOut = (t) => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
169
- export const getTimingFunc = (easeFunc, frameCnt) => {
170
- return x => {
171
- if (frameCnt <= 1) {
172
- return easeFunc(1);
173
- }
174
- const t = x / (frameCnt - 1);
175
- return easeFunc(t);
176
- };
177
- };
157
+ export * from './animation';
158
+ export * from './lodash';
159
+ export * from './valid';
@@ -0,0 +1,29 @@
1
+ export function throttle(fn, threshold = 250, scope) {
2
+ let lastTime = 0;
3
+ let deferTimer;
4
+ return function (...args) {
5
+ const context = scope || this;
6
+ const now = Date.now();
7
+ if (now - lastTime > threshold) {
8
+ fn.apply(this, args);
9
+ lastTime = now;
10
+ }
11
+ else {
12
+ clearTimeout(deferTimer);
13
+ deferTimer = setTimeout(() => {
14
+ lastTime = now;
15
+ fn.apply(context, args);
16
+ }, threshold);
17
+ }
18
+ };
19
+ }
20
+ export function debounce(fn, ms = 250, scope) {
21
+ let timer;
22
+ return function (...args) {
23
+ const context = scope || this;
24
+ clearTimeout(timer);
25
+ timer = setTimeout(function () {
26
+ fn.apply(context, args);
27
+ }, ms);
28
+ };
29
+ }
@@ -0,0 +1,7 @@
1
+ export function isFunction(obj) {
2
+ return typeof obj === 'function';
3
+ }
4
+ const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
5
+ export const isValidColor = (color) => {
6
+ return VALID_COLOR_REG.test(color);
7
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * ease-in-out的函数
3
+ * @param t 0-1的数字
4
+ */
5
+ export declare const easeInOut: (t: number) => number;
6
+ export declare const getTimingFunc: (easeFunc: any, frameCnt: any) => (x: any) => any;
@@ -26,8 +26,6 @@ export declare function weixinCorpSupport(apiName: any): () => Promise<{
26
26
  export declare function permanentlyNotSupport(apiName: any): () => Promise<{
27
27
  errMsg: string;
28
28
  }>;
29
- export declare function isFunction(obj: any): boolean;
30
- export declare const isValidColor: (color: any) => boolean;
31
29
  interface IProcessOpenApi<TOptions = Record<string, unknown>, TResult extends TaroGeneral.CallbackResult = any> {
32
30
  name: string;
33
31
  defaultOptions?: TOptions;
@@ -36,10 +34,6 @@ interface IProcessOpenApi<TOptions = Record<string, unknown>, TResult extends Ta
36
34
  formatResult?: (res: TResult) => TResult;
37
35
  }
38
36
  export declare function processOpenApi<TOptions = Record<string, unknown>, TResult extends TaroGeneral.CallbackResult = any>({ name, defaultOptions, standardMethod, formatOptions, formatResult }: IProcessOpenApi<TOptions, TResult>): (options?: Partial<TOptions>) => Promise<TResult>;
39
- /**
40
- * ease-in-out的函数
41
- * @param t 0-1的数字
42
- */
43
- export declare const easeInOut: (t: number) => number;
44
- export declare const getTimingFunc: (easeFunc: any, frameCnt: any) => (x: any) => any;
45
- export {};
37
+ export * from './animation';
38
+ export * from './lodash';
39
+ export * from './valid';
@@ -0,0 +1,2 @@
1
+ export declare function throttle(fn: any, threshold?: number, scope?: any): (...args: any[]) => void;
2
+ export declare function debounce(fn: any, ms?: number, scope?: any): (...args: any[]) => void;
@@ -0,0 +1,2 @@
1
+ export declare function isFunction(obj: any): boolean;
2
+ export declare const isValidColor: (color: any) => boolean;
package/dist/index.cjs.js CHANGED
@@ -17,6 +17,59 @@ var Taro__default = /*#__PURE__*/_interopDefaultLegacy(Taro);
17
17
  var MobileDetect__default = /*#__PURE__*/_interopDefaultLegacy(MobileDetect);
18
18
  var jsonpRetry__default = /*#__PURE__*/_interopDefaultLegacy(jsonpRetry);
19
19
 
20
+ /**
21
+ * ease-in-out的函数
22
+ * @param t 0-1的数字
23
+ */
24
+ const easeInOut = (t) => (t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1);
25
+ const getTimingFunc = (easeFunc, frameCnt) => {
26
+ return x => {
27
+ if (frameCnt <= 1) {
28
+ return easeFunc(1);
29
+ }
30
+ const t = x / (frameCnt - 1);
31
+ return easeFunc(t);
32
+ };
33
+ };
34
+
35
+ function throttle(fn, threshold = 250, scope) {
36
+ let lastTime = 0;
37
+ let deferTimer;
38
+ return function (...args) {
39
+ const context = scope || this;
40
+ const now = Date.now();
41
+ if (now - lastTime > threshold) {
42
+ fn.apply(this, args);
43
+ lastTime = now;
44
+ }
45
+ else {
46
+ clearTimeout(deferTimer);
47
+ deferTimer = setTimeout(() => {
48
+ lastTime = now;
49
+ fn.apply(context, args);
50
+ }, threshold);
51
+ }
52
+ };
53
+ }
54
+ function debounce(fn, ms = 250, scope) {
55
+ let timer;
56
+ return function (...args) {
57
+ const context = scope || this;
58
+ clearTimeout(timer);
59
+ timer = setTimeout(function () {
60
+ fn.apply(context, args);
61
+ }, ms);
62
+ };
63
+ }
64
+
65
+ function isFunction(obj) {
66
+ return typeof obj === 'function';
67
+ }
68
+ const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
69
+ const isValidColor = (color) => {
70
+ return VALID_COLOR_REG.test(color);
71
+ };
72
+
20
73
  /* eslint-disable prefer-promise-reject-errors */
21
74
  function shouldBeObject(target) {
22
75
  if (target && typeof target === 'object')
@@ -140,13 +193,6 @@ function permanentlyNotSupport(apiName) {
140
193
  }
141
194
  };
142
195
  }
143
- function isFunction(obj) {
144
- return typeof obj === 'function';
145
- }
146
- const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
147
- const isValidColor = (color) => {
148
- return VALID_COLOR_REG.test(color);
149
- };
150
196
  function processOpenApi({ name, defaultOptions, standardMethod, formatOptions = options => options, formatResult = res => res }) {
151
197
  const notSupported = weixinCorpSupport(name);
152
198
  return (options = {}) => {
@@ -179,20 +225,6 @@ function processOpenApi({ name, defaultOptions, standardMethod, formatOptions =
179
225
  }
180
226
  };
181
227
  }
182
- /**
183
- * ease-in-out的函数
184
- * @param t 0-1的数字
185
- */
186
- const easeInOut = (t) => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
187
- const getTimingFunc = (easeFunc, frameCnt) => {
188
- return x => {
189
- if (frameCnt <= 1) {
190
- return easeFunc(1);
191
- }
192
- const t = x / (frameCnt - 1);
193
- return easeFunc(t);
194
- };
195
- };
196
228
 
197
229
  // 广告
198
230
  const createRewardedVideoAd = temporarilyNotSupport('createRewardedVideoAd');
@@ -854,22 +886,6 @@ const INTERVAL_MAP$1 = {
854
886
  frequency: 5
855
887
  }
856
888
  };
857
- const getDevicemotionListener = interval => {
858
- let lock;
859
- let timer;
860
- return evt => {
861
- if (lock)
862
- return;
863
- lock = true;
864
- timer && clearTimeout(timer);
865
- callbackManager$3.trigger({
866
- x: evt.acceleration.x || 0,
867
- y: evt.acceleration.y || 0,
868
- z: evt.acceleration.z || 0
869
- });
870
- timer = setTimeout(() => { lock = false; }, interval);
871
- };
872
- };
873
889
  /**
874
890
  * 开始监听加速度数据。
875
891
  */
@@ -881,7 +897,14 @@ const startAccelerometer = ({ interval = 'normal', success, fail, complete } = {
881
897
  if (devicemotionListener) {
882
898
  stopAccelerometer();
883
899
  }
884
- devicemotionListener = getDevicemotionListener(intervalObj.interval);
900
+ devicemotionListener = throttle((evt) => {
901
+ var _a, _b, _c;
902
+ callbackManager$3.trigger({
903
+ x: ((_a = evt.acceleration) === null || _a === void 0 ? void 0 : _a.x) || 0,
904
+ y: ((_b = evt.acceleration) === null || _b === void 0 ? void 0 : _b.y) || 0,
905
+ z: ((_c = evt.acceleration) === null || _c === void 0 ? void 0 : _c.z) || 0
906
+ });
907
+ }, intervalObj.interval);
885
908
  window.addEventListener('devicemotion', devicemotionListener, true);
886
909
  }
887
910
  else {
@@ -1198,45 +1221,57 @@ const getClipboardData = async ({ success, fail, complete } = {}) => {
1198
1221
 
1199
1222
  const callbackManager$2 = new CallbackManager();
1200
1223
  let compassListener;
1224
+ /**
1225
+ * Note: 按系统类型获取对应绝对 orientation 事件名,因为安卓系统中直接监听 deviceorientation 事件得到的不是绝对 orientation
1226
+ */
1227
+ const deviceorientationEventName = ['absolutedeviceorientation', 'deviceorientationabsolute', 'deviceorientation'].find(item => {
1228
+ if ('on' + item in window) {
1229
+ return item;
1230
+ }
1231
+ }) || '';
1201
1232
  /**
1202
1233
  * 停止监听罗盘数据
1203
1234
  */
1204
1235
  const stopCompass = ({ success, fail, complete } = {}) => {
1205
1236
  const handle = new MethodHandler({ name: 'stopCompass', success, fail, complete });
1206
1237
  try {
1207
- window.removeEventListener('deviceorientation', compassListener, true);
1238
+ window.removeEventListener(deviceorientationEventName, compassListener, true);
1208
1239
  return handle.success();
1209
1240
  }
1210
1241
  catch (e) {
1211
1242
  return handle.fail({ errMsg: e.message });
1212
1243
  }
1213
1244
  };
1214
- const getDeviceOrientationListener$1 = interval => {
1215
- let lock;
1216
- let timer;
1217
- return evt => {
1218
- if (lock)
1219
- return;
1220
- lock = true;
1221
- timer && clearTimeout(timer);
1222
- callbackManager$2.trigger({
1223
- direction: 360 - evt.alpha
1224
- });
1225
- timer = setTimeout(() => { lock = false; }, interval);
1226
- };
1227
- };
1245
+ let CompassChangeTrigger = false;
1228
1246
  /**
1229
1247
  * 开始监听罗盘数据
1230
1248
  */
1231
1249
  const startCompass = ({ success, fail, complete } = {}) => {
1232
1250
  const handle = new MethodHandler({ name: 'startCompass', success, fail, complete });
1233
1251
  try {
1234
- if (window.DeviceOrientationEvent) {
1252
+ if (deviceorientationEventName !== '') {
1235
1253
  if (compassListener) {
1236
1254
  stopCompass();
1237
1255
  }
1238
- compassListener = getDeviceOrientationListener$1(200);
1239
- window.addEventListener('deviceorientation', compassListener, true);
1256
+ compassListener = throttle((evt) => {
1257
+ const isAndroid = getDeviceInfo().system === 'AndroidOS';
1258
+ if (isAndroid && !evt.absolute && !CompassChangeTrigger) {
1259
+ CompassChangeTrigger = true;
1260
+ console.warn('Warning: In \'onCompassChange\', your browser is not supported to get the orientation relative to the earth, the orientation data will be related to the initial orientation of the device .');
1261
+ }
1262
+ const alpha = evt.alpha || 0;
1263
+ /**
1264
+ * 由于平台差异,accuracy 在 iOS/Android 的值不同。
1265
+ * - iOS:accuracy 是一个 number 类型的值,表示相对于磁北极的偏差。0 表示设备指向磁北,90 表示指向东,180 表示指向南,依此类推。
1266
+ * - Android:accuracy 是一个 string 类型的枚举值。
1267
+ */
1268
+ const accuracy = isAndroid ? evt.absolute ? 'high' : 'medium' : alpha;
1269
+ callbackManager$2.trigger({
1270
+ direction: 360 - alpha,
1271
+ accuracy: accuracy
1272
+ });
1273
+ }, 5000);
1274
+ window.addEventListener(deviceorientationEventName, compassListener, true);
1240
1275
  }
1241
1276
  else {
1242
1277
  throw new Error('compass is not supported');
@@ -1321,22 +1356,6 @@ const stopDeviceMotionListening = ({ success, fail, complete } = {}) => {
1321
1356
  return handle.fail({ errMsg: e.message });
1322
1357
  }
1323
1358
  };
1324
- const getDeviceOrientationListener = interval => {
1325
- let lock;
1326
- let timer;
1327
- return evt => {
1328
- if (lock)
1329
- return;
1330
- lock = true;
1331
- timer && clearTimeout(timer);
1332
- callbackManager$1.trigger({
1333
- alpha: evt.alpha,
1334
- beta: evt.beta,
1335
- gamma: evt.gamma
1336
- });
1337
- timer = setTimeout(() => { lock = false; }, interval);
1338
- };
1339
- };
1340
1359
  /**
1341
1360
  * 开始监听设备方向的变化。
1342
1361
  */
@@ -1348,7 +1367,13 @@ const startDeviceMotionListening = ({ interval = 'normal', success, fail, comple
1348
1367
  if (deviceMotionListener) {
1349
1368
  stopDeviceMotionListening();
1350
1369
  }
1351
- deviceMotionListener = getDeviceOrientationListener(intervalObj.interval);
1370
+ deviceMotionListener = throttle((evt) => {
1371
+ callbackManager$1.trigger({
1372
+ alpha: evt.alpha,
1373
+ beta: evt.beta,
1374
+ gamma: evt.gamma
1375
+ });
1376
+ }, intervalObj.interval);
1352
1377
  window.addEventListener('deviceorientation', deviceMotionListener, true);
1353
1378
  }
1354
1379
  else {