@tarojs/taro-h5 3.7.0-alpha.7 → 3.7.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.
package/dist/index.esm.js CHANGED
@@ -3,7 +3,7 @@ import { history, navigateBack, navigateTo, reLaunch, redirectTo, getCurrentPage
3
3
  export { getCurrentPages, history, navigateBack, navigateTo, reLaunch, redirectTo, switchTab } from '@tarojs/router';
4
4
  import { isFunction, toKebabCase, PLATFORM_TYPE } from '@tarojs/shared';
5
5
  import { getCurrentPage, getHomePage } from '@tarojs/router/dist/utils';
6
- import { hooks, Current as Current$1 } from '@tarojs/runtime';
6
+ import { hooks, Current as Current$1, eventCenter as eventCenter$1 } from '@tarojs/runtime';
7
7
  import { fromByteArray, toByteArray } from 'base64-js';
8
8
  import { getMobileDetect, setTitle } from '@tarojs/router/dist/utils/navigate';
9
9
  import isNil from 'lodash-es/isNil';
@@ -2396,7 +2396,6 @@ class InnerAudioContext {
2396
2396
  this.destroy = () => {
2397
2397
  this.stop();
2398
2398
  if (this.Instance) {
2399
- document.body.removeChild(this.Instance);
2400
2399
  this.Instance = undefined;
2401
2400
  }
2402
2401
  };
@@ -2423,6 +2422,7 @@ class InnerAudioContext {
2423
2422
  this.Instance = new Audio();
2424
2423
  this.errorStack = new CallbackManager();
2425
2424
  this.stopStack = new CallbackManager();
2425
+ this.Instance.onerror = this.errorStack.trigger;
2426
2426
  Taro.eventCenter.on('__taroRouterChange', () => { this.stop(); });
2427
2427
  this.onPlay(() => {
2428
2428
  if (this.__isFirstPlay) {
@@ -2515,6 +2515,7 @@ class BackgroundAudioManager {
2515
2515
  this.Instance = new Audio();
2516
2516
  this.errorStack = new CallbackManager();
2517
2517
  this.stopStack = new CallbackManager();
2518
+ this.Instance.onerror = this.errorStack.trigger;
2518
2519
  this.Instance.autoplay = true;
2519
2520
  this.onPlay(() => {
2520
2521
  if (this.currentTime !== this.startTime) {
@@ -2685,12 +2686,16 @@ const previewImage = (options) => __awaiter(void 0, void 0, void 0, function* ()
2685
2686
  const { urls = [], current = '', success, fail, complete } = options;
2686
2687
  const handle = new MethodHandler({ name: 'previewImage', success, fail, complete });
2687
2688
  const container = document.createElement('div');
2689
+ const removeHandler = () => {
2690
+ eventCenter$1.off('__taroRouterChange', removeHandler);
2691
+ container.remove();
2692
+ };
2693
+ // 路由改变后应该关闭预览框
2694
+ eventCenter$1.on('__taroRouterChange', removeHandler);
2688
2695
  container.classList.add('preview-image');
2689
2696
  container.style.cssText =
2690
2697
  'position:fixed;top:0;left:0;z-index:1050;width:100%;height:100%;overflow:hidden;outline:0;background-color:#111;';
2691
- container.addEventListener('click', () => {
2692
- container.remove();
2693
- });
2698
+ container.addEventListener('click', removeHandler);
2694
2699
  const swiper = document.createElement('taro-swiper-core');
2695
2700
  // @ts-ignore
2696
2701
  swiper.full = true;
@@ -4967,15 +4972,17 @@ const downloadFile = ({ url, header, withCredentials, timeout, success, fail, co
4967
4972
  }
4968
4973
  });
4969
4974
  });
4970
- result.headersReceive = task.onHeadersReceived;
4971
- result.progress = task.onProgressUpdate;
4972
- return new Proxy(result, {
4973
- get(target, prop) {
4974
- const object = prop in task ? task : target;
4975
- const value = object[prop];
4976
- return typeof value === 'function' ? value.bind(object) : value;
4977
- },
4975
+ result.headersReceive = task.onHeadersReceived.bind(task);
4976
+ result.progress = task.onProgressUpdate.bind(task);
4977
+ const properties = {};
4978
+ Object.keys(task).forEach(key => {
4979
+ properties[key] = {
4980
+ get() {
4981
+ return typeof task[key] === 'function' ? task[key].bind(task) : task[key];
4982
+ }
4983
+ };
4978
4984
  });
4985
+ return Object.defineProperties(result, properties);
4979
4986
  };
4980
4987
 
4981
4988
  // mDNS
@@ -5002,19 +5009,21 @@ function generateRequestUrlWithParams(url = '', params) {
5002
5009
  }
5003
5010
  function _request(options = {}) {
5004
5011
  const { success, complete, fail } = options;
5005
- let url = options.url || '';
5006
5012
  const params = {};
5007
5013
  const res = {};
5008
- if (options.jsonp) {
5009
- const { jsonp } = options, opts = __rest(options, ["jsonp"]);
5010
- Object.assign(params, opts);
5014
+ let { cache = 'default', credentials, data, dataType, header = {}, jsonp, method = 'GET', mode, responseType, signal, timeout = 2000, url = '' } = options, opts = __rest(options, ["cache", "credentials", "data", "dataType", "header", "jsonp", "method", "mode", "responseType", "signal", "timeout", "url"]);
5015
+ Object.assign(params, opts);
5016
+ if (jsonp) {
5011
5017
  // @ts-ignore
5012
- params.params = opts.data;
5018
+ params.params = data;
5013
5019
  params.cache = opts.jsonpCache;
5020
+ // @ts-ignore
5021
+ params.timeout = timeout;
5014
5022
  if (typeof jsonp === 'string') {
5015
5023
  // @ts-ignore
5016
5024
  params.name = jsonp;
5017
5025
  }
5026
+ // Note: https://github.com/luckyadam/jsonp-retry
5018
5027
  return jsonpRetry(url, params)
5019
5028
  .then(data => {
5020
5029
  res.statusCode = 200;
@@ -5029,50 +5038,49 @@ function _request(options = {}) {
5029
5038
  return Promise.reject(err);
5030
5039
  });
5031
5040
  }
5032
- params.method = options.method || 'GET';
5041
+ params.method = method;
5033
5042
  const methodUpper = params.method.toUpperCase();
5034
- params.cache = options.cache || 'default';
5043
+ params.cache = cache;
5035
5044
  if (methodUpper === 'GET' || methodUpper === 'HEAD') {
5036
- url = generateRequestUrlWithParams(url, options.data);
5045
+ url = generateRequestUrlWithParams(url, data);
5037
5046
  }
5038
- else if (['[object Array]', '[object Object]'].indexOf(Object.prototype.toString.call(options.data)) >= 0) {
5039
- options.header = options.header || {};
5040
- const keyOfContentType = Object.keys(options.header).find(item => item.toLowerCase() === 'content-type');
5047
+ else if (['[object Array]', '[object Object]'].indexOf(Object.prototype.toString.call(data)) >= 0) {
5048
+ const keyOfContentType = Object.keys(header).find(item => item.toLowerCase() === 'content-type');
5041
5049
  if (!keyOfContentType) {
5042
- options.header['Content-Type'] = 'application/json';
5050
+ header['Content-Type'] = 'application/json';
5043
5051
  }
5044
- const contentType = options.header[keyOfContentType || 'Content-Type'];
5052
+ const contentType = header[keyOfContentType || 'Content-Type'];
5045
5053
  if (contentType.indexOf('application/json') >= 0) {
5046
- params.body = JSON.stringify(options.data);
5054
+ params.body = JSON.stringify(data);
5047
5055
  }
5048
5056
  else if (contentType.indexOf('application/x-www-form-urlencoded') >= 0) {
5049
- params.body = serializeParams(options.data);
5057
+ params.body = serializeParams(data);
5050
5058
  }
5051
5059
  else {
5052
- params.body = options.data;
5060
+ params.body = data;
5053
5061
  }
5054
5062
  }
5055
5063
  else {
5056
- params.body = options.data;
5064
+ params.body = data;
5057
5065
  }
5058
- if (options.header) {
5059
- params.headers = options.header;
5066
+ if (header) {
5067
+ params.headers = header;
5060
5068
  }
5061
- if (options.mode) {
5062
- params.mode = options.mode;
5069
+ if (mode) {
5070
+ params.mode = mode;
5063
5071
  }
5064
5072
  let timeoutTimer = null;
5065
- if (options.signal) {
5066
- params.signal = options.signal;
5073
+ if (signal) {
5074
+ params.signal = signal;
5067
5075
  }
5068
- else if (typeof options.timeout === 'number') {
5076
+ else if (typeof timeout === 'number') {
5069
5077
  const controller = new window.AbortController();
5070
5078
  params.signal = controller.signal;
5071
5079
  timeoutTimer = setTimeout(function () {
5072
5080
  controller.abort();
5073
- }, options.timeout);
5081
+ }, timeout);
5074
5082
  }
5075
- params.credentials = options.credentials;
5083
+ params.credentials = credentials;
5076
5084
  return fetch(url, params)
5077
5085
  .then(response => {
5078
5086
  if (timeoutTimer) {
@@ -5088,17 +5096,17 @@ function _request(options = {}) {
5088
5096
  for (const key of response.headers.keys()) {
5089
5097
  res.header[key] = response.headers.get(key);
5090
5098
  }
5091
- if (options.responseType === 'arraybuffer') {
5099
+ if (responseType === 'arraybuffer') {
5092
5100
  return response.arrayBuffer();
5093
5101
  }
5094
5102
  if (res.statusCode !== 204) {
5095
- if (options.dataType === 'json' || typeof options.dataType === 'undefined') {
5103
+ if (dataType === 'json' || typeof dataType === 'undefined') {
5096
5104
  return response.json().catch(() => {
5097
5105
  return null;
5098
5106
  });
5099
5107
  }
5100
5108
  }
5101
- if (options.responseType === 'text' || options.dataType === 'text') {
5109
+ if (responseType === 'text' || dataType === 'text') {
5102
5110
  return response.text();
5103
5111
  }
5104
5112
  return Promise.resolve(null);
@@ -5285,15 +5293,17 @@ const uploadFile = ({ url, filePath, name, header, formData, timeout, fileName,
5285
5293
  }
5286
5294
  });
5287
5295
  });
5288
- result.headersReceive = task.onHeadersReceived;
5289
- result.progress = task.onProgressUpdate;
5290
- return new Proxy(result, {
5291
- get(target, prop) {
5292
- const object = prop in task ? task : target;
5293
- const value = object[prop];
5294
- return typeof value === 'function' ? value.bind(object) : value;
5295
- },
5296
+ result.headersReceive = task.onHeadersReceived.bind(task);
5297
+ result.progress = task.onProgressUpdate.bind(task);
5298
+ const properties = {};
5299
+ Object.keys(task).forEach(key => {
5300
+ properties[key] = {
5301
+ get() {
5302
+ return typeof task[key] === 'function' ? task[key].bind(task) : task[key];
5303
+ }
5304
+ };
5296
5305
  });
5306
+ return Object.defineProperties(result, properties);
5297
5307
  };
5298
5308
 
5299
5309
  class SocketTask {
@@ -5408,9 +5418,9 @@ function connectSocket(options) {
5408
5418
  // protocols must be array
5409
5419
  const _protocols = Array.isArray(protocols) ? protocols : null;
5410
5420
  // 2 connection at most
5411
- if (socketTasks.length > 1) {
5421
+ if (socketTasks.length >= 5) {
5412
5422
  return handle.fail({
5413
- errMsg: '同时最多发起 2 个 socket 请求,更多请参考文档。'
5423
+ errMsg: '同时最多发起 5 个 socket 请求,更多请参考文档。'
5414
5424
  }, { resolve, reject });
5415
5425
  }
5416
5426
  const task = new SocketTask(url, _protocols);