@tarojs/with-weapp 4.0.0-beta.1 → 4.0.0-beta.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.
package/dist/index.esm.js CHANGED
@@ -1,30 +1,17 @@
1
- import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
- import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
3
- import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
4
- import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
5
- import _createClass from '@babel/runtime/helpers/createClass';
6
- import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
7
- import _get from '@babel/runtime/helpers/get';
8
- import _inherits from '@babel/runtime/helpers/inherits';
9
- import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
10
- import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
11
- import _typeof from '@babel/runtime/helpers/typeof';
12
1
  import { getCurrentInstance } from '@tarojs/runtime';
13
2
  import { nextTick, eventCenter, createSelectorQuery, createIntersectionObserver, createMediaQueryObserver } from '@tarojs/taro';
14
3
 
15
- var json = JSON;
16
- var clone = function clone(obj) {
17
- return json.parse(stringify(obj));
18
- };
19
- var isArray = Array.isArray || function (x) {
4
+ const json = JSON;
5
+ const clone = obj => json.parse(stringify(obj));
6
+ const isArray = Array.isArray || function (x) {
20
7
  return {}.toString.call(x) === '[object Array]';
21
8
  };
22
- var objectKeys = Object.keys || function (obj) {
23
- var has = Object.prototype.hasOwnProperty || function () {
9
+ const objectKeys = Object.keys || function (obj) {
10
+ const has = Object.prototype.hasOwnProperty || function () {
24
11
  return true;
25
12
  };
26
- var keys = [];
27
- for (var key in obj) {
13
+ const keys = [];
14
+ for (const key in obj) {
28
15
  if (has.call(obj, key)) keys.push(key);
29
16
  }
30
17
  return keys;
@@ -34,20 +21,20 @@ function stringify(obj, opts) {
34
21
  if (typeof opts === 'function') opts = {
35
22
  cmp: opts
36
23
  };
37
- var space = opts.space || '';
24
+ let space = opts.space || '';
38
25
  if (typeof space === 'number') space = Array(space + 1).join(' ');
39
- var cycles = typeof opts.cycles === 'boolean' ? opts.cycles : false;
40
- var replacer = opts.replacer || function (key, value) {
26
+ const cycles = typeof opts.cycles === 'boolean' ? opts.cycles : false;
27
+ const replacer = opts.replacer || function (key, value) {
41
28
  return value;
42
29
  };
43
- var cmp = opts.cmp && function (f) {
30
+ const cmp = opts.cmp && function (f) {
44
31
  return function (node) {
45
32
  return function (a, b) {
46
- var aobj = {
33
+ const aobj = {
47
34
  key: a,
48
35
  value: node[a]
49
36
  };
50
- var bobj = {
37
+ const bobj = {
51
38
  key: b,
52
39
  value: node[b]
53
40
  };
@@ -55,10 +42,10 @@ function stringify(obj, opts) {
55
42
  };
56
43
  };
57
44
  }(opts.cmp);
58
- var seen = [];
45
+ const seen = [];
59
46
  return function stringify(parent, key, node, level) {
60
- var indent = space ? '\n' + new Array(level + 1).join(space) : '';
61
- var colonSeparator = space ? ': ' : ':';
47
+ const indent = space ? '\n' + new Array(level + 1).join(space) : '';
48
+ const colonSeparator = space ? ': ' : ':';
62
49
  if (node && node.toJSON && typeof node.toJSON === 'function') {
63
50
  node = node.toJSON();
64
51
  }
@@ -66,13 +53,13 @@ function stringify(obj, opts) {
66
53
  if (node === undefined) {
67
54
  return;
68
55
  }
69
- if (_typeof(node) !== 'object' || node === null) {
56
+ if (typeof node !== 'object' || node === null) {
70
57
  return json.stringify(node);
71
58
  }
72
59
  if (isArray(node)) {
73
- var out = [];
74
- for (var i = 0; i < node.length; i++) {
75
- var item = stringify(node, i, node[i], level + 1) || json.stringify(null);
60
+ const out = [];
61
+ for (let i = 0; i < node.length; i++) {
62
+ const item = stringify(node, i, node[i], level + 1) || json.stringify(null);
76
63
  out.push(indent + space + item);
77
64
  }
78
65
  return '[' + out.join(',') + indent + ']';
@@ -81,17 +68,17 @@ function stringify(obj, opts) {
81
68
  if (cycles) return json.stringify('__cycle__');
82
69
  throw new TypeError('Converting circular structure to JSON');
83
70
  } else seen.push(node);
84
- var keys = objectKeys(node).sort(cmp && cmp(node));
85
- var _out = [];
86
- for (var _i = 0; _i < keys.length; _i++) {
87
- var _key = keys[_i];
88
- var value = stringify(node, _key, node[_key], level + 1);
71
+ const keys = objectKeys(node).sort(cmp && cmp(node));
72
+ const out = [];
73
+ for (let i = 0; i < keys.length; i++) {
74
+ const key = keys[i];
75
+ const value = stringify(node, key, node[key], level + 1);
89
76
  if (!value) continue;
90
- var keyValue = json.stringify(_key) + colonSeparator + value;
91
- _out.push(indent + space + keyValue);
77
+ const keyValue = json.stringify(key) + colonSeparator + value;
78
+ out.push(indent + space + keyValue);
92
79
  }
93
80
  seen.splice(seen.indexOf(node), 1);
94
- return '{' + _out.join(',') + indent + '}';
81
+ return '{' + out.join(',') + indent + '}';
95
82
  }
96
83
  }({
97
84
  '': obj
@@ -99,23 +86,23 @@ function stringify(obj, opts) {
99
86
  }
100
87
 
101
88
  /*eslint-disable*/
102
- var ARRAYTYPE = '[object Array]';
103
- var OBJECTTYPE = '[object Object]';
104
- var FUNCTIONTYPE = '[object Function]';
89
+ const ARRAYTYPE = '[object Array]';
90
+ const OBJECTTYPE = '[object Object]';
91
+ const FUNCTIONTYPE = '[object Function]';
105
92
  function diff(current, pre) {
106
- var result = {};
93
+ const result = {};
107
94
  syncKeys(current, pre);
108
95
  _diff(current, pre, '', result);
109
96
  return result;
110
97
  }
111
98
  function syncKeys(current, pre) {
112
99
  if (current === pre) return;
113
- var rootCurrentType = type(current);
114
- var rootPreType = type(pre);
100
+ const rootCurrentType = type(current);
101
+ const rootPreType = type(pre);
115
102
  if (rootCurrentType == OBJECTTYPE && rootPreType == OBJECTTYPE) {
116
103
  // if(Object.keys(current).length >= Object.keys(pre).length){
117
- for (var key in pre) {
118
- var currentValue = current[key];
104
+ for (let key in pre) {
105
+ const currentValue = current[key];
119
106
  if (currentValue === undefined) {
120
107
  current[key] = null;
121
108
  } else {
@@ -125,7 +112,7 @@ function syncKeys(current, pre) {
125
112
  // }
126
113
  } else if (rootCurrentType == ARRAYTYPE && rootPreType == ARRAYTYPE) {
127
114
  if (current.length >= pre.length) {
128
- pre.forEach(function (item, index) {
115
+ pre.forEach((item, index) => {
129
116
  syncKeys(current[index], item);
130
117
  });
131
118
  }
@@ -133,17 +120,17 @@ function syncKeys(current, pre) {
133
120
  }
134
121
  function _diff(current, pre, path, result) {
135
122
  if (current === pre) return;
136
- var rootCurrentType = type(current);
137
- var rootPreType = type(pre);
123
+ const rootCurrentType = type(current);
124
+ const rootPreType = type(pre);
138
125
  if (rootCurrentType == OBJECTTYPE) {
139
126
  if (rootPreType != OBJECTTYPE || Object.keys(current).length < Object.keys(pre).length) {
140
127
  setResult(result, path, current);
141
128
  } else {
142
- var _loop = function _loop(key) {
143
- var currentValue = current[key];
144
- var preValue = pre[key];
145
- var currentType = type(currentValue);
146
- var preType = type(preValue);
129
+ for (let key in current) {
130
+ const currentValue = current[key];
131
+ const preValue = pre[key];
132
+ const currentType = type(currentValue);
133
+ const preType = type(preValue);
147
134
  if (currentType != ARRAYTYPE && currentType != OBJECTTYPE) {
148
135
  if (currentValue != pre[key]) {
149
136
  setResult(result, (path == '' ? '' : path + '.') + key, currentValue);
@@ -155,7 +142,7 @@ function _diff(current, pre, path, result) {
155
142
  if (currentValue.length < preValue.length) {
156
143
  setResult(result, (path == '' ? '' : path + '.') + key, currentValue);
157
144
  } else {
158
- currentValue.forEach(function (item, index) {
145
+ currentValue.forEach((item, index) => {
159
146
  _diff(item, preValue[index], (path == '' ? '' : path + '.') + key + '[' + index + ']', result);
160
147
  });
161
148
  }
@@ -164,14 +151,11 @@ function _diff(current, pre, path, result) {
164
151
  if (preType != OBJECTTYPE || Object.keys(currentValue).length < Object.keys(preValue).length) {
165
152
  setResult(result, (path == '' ? '' : path + '.') + key, currentValue);
166
153
  } else {
167
- for (var subKey in currentValue) {
154
+ for (let subKey in currentValue) {
168
155
  _diff(currentValue[subKey], preValue[subKey], (path == '' ? '' : path + '.') + key + '.' + subKey, result);
169
156
  }
170
157
  }
171
158
  }
172
- };
173
- for (var key in current) {
174
- _loop(key);
175
159
  }
176
160
  }
177
161
  } else if (rootCurrentType == ARRAYTYPE) {
@@ -181,7 +165,7 @@ function _diff(current, pre, path, result) {
181
165
  if (current.length < pre.length) {
182
166
  setResult(result, path, current);
183
167
  } else {
184
- current.forEach(function (item, index) {
168
+ current.forEach((item, index) => {
185
169
  _diff(item, pre[index], path + '[' + index + ']', result);
186
170
  });
187
171
  }
@@ -207,16 +191,20 @@ var TaroLifeCycles;
207
191
  TaroLifeCycles["DidHide"] = "componentDidHide";
208
192
  TaroLifeCycles["WillUnmount"] = "componentWillUnmount";
209
193
  })(TaroLifeCycles || (TaroLifeCycles = {}));
210
- var lifecycleMap = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, TaroLifeCycles.WillMount, ['created']), TaroLifeCycles.DidMount, ['attached']), TaroLifeCycles.DidShow, ['onShow']), TaroLifeCycles.DidHide, ['onHide']), TaroLifeCycles.WillUnmount, ['detached', 'onUnload']);
211
- var lifecycles = new Set(['ready']);
212
- for (var key in lifecycleMap) {
213
- var lifecycle = lifecycleMap[key];
214
- lifecycle.forEach(function (l) {
215
- return lifecycles.add(l);
216
- });
194
+ const lifecycleMap = {
195
+ [TaroLifeCycles.WillMount]: ['created'],
196
+ [TaroLifeCycles.DidMount]: ['attached'],
197
+ [TaroLifeCycles.DidShow]: ['onShow'],
198
+ [TaroLifeCycles.DidHide]: ['onHide'],
199
+ [TaroLifeCycles.WillUnmount]: ['detached', 'onUnload']
200
+ };
201
+ const lifecycles = new Set(['ready']);
202
+ for (const key in lifecycleMap) {
203
+ const lifecycle = lifecycleMap[key];
204
+ lifecycle.forEach(l => lifecycles.add(l));
217
205
  }
218
- var uniquePageLifecycle = ['onPullDownRefresh', 'onReachBottom', 'onShareAppMessage', 'onShareTimeline', 'onAddToFavorites', 'onPageScroll', 'onResize', 'onTabItemTap'];
219
- var appOptions = ['onLaunch', 'onShow', 'onHide', 'onError', 'onPageNotFound', 'onUnhandledRejection', 'onThemeChange'];
206
+ const uniquePageLifecycle = ['onPullDownRefresh', 'onReachBottom', 'onShareAppMessage', 'onShareTimeline', 'onAddToFavorites', 'onPageScroll', 'onResize', 'onTabItemTap'];
207
+ const appOptions = ['onLaunch', 'onShow', 'onHide', 'onError', 'onPageNotFound', 'onUnhandledRejection', 'onThemeChange'];
220
208
 
221
209
  /**
222
210
  * Simple bind, faster than native
@@ -224,7 +212,7 @@ var appOptions = ['onLaunch', 'onShow', 'onHide', 'onError', 'onPageNotFound', '
224
212
  function bind(fn /*: Function */, ctx /*: Object */) {
225
213
  if (!fn) return false;
226
214
  function boundFn(a) {
227
- var l /*: number */ = arguments.length;
215
+ const l /*: number */ = arguments.length;
228
216
  return l ? l > 1 ? fn.apply(ctx, arguments) : fn.call(ctx, a) : fn.call(ctx);
229
217
  }
230
218
  // record original fn length
@@ -238,7 +226,7 @@ function safeGet(obj, propsArg, defaultValue) {
238
226
  if (!obj) {
239
227
  return defaultValue;
240
228
  }
241
- var props, prop;
229
+ let props, prop;
242
230
  if (Array.isArray(propsArg)) {
243
231
  props = propsArg.slice(0);
244
232
  }
@@ -246,7 +234,7 @@ function safeGet(obj, propsArg, defaultValue) {
246
234
  props = propsArg.replace(/\[(.+?)\]/g, '.$1');
247
235
  props = props.split('.');
248
236
  }
249
- if (_typeof(propsArg) === 'symbol') {
237
+ if (typeof propsArg === 'symbol') {
250
238
  props = [propsArg];
251
239
  }
252
240
  if (!Array.isArray(props)) {
@@ -269,14 +257,14 @@ function safeSet(obj, props, value) {
269
257
  props = props.replace(/\[(.+?)\]/g, '.$1');
270
258
  props = props.split('.');
271
259
  }
272
- if (_typeof(props) === 'symbol') {
260
+ if (typeof props === 'symbol') {
273
261
  props = [props];
274
262
  }
275
- var lastProp = props.pop();
263
+ const lastProp = props.pop();
276
264
  if (!lastProp) {
277
265
  return false;
278
266
  }
279
- var thisProp;
267
+ let thisProp;
280
268
  while (thisProp = props.shift()) {
281
269
  if (typeof obj[thisProp] === 'undefined') {
282
270
  obj[thisProp] = {};
@@ -284,12 +272,12 @@ function safeSet(obj, props, value) {
284
272
  // 直接按路径修改 this.state 可能会导致 nextProps 也被修改
285
273
  // 因此按路径寻找时,每一层都复制一遍
286
274
  if (Array.isArray(obj[thisProp])) {
287
- obj[thisProp] = _toConsumableArray(obj[thisProp]);
288
- } else if (_typeof(obj[thisProp]) === 'object') {
275
+ obj[thisProp] = [...obj[thisProp]];
276
+ } else if (typeof obj[thisProp] === 'object') {
289
277
  obj[thisProp] = Object.assign({}, obj[thisProp]);
290
278
  }
291
279
  obj = obj[thisProp];
292
- if (!obj || _typeof(obj) !== 'object') {
280
+ if (!obj || typeof obj !== 'object') {
293
281
  return false;
294
282
  }
295
283
  }
@@ -299,26 +287,24 @@ function safeSet(obj, props, value) {
299
287
  function report(msg) {
300
288
  console.warn('[Taro Convert Warning] ' + msg);
301
289
  }
302
- var nonsupport = new Map([['onError', '不支持 App 的 onError 生命周期方法。'], ['onPageNotFound', '不支持 App 的 onPageNotFound 生命周期方法。'], ['onUnhandledRejection', '不支持 App 的 onUnhandledRejection 生命周期方法。'], ['onThemeChange', '不支持 App 的 onThemeChange 生命周期方法。'], ['moved', '不支持自定义组件的 moved 生命周期。'], ['externalClasses', '不支持自定义组件的 externalClasses 功能。'], ['relations', '不支持自定义组件的 relations 功能。'], ['options', '不支持自定义组件的 options 功能。'], ['definitionFilter', '不支持自定义组件的 definitionFilter 功能。'], ['selectComponent', 'selectComponent 方法产生不到目标效果,请使用 React 的 ref 进行重构。'], ['selectAllComponents', 'selectAllComponents 方法产生不到目标效果,请使用 React 的 ref 进行重构。'], ['selectOwnerComponent', 'selectOwnerComponent 方法产生不到目标效果,请使用 React 语法重构。'], ['groupSetData', 'groupSetData 方法产生不到目标效果,请使用 React 语法重构。']]);
290
+ const nonsupport = new Map([['onError', '不支持 App 的 onError 生命周期方法。'], ['onPageNotFound', '不支持 App 的 onPageNotFound 生命周期方法。'], ['onUnhandledRejection', '不支持 App 的 onUnhandledRejection 生命周期方法。'], ['onThemeChange', '不支持 App 的 onThemeChange 生命周期方法。'], ['moved', '不支持自定义组件的 moved 生命周期。'], ['externalClasses', '不支持自定义组件的 externalClasses 功能。'], ['relations', '不支持自定义组件的 relations 功能。'], ['options', '不支持自定义组件的 options 功能。'], ['definitionFilter', '不支持自定义组件的 definitionFilter 功能。'], ['selectComponent', 'selectComponent 方法产生不到目标效果,请使用 React 的 ref 进行重构。'], ['selectAllComponents', 'selectAllComponents 方法产生不到目标效果,请使用 React 的 ref 进行重构。'], ['selectOwnerComponent', 'selectOwnerComponent 方法产生不到目标效果,请使用 React 语法重构。'], ['groupSetData', 'groupSetData 方法产生不到目标效果,请使用 React 语法重构。']]);
303
291
  function flattenBehaviors(behavior, behaviorMap) {
304
292
  if (typeof behavior === 'string') {
305
- return report("\u4E0D\u652F\u6301\u4F7F\u7528\u5185\u7F6E Behavior: [".concat(behavior, "]"));
293
+ return report(`不支持使用内置 Behavior: [${behavior}]`);
306
294
  }
307
- var subBehaviors = behavior.behaviors;
295
+ const subBehaviors = behavior.behaviors;
308
296
  if (subBehaviors === null || subBehaviors === void 0 ? void 0 : subBehaviors.length) {
309
- subBehaviors.forEach(function (subBehavior) {
310
- return flattenBehaviors(subBehavior, behaviorMap);
311
- });
297
+ subBehaviors.forEach(subBehavior => flattenBehaviors(subBehavior, behaviorMap));
312
298
  }
313
- Object.keys(behavior).forEach(function (key) {
299
+ Object.keys(behavior).forEach(key => {
314
300
  // 不支持的属性
315
301
  if (nonsupport.has(key)) {
316
- var advise = nonsupport.get(key);
302
+ const advise = nonsupport.get(key);
317
303
  return report(advise);
318
304
  }
319
305
  if (behaviorMap.has(key)) {
320
- var list = behaviorMap.get(key);
321
- var value = behavior[key];
306
+ const list = behaviorMap.get(key);
307
+ const value = behavior[key];
322
308
  list.push(value);
323
309
  }
324
310
  });
@@ -327,21 +313,21 @@ function flattenBehaviors(behavior, behaviorMap) {
327
313
  /**
328
314
  * 该模块仅存放 convert 转换时用到的工具函数或变量
329
315
  */
330
- var cacheOptions = {
316
+ const cacheOptions = {
331
317
  cacheOptions: {},
332
- setOptionsToCache: function setOptionsToCache(options) {
318
+ setOptionsToCache: function (options) {
333
319
  if (Object.keys(options).length !== 0) {
334
320
  this.cacheOptions = options;
335
321
  }
336
322
  },
337
- getOptionsFromCache: function getOptionsFromCache() {
323
+ getOptionsFromCache: function () {
338
324
  return this.cacheOptions;
339
325
  }
340
326
  };
341
327
  function toCamelCase(s) {
342
- var camel = '';
343
- var nextCap = false;
344
- for (var i = 0; i < s.length; i++) {
328
+ let camel = '';
329
+ let nextCap = false;
330
+ for (let i = 0; i < s.length; i++) {
345
331
  if (s[i] !== '-') {
346
332
  camel += nextCap ? s[i].toUpperCase() : s[i];
347
333
  nextCap = false;
@@ -351,41 +337,39 @@ function toCamelCase(s) {
351
337
  }
352
338
  return camel;
353
339
  }
354
- var convertToArray = function convertToArray(value, fn) {
340
+ const convertToArray = function (value, fn) {
355
341
  if (value instanceof Array) {
356
342
  return value.map(fn);
357
343
  } else if (typeof value === 'number') {
358
344
  return Array.from({
359
345
  length: value
360
- }, function (_, index) {
361
- return index;
362
- }).map(fn);
346
+ }, (_, index) => index).map(fn);
363
347
  } else if (typeof value === 'string') {
364
348
  return Array.from(value).map(fn);
365
- } else if (_typeof(value) === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype) {
366
- var result = Object.keys(value).map(function (item) {
349
+ } else if (typeof value === 'object' && value !== null && Object.getPrototypeOf(value) === Object.prototype) {
350
+ const result = Object.keys(value).map(item => {
367
351
  return fn(value[item], item);
368
352
  });
369
353
  return result;
370
354
  }
371
355
  };
372
- var getTarget = function getTarget(target, Taro) {
356
+ const getTarget = (target, Taro) => {
373
357
  if (!target) {
374
358
  return {
375
359
  dataset: {}
376
360
  };
377
361
  }
378
- if (Taro.getEnv() === Taro.ENV_TYPE.MPHARMONY || Taro.getEnv() === Taro.ENV_TYPE.WEB) {
362
+ if (Taro.getEnv() === Taro.ENV_TYPE.HARMONYHYBRID || Taro.getEnv() === Taro.ENV_TYPE.WEB) {
379
363
  if (target.fullDataset) {
380
364
  return {
381
365
  dataset: target.fullDataset
382
366
  };
383
367
  }
384
- var fullDataset = {};
368
+ const fullDataset = {};
385
369
  // 获取元素的所有属性
386
- var targetAttrKeys = Object.keys(target);
370
+ const targetAttrKeys = Object.keys(target);
387
371
  // 遍历所有属性
388
- for (var i = 0; i < targetAttrKeys.length; i++) {
372
+ for (let i = 0; i < targetAttrKeys.length; i++) {
389
373
  if (targetAttrKeys[i].startsWith('data-')) {
390
374
  fullDataset[toCamelCase(targetAttrKeys[i].replace(/^data-/, '').toLowerCase())] = target[targetAttrKeys[i]];
391
375
  }
@@ -398,16 +382,11 @@ var getTarget = function getTarget(target, Taro) {
398
382
  return target;
399
383
  };
400
384
 
401
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
402
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
403
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
404
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
405
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
406
385
  function defineGetter(component, key, getter) {
407
386
  Object.defineProperty(component, key, {
408
387
  enumerable: true,
409
388
  configurable: true,
410
- get: function get() {
389
+ get: () => {
411
390
  if (getter === 'props') {
412
391
  return component.props;
413
392
  }
@@ -419,38 +398,38 @@ function defineGetter(component, key, getter) {
419
398
  }
420
399
  });
421
400
  }
401
+
422
402
  function propToState(newValue, _oldValue, key) {
423
403
  this.state[key] = newValue;
424
404
  }
425
405
  function isFunction(o) {
426
406
  return typeof o === 'function';
427
407
  }
428
- function withWeapp(weappConf) {
429
- var isApp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
430
- if (_typeof(weappConf) === 'object' && Object.keys(weappConf).length === 0) {
408
+ function withWeapp(weappConf, isApp = false) {
409
+ if (typeof weappConf === 'object' && Object.keys(weappConf).length === 0) {
431
410
  report('withWeapp 请传入“App/页面/组件“的配置对象。如果原生写法使用了基类,请将基类组合后的配置对象传入,详情请参考文档。');
432
411
  }
433
- return function (ConnectComponent) {
412
+ return ConnectComponent => {
434
413
  var _a;
435
- var behaviorMap = new Map([['properties', []], ['data', []], ['methods', []], ['created', []], ['attached', []], ['ready', []], ['detached', []], ['lifetimes', []]]);
436
- var behaviorProperties = {};
414
+ const behaviorMap = new Map([['properties', []], ['data', []], ['methods', []], ['created', []], ['attached', []], ['ready', []], ['detached', []], ['lifetimes', []]]);
415
+ const behaviorProperties = {};
437
416
  if ((_a = weappConf.behaviors) === null || _a === void 0 ? void 0 : _a.length) {
438
- var behaviors = weappConf.behaviors;
439
- behaviors.forEach(function (behavior) {
440
- return flattenBehaviors(behavior, behaviorMap);
441
- });
442
- var propertiesList = behaviorMap.get('properties');
417
+ const {
418
+ behaviors
419
+ } = weappConf;
420
+ behaviors.forEach(behavior => flattenBehaviors(behavior, behaviorMap));
421
+ const propertiesList = behaviorMap.get('properties');
443
422
  if (propertiesList.length) {
444
- propertiesList.forEach(function (property) {
423
+ propertiesList.forEach(property => {
445
424
  Object.assign(behaviorProperties, property);
446
425
  });
447
- Object.keys(behaviorProperties).forEach(function (propName) {
448
- var propValue = behaviorProperties[propName];
426
+ Object.keys(behaviorProperties).forEach(propName => {
427
+ const propValue = behaviorProperties[propName];
449
428
  if (!weappConf.properties) {
450
429
  weappConf.properties = {};
451
430
  }
452
431
  if (!weappConf.properties.hasOwnProperty(propName)) {
453
- if (propValue && _typeof(propValue) === 'object' && propValue.value) {
432
+ if (propValue && typeof propValue === 'object' && propValue.value) {
454
433
  propValue.value = clone(propValue.value);
455
434
  }
456
435
  weappConf.properties[propName] = propValue;
@@ -458,45 +437,38 @@ function withWeapp(weappConf) {
458
437
  });
459
438
  }
460
439
  }
461
- var BaseComponent = /*#__PURE__*/function (_ConnectComponent) {
462
- _inherits(BaseComponent, _ConnectComponent);
463
- var _super = _createSuper(BaseComponent);
464
- function BaseComponent(props) {
465
- var _this;
466
- _classCallCheck(this, BaseComponent);
467
- _this = _super.call(this, props);
468
- _this._observeProps = [];
440
+ class BaseComponent extends ConnectComponent {
441
+ constructor(props) {
442
+ super(props);
443
+ this._observeProps = [];
469
444
  // mixins 可以多次调用生命周期
470
- _this.willMounts = [];
471
- _this.didMounts = [];
472
- _this.didHides = [];
473
- _this.didShows = [];
474
- _this.willUnmounts = [];
475
- _this.eventDestroyList = [];
476
- _this.current = getCurrentInstance();
477
- _this.taroGlobalData = Object.create(null);
478
- _this.safeExecute = function (func) {
479
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
480
- args[_key - 1] = arguments[_key];
481
- }
482
- if (isFunction(func)) func.apply(_assertThisInitialized(_this), args);
445
+ this.willMounts = [];
446
+ this.didMounts = [];
447
+ this.didHides = [];
448
+ this.didShows = [];
449
+ this.willUnmounts = [];
450
+ this.eventDestroyList = [];
451
+ this.current = getCurrentInstance();
452
+ this.taroGlobalData = Object.create(null);
453
+ this.safeExecute = (func, ...args) => {
454
+ if (isFunction(func)) func.apply(this, args);
483
455
  };
484
- _this.setData = function (obj, callback) {
485
- var oldState;
486
- if (_this.observers && Object.keys(Object.keys(_this.observers))) {
487
- oldState = clone(_this.state);
456
+ this.setData = (obj, callback) => {
457
+ let oldState;
458
+ if (this.observers && Object.keys(Object.keys(this.observers))) {
459
+ oldState = clone(this.state);
488
460
  }
489
- Object.keys(obj).forEach(function (key) {
490
- safeSet(_this.state, key, obj[key]);
461
+ Object.keys(obj).forEach(key => {
462
+ safeSet(this.state, key, obj[key]);
491
463
  });
492
- _this.setState(_this.state, function () {
493
- _this.triggerObservers(_this.state, oldState);
464
+ this.setState(this.state, () => {
465
+ this.triggerObservers(this.state, oldState);
494
466
  if (callback) {
495
- callback.call(_assertThisInitialized(_this));
467
+ callback.call(this);
496
468
  }
497
469
  });
498
470
  };
499
- _this.triggerEvent = function (eventName, detail, options) {
471
+ this.triggerEvent = (eventName, detail, options) => {
500
472
  if (options) {
501
473
  report('triggerEvent 不支持事件选项。');
502
474
  }
@@ -506,561 +478,467 @@ function withWeapp(weappConf) {
506
478
  return match[1].toUpperCase();
507
479
  });
508
480
  }
509
- var props = _this.props;
510
- var dataset = {};
511
- for (var key in props) {
481
+ const props = this.props;
482
+ const dataset = {};
483
+ for (const key in props) {
512
484
  if (!key.startsWith('data-')) continue;
513
485
  dataset[key.replace(/^data-/, '')] = props[key];
514
486
  }
515
- var func = props["on".concat(eventName[0].toUpperCase()).concat(eventName.slice(1))];
487
+ const func = props[`on${eventName[0].toUpperCase()}${eventName.slice(1)}`];
516
488
  if (isFunction(func)) {
517
- func.call(_assertThisInitialized(_this), {
489
+ func.call(this, {
518
490
  type: eventName,
519
- detail: detail,
491
+ detail,
520
492
  target: {
521
493
  id: props.id || '',
522
- dataset: dataset
494
+ dataset
523
495
  },
524
496
  currentTarget: {
525
497
  id: props.id || '',
526
- dataset: dataset
498
+ dataset
527
499
  }
528
500
  });
529
501
  }
530
502
  };
531
- _this.hasBehavior = _this.componentMethodsProxy('hasBehavior');
532
- _this.createSelectorQuery = _this.componentMethodsProxy('createSelectorQuery');
533
- _this.createIntersectionObserver = _this.componentMethodsProxy('createIntersectionObserver');
534
- _this.createMediaQueryObserver = _this.componentMethodsProxy('createMediaQueryObserver');
535
- _this.getRelationNodes = _this.componentMethodsProxy('getRelationNodes');
536
- _this.getTabBar = _this.componentMethodsProxy('getTabBar');
537
- _this.getPageId = _this.componentMethodsProxy('getPageId');
538
- _this.animate = _this.componentMethodsProxy('animate');
539
- _this.clearAnimation = _this.componentMethodsProxy('clearAnimation');
540
- _this.setUpdatePerformanceListener = _this.componentMethodsProxy('setUpdatePerformanceListener');
541
- _this.state = _this.state || {};
542
- _this.init(weappConf);
543
- defineGetter(_assertThisInitialized(_this), 'data', 'state');
544
- defineGetter(_assertThisInitialized(_this), 'properties', 'props');
545
- return _this;
503
+ this.hasBehavior = this.componentMethodsProxy('hasBehavior');
504
+ this.createSelectorQuery = this.componentMethodsProxy('createSelectorQuery');
505
+ this.createIntersectionObserver = this.componentMethodsProxy('createIntersectionObserver');
506
+ this.createMediaQueryObserver = this.componentMethodsProxy('createMediaQueryObserver');
507
+ this.getRelationNodes = this.componentMethodsProxy('getRelationNodes');
508
+ this.getTabBar = this.componentMethodsProxy('getTabBar');
509
+ this.getPageId = this.componentMethodsProxy('getPageId');
510
+ this.animate = this.componentMethodsProxy('animate');
511
+ this.clearAnimation = this.componentMethodsProxy('clearAnimation');
512
+ this.setUpdatePerformanceListener = this.componentMethodsProxy('setUpdatePerformanceListener');
513
+ this.state = this.state || {};
514
+ this.init(weappConf);
515
+ defineGetter(this, 'data', 'state');
516
+ defineGetter(this, 'properties', 'props');
546
517
  }
547
- _createClass(BaseComponent, [{
548
- key: "initProps",
549
- value: function initProps(props) {
550
- var properties = {};
551
- for (var propKey in props) {
552
- if (props.hasOwnProperty(propKey)) {
553
- var propValue = props[propKey];
554
- // propValue 可能是 null, 构造函数, 对象
555
- var observers = [propToState];
556
- if (propValue === null || propValue === undefined) {
557
- // propValue为null、undefined情况
558
- properties[propKey] = null;
559
- } else if (isFunction(propValue)) {
560
- // propValue为Function,即Array、String、Boolean等情况时
561
- if (propValue.name === 'Array') {
562
- properties[propKey] = [];
563
- } else if (propValue.name === 'String') {
564
- properties[propKey] = '';
565
- } else if (propValue.name === 'Boolean') {
566
- properties[propKey] = false;
567
- } else if (propValue.name === 'Number') {
568
- properties[propKey] = 0;
569
- } else {
570
- properties[propKey] = null;
571
- }
572
- } else if (_typeof(propValue) === 'object') {
573
- // propValue为对象时
574
- properties[propKey] = propValue.value;
575
- if (propValue.observer) {
576
- observers.push(propValue.observer);
577
- }
518
+ initProps(props) {
519
+ const properties = {};
520
+ for (const propKey in props) {
521
+ if (props.hasOwnProperty(propKey)) {
522
+ const propValue = props[propKey];
523
+ // propValue 可能是 null, 构造函数, 对象
524
+ const observers = [propToState];
525
+ if (propValue === null || propValue === undefined) {
526
+ // propValue为null、undefined情况
527
+ properties[propKey] = null;
528
+ } else if (isFunction(propValue)) {
529
+ // propValue为Function,即Array、String、Boolean等情况时
530
+ if (propValue.name === 'Array') {
531
+ properties[propKey] = [];
532
+ } else if (propValue.name === 'String') {
533
+ properties[propKey] = '';
534
+ } else if (propValue.name === 'Boolean') {
535
+ properties[propKey] = false;
536
+ } else if (propValue.name === 'Number') {
537
+ properties[propKey] = 0;
578
538
  } else {
579
539
  properties[propKey] = null;
580
540
  }
581
- this._observeProps.push({
582
- name: propKey,
583
- observers: observers
584
- });
541
+ } else if (typeof propValue === 'object') {
542
+ // propValue为对象时
543
+ properties[propKey] = propValue.value;
544
+ if (propValue.observer) {
545
+ observers.push(propValue.observer);
546
+ }
547
+ } else {
548
+ properties[propKey] = null;
585
549
  }
550
+ this._observeProps.push({
551
+ name: propKey,
552
+ observers: observers
553
+ });
586
554
  }
587
- this.state = Object.assign(Object.assign({}, properties), this.state);
588
555
  }
589
- }, {
590
- key: "init",
591
- value: function init(options) {
592
- var _this2 = this;
593
- var _a, _b, _c;
594
- // 处理 Behaviors
595
- if ((_a = options.behaviors) === null || _a === void 0 ? void 0 : _a.length) {
596
- var _iterator = _createForOfIteratorHelper(behaviorMap.entries()),
597
- _step;
598
- try {
599
- var _loop = function _loop() {
600
- var _step$value = _slicedToArray(_step.value, 2),
601
- key = _step$value[0],
602
- list = _step$value[1];
603
- switch (key) {
604
- case 'created':
605
- case 'attached':
606
- case 'detached':
607
- case 'ready':
608
- list.forEach(function (fn) {
609
- return _this2.initLifeCycles(key, fn);
610
- });
611
- break;
612
- }
613
- };
614
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
615
- _loop();
616
- }
617
- } catch (err) {
618
- _iterator.e(err);
619
- } finally {
620
- _iterator.f();
556
+ this.state = Object.assign(Object.assign({}, properties), this.state);
557
+ }
558
+ init(options) {
559
+ var _a, _b, _c;
560
+ // 处理 Behaviors
561
+ if ((_a = options.behaviors) === null || _a === void 0 ? void 0 : _a.length) {
562
+ for (const [key, list] of behaviorMap.entries()) {
563
+ switch (key) {
564
+ case 'created':
565
+ case 'attached':
566
+ case 'detached':
567
+ case 'ready':
568
+ list.forEach(fn => this.initLifeCycles(key, fn));
569
+ break;
621
570
  }
622
571
  }
623
- for (var confKey in options) {
624
- // 不支持的属性
625
- if (nonsupport.has(confKey)) {
626
- var advise = nonsupport.get(confKey);
627
- report(advise);
628
- }
629
- var confValue = options[confKey];
630
- switch (confKey) {
631
- case 'behaviors':
632
- break;
633
- case 'data':
634
- {
635
- if (isApp) {
636
- this[confKey] = confValue;
637
- if (!appOptions.includes(confKey)) {
638
- this.defineProperty(this.taroGlobalData, confKey, this);
639
- }
640
- } else {
641
- this.state = Object.assign(Object.assign({}, confValue), this.state);
572
+ }
573
+ for (const confKey in options) {
574
+ // 不支持的属性
575
+ if (nonsupport.has(confKey)) {
576
+ const advise = nonsupport.get(confKey);
577
+ report(advise);
578
+ }
579
+ const confValue = options[confKey];
580
+ switch (confKey) {
581
+ case 'behaviors':
582
+ break;
583
+ case 'data':
584
+ {
585
+ if (isApp) {
586
+ this[confKey] = confValue;
587
+ if (!appOptions.includes(confKey)) {
588
+ this.defineProperty(this.taroGlobalData, confKey, this);
642
589
  }
643
- break;
590
+ } else {
591
+ this.state = Object.assign(Object.assign({}, confValue), this.state);
644
592
  }
645
- case 'properties':
646
- this.initProps(Object.assign(behaviorProperties, confValue));
647
593
  break;
648
- case 'methods':
649
- for (var key in confValue) {
650
- var method = confValue[key];
651
- this[key] = bind(method, this);
594
+ }
595
+ case 'properties':
596
+ this.initProps(Object.assign(behaviorProperties, confValue));
597
+ break;
598
+ case 'methods':
599
+ for (const key in confValue) {
600
+ const method = confValue[key];
601
+ this[key] = bind(method, this);
602
+ }
603
+ break;
604
+ case 'lifetimes':
605
+ for (const key in confValue) {
606
+ this.initLifeCycles(key, confValue[key]);
607
+ }
608
+ break;
609
+ case 'pageLifetimes':
610
+ for (const key in confValue) {
611
+ const cb = confValue[key];
612
+ switch (key) {
613
+ case 'show':
614
+ {
615
+ this.initLifeCycleListener('show', cb);
616
+ break;
617
+ }
618
+ case 'hide':
619
+ {
620
+ this.initLifeCycleListener('hide', cb);
621
+ break;
622
+ }
623
+ case 'resize':
624
+ {
625
+ report('不支持组件所在页面的生命周期 pageLifetimes.resize。');
626
+ break;
627
+ }
652
628
  }
653
- break;
654
- case 'lifetimes':
655
- for (var _key2 in confValue) {
656
- this.initLifeCycles(_key2, confValue[_key2]);
629
+ }
630
+ break;
631
+ default:
632
+ if (lifecycles.has(confKey)) {
633
+ // 优先使用 lifetimes 中定义的生命周期
634
+ if ((_b = options.lifetimes) === null || _b === void 0 ? void 0 : _b[confKey]) {
635
+ break;
657
636
  }
658
- break;
659
- case 'pageLifetimes':
660
- for (var _key3 in confValue) {
661
- var cb = confValue[_key3];
662
- switch (_key3) {
663
- case 'show':
664
- {
665
- this.initLifeCycleListener('show', cb);
666
- break;
667
- }
668
- case 'hide':
669
- {
670
- this.initLifeCycleListener('hide', cb);
671
- break;
672
- }
673
- case 'resize':
674
- {
675
- report('不支持组件所在页面的生命周期 pageLifetimes.resize。');
676
- break;
677
- }
678
- }
637
+ const lifecycle = options[confKey];
638
+ this.initLifeCycles(confKey, lifecycle);
639
+ } else if (isFunction(confValue)) {
640
+ this[confKey] = bind(confValue, this);
641
+ if (isApp && !appOptions.includes(confKey)) {
642
+ this.defineProperty(this.taroGlobalData, confKey, this);
679
643
  }
680
- break;
681
- default:
682
- if (lifecycles.has(confKey)) {
683
- // 优先使用 lifetimes 中定义的生命周期
684
- if ((_b = options.lifetimes) === null || _b === void 0 ? void 0 : _b[confKey]) {
685
- break;
686
- }
687
- var lifecycle = options[confKey];
688
- this.initLifeCycles(confKey, lifecycle);
689
- } else if (isFunction(confValue)) {
690
- this[confKey] = bind(confValue, this);
691
- if (isApp && !appOptions.includes(confKey)) {
692
- this.defineProperty(this.taroGlobalData, confKey, this);
693
- }
694
- // 原生页面和 Taro 页面中共计只能定义一次的生命周期
695
- if (uniquePageLifecycle.includes(confKey) && ConnectComponent.prototype[confKey]) {
696
- report("\u751F\u547D\u5468\u671F ".concat(confKey, " \u5DF2\u5728\u539F\u751F\u90E8\u5206\u8FDB\u884C\u5B9A\u4E49\uFF0CReact \u90E8\u5206\u7684\u5B9A\u4E49\u5C06\u4E0D\u4F1A\u88AB\u6267\u884C\u3002"));
697
- }
698
- } else {
699
- this[confKey] = confValue;
700
- if (isApp && !appOptions.includes(confKey)) {
701
- this.defineProperty(this.taroGlobalData, confKey, this);
702
- }
644
+ // 原生页面和 Taro 页面中共计只能定义一次的生命周期
645
+ if (uniquePageLifecycle.includes(confKey) && ConnectComponent.prototype[confKey]) {
646
+ report(`生命周期 ${confKey} 已在原生部分进行定义,React 部分的定义将不会被执行。`);
703
647
  }
704
- break;
705
- }
706
- }
707
- // 处理 Behaviors
708
- if ((_c = options.behaviors) === null || _c === void 0 ? void 0 : _c.length) {
709
- var behaviorData = {};
710
- var methods = {};
711
- var _iterator2 = _createForOfIteratorHelper(behaviorMap.entries()),
712
- _step2;
713
- try {
714
- var _loop2 = function _loop2() {
715
- var _step2$value = _slicedToArray(_step2.value, 2),
716
- key = _step2$value[0],
717
- list = _step2$value[1];
718
- switch (key) {
719
- case 'data':
720
- [].concat(_toConsumableArray(list), [_this2.state]).forEach(function (dataObject, index) {
721
- Object.keys(dataObject).forEach(function (dataKey) {
722
- var value = dataObject[dataKey];
723
- var preValue = behaviorData[dataKey];
724
- var valueType = _typeof(value);
725
- var preValueType = _typeof(preValue);
726
- if (valueType === 'object') {
727
- if (!value) {
728
- behaviorData[dataKey] = value;
729
- } else if (preValueType !== 'object' || !preValueType || Array.isArray(value)) {
730
- behaviorData[dataKey] = index === list.length ? value : clone(value);
731
- } else {
732
- var newVal = Object.assign({}, preValue, value);
733
- behaviorData[dataKey] = index === list.length ? newVal : clone(newVal);
734
- }
735
- } else {
736
- behaviorData[dataKey] = value;
737
- }
738
- });
739
- });
740
- _this2.state = behaviorData;
741
- break;
742
- case 'methods':
743
- list.forEach(function (methodsObject) {
744
- Object.assign(methods, methodsObject);
745
- });
746
- Object.keys(methods).forEach(function (methodName) {
747
- if (!_this2[methodName]) {
748
- var _method = methods[methodName];
749
- _this2[methodName] = bind(_method, _this2);
750
- }
751
- });
752
- break;
753
- case 'lifetimes':
754
- list.forEach(function (lifetimesObject) {
755
- for (var _key4 in lifetimesObject) {
756
- _this2.initLifeCycles(_key4, lifetimesObject[_key4]);
757
- }
758
- });
759
- break;
760
- default:
761
- break;
648
+ } else {
649
+ this[confKey] = confValue;
650
+ if (isApp && !appOptions.includes(confKey)) {
651
+ this.defineProperty(this.taroGlobalData, confKey, this);
762
652
  }
763
- };
764
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
765
- _loop2();
766
653
  }
767
- } catch (err) {
768
- _iterator2.e(err);
769
- } finally {
770
- _iterator2.f();
771
- }
654
+ break;
772
655
  }
773
656
  }
774
- }, {
775
- key: "initLifeCycles",
776
- value: function initLifeCycles(lifecycleName, lifecycle) {
777
- // 不支持的生命周期
778
- if (nonsupport.has(lifecycleName)) {
779
- var advise = nonsupport.get(lifecycleName);
780
- return report(advise);
781
- }
782
- if (lifecycleName === 'ready') {
783
- // 如果组件是延时渲染的,页面 onReady 的事件已经 emit 了,因此使用 componentDidMount + nextTick 模拟
784
- if (this.current.page.onReady.called) {
785
- this.didMounts.push(function () {
786
- var _this3 = this;
787
- for (var _len2 = arguments.length, args = new Array(_len2), _key5 = 0; _key5 < _len2; _key5++) {
788
- args[_key5] = arguments[_key5];
789
- }
790
- nextTick(function () {
791
- if (isFunction(lifecycle)) {
792
- lifecycle.apply(_this3, args);
657
+ // 处理 Behaviors
658
+ if ((_c = options.behaviors) === null || _c === void 0 ? void 0 : _c.length) {
659
+ const behaviorData = {};
660
+ const methods = {};
661
+ for (const [key, list] of behaviorMap.entries()) {
662
+ switch (key) {
663
+ case 'data':
664
+ [...list, this.state].forEach((dataObject, index) => {
665
+ Object.keys(dataObject).forEach(dataKey => {
666
+ const value = dataObject[dataKey];
667
+ const preValue = behaviorData[dataKey];
668
+ const valueType = typeof value;
669
+ const preValueType = typeof preValue;
670
+ if (valueType === 'object') {
671
+ if (!value) {
672
+ behaviorData[dataKey] = value;
673
+ } else if (preValueType !== 'object' || !preValueType || Array.isArray(value)) {
674
+ behaviorData[dataKey] = index === list.length ? value : clone(value);
675
+ } else {
676
+ const newVal = Object.assign({}, preValue, value);
677
+ behaviorData[dataKey] = index === list.length ? newVal : clone(newVal);
678
+ }
679
+ } else {
680
+ behaviorData[dataKey] = value;
681
+ }
682
+ });
683
+ });
684
+ this.state = behaviorData;
685
+ break;
686
+ case 'methods':
687
+ list.forEach(methodsObject => {
688
+ Object.assign(methods, methodsObject);
689
+ });
690
+ Object.keys(methods).forEach(methodName => {
691
+ if (!this[methodName]) {
692
+ const method = methods[methodName];
693
+ this[methodName] = bind(method, this);
793
694
  }
794
695
  });
795
- });
796
- } else {
797
- this.initLifeCycleListener('ready', lifecycle);
798
- }
799
- } else {
800
- for (var lifecycleKey in lifecycleMap) {
801
- var cycleNames = lifecycleMap[lifecycleKey];
802
- if (cycleNames.indexOf(lifecycleName) !== -1) {
803
- switch (lifecycleKey) {
804
- case TaroLifeCycles.DidHide:
805
- this.didHides.push(lifecycle);
806
- break;
807
- case TaroLifeCycles.DidMount:
808
- this.didMounts.push(lifecycle);
809
- break;
810
- case TaroLifeCycles.DidShow:
811
- this.didShows.push(lifecycle);
812
- break;
813
- case TaroLifeCycles.WillMount:
814
- this.willMounts.push(lifecycle);
815
- break;
816
- case TaroLifeCycles.WillUnmount:
817
- this.willUnmounts.push(lifecycle);
818
- break;
819
- }
820
- }
696
+ break;
697
+ case 'lifetimes':
698
+ list.forEach(lifetimesObject => {
699
+ for (const key in lifetimesObject) {
700
+ this.initLifeCycles(key, lifetimesObject[key]);
701
+ }
702
+ });
703
+ break;
821
704
  }
822
705
  }
823
- // mixins 不会覆盖已经设置的生命周期,加入到 this 是为了形如 this.created() 的调用
824
- if (!isFunction(this[lifecycleName])) {
825
- this[lifecycleName] = lifecycle;
826
- }
827
706
  }
828
- }, {
829
- key: "initLifeCycleListener",
830
- value: function initLifeCycleListener(name, cb) {
831
- // 组件的 ready、show、hide 需要利用页面事件触发
832
- var router = this.current.router;
833
- var lifecycleName = "on".concat(name[0].toUpperCase()).concat(name.slice(1));
834
- cb = cb.bind(this);
835
- (router === null || router === void 0 ? void 0 : router[lifecycleName]) && eventCenter.on(router[lifecycleName], cb);
836
- // unMount 时需要取消事件监听
837
- this.eventDestroyList.push(function () {
838
- return eventCenter.off(router[lifecycleName], cb);
839
- });
840
- }
841
- }, {
842
- key: "executeLifeCycles",
843
- value: function executeLifeCycles(funcs) {
844
- for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key6 = 1; _key6 < _len3; _key6++) {
845
- args[_key6 - 1] = arguments[_key6];
846
- }
847
- for (var i = 0; i < funcs.length; i++) {
848
- var func = funcs[i];
849
- this.safeExecute.apply(this, [func].concat(args));
850
- }
707
+ }
708
+ initLifeCycles(lifecycleName, lifecycle) {
709
+ // 不支持的生命周期
710
+ if (nonsupport.has(lifecycleName)) {
711
+ const advise = nonsupport.get(lifecycleName);
712
+ return report(advise);
851
713
  }
852
- }, {
853
- key: "triggerPropertiesObservers",
854
- value: function triggerPropertiesObservers(prevProps, nextProps) {
855
- var _this4 = this;
856
- this._observeProps.forEach(function (_ref) {
857
- var key = _ref.name,
858
- observers = _ref.observers;
859
- var prop = prevProps === null || prevProps === void 0 ? void 0 : prevProps[key];
860
- var nextProp = nextProps[key];
861
- // 小程序是深比较不同之后才 trigger observer
862
- if (!isEqual(prop, nextProp)) {
863
- observers.forEach(function (observer) {
864
- if (typeof observer === 'string') {
865
- var ob = _this4[observer];
866
- if (isFunction(ob)) {
867
- ob.call(_this4, nextProp, prop, key);
868
- }
869
- } else if (isFunction(observer)) {
870
- observer.call(_this4, nextProp, prop, key);
714
+ if (lifecycleName === 'ready') {
715
+ // 如果组件是延时渲染的,页面 onReady 的事件已经 emit 了,因此使用 componentDidMount + nextTick 模拟
716
+ if (this.current.page.onReady.called) {
717
+ this.didMounts.push(function (...args) {
718
+ nextTick(() => {
719
+ if (isFunction(lifecycle)) {
720
+ lifecycle.apply(this, args);
871
721
  }
872
722
  });
873
- }
874
- });
875
- }
876
- }, {
877
- key: "triggerObservers",
878
- value: function triggerObservers(current, prev) {
879
- var observers = this.observers;
880
- if (observers == null) {
881
- return;
882
- }
883
- if (Object.keys(observers).length === 0) {
884
- return;
885
- }
886
- var result = diff(current, prev);
887
- var resultKeys = Object.keys(result);
888
- if (resultKeys.length === 0) {
889
- return;
890
- }
891
- for (var observerKey in observers) {
892
- if (/\*\*/.test(observerKey)) {
893
- report('数据监听器 observers 不支持使用通配符 **。');
894
- continue;
895
- }
896
- var keys = observerKey.split(',').map(function (k) {
897
- return k.trim();
898
723
  });
899
- var isModified = false;
900
- for (var i = 0; i < keys.length; i++) {
901
- var key = keys[i];
902
- for (var j = 0; j < resultKeys.length; j++) {
903
- var resultKey = resultKeys[j];
904
- if (resultKey.startsWith(key) || key.startsWith(resultKey) && key.endsWith(']')) {
905
- isModified = true;
906
- }
907
- }
908
- }
909
- if (isModified) {
910
- observers[observerKey].apply(this, keys.map(function (key) {
911
- return safeGet(current, key);
912
- }));
913
- }
724
+ } else {
725
+ this.initLifeCycleListener('ready', lifecycle);
914
726
  }
915
- }
916
- }, {
917
- key: "defineProperty",
918
- value: function defineProperty(target, key, data) {
919
- Object.defineProperty(target, key, {
920
- configurable: true,
921
- enumerable: true,
922
- set: function set(value) {
923
- data[key] = value;
924
- },
925
- get: function get() {
926
- return data[key];
727
+ } else {
728
+ for (const lifecycleKey in lifecycleMap) {
729
+ const cycleNames = lifecycleMap[lifecycleKey];
730
+ if (cycleNames.indexOf(lifecycleName) !== -1) {
731
+ switch (lifecycleKey) {
732
+ case TaroLifeCycles.DidHide:
733
+ this.didHides.push(lifecycle);
734
+ break;
735
+ case TaroLifeCycles.DidMount:
736
+ this.didMounts.push(lifecycle);
737
+ break;
738
+ case TaroLifeCycles.DidShow:
739
+ this.didShows.push(lifecycle);
740
+ break;
741
+ case TaroLifeCycles.WillMount:
742
+ this.willMounts.push(lifecycle);
743
+ break;
744
+ case TaroLifeCycles.WillUnmount:
745
+ this.willUnmounts.push(lifecycle);
746
+ break;
747
+ }
927
748
  }
928
- });
929
- }
930
- }, {
931
- key: "privateStopNoop",
932
- value: function privateStopNoop() {
933
- var e;
934
- var fn;
935
- if (arguments.length === 2) {
936
- fn = arguments.length <= 0 ? undefined : arguments[0];
937
- e = arguments.length <= 1 ? undefined : arguments[1];
938
- } else if (arguments.length === 1) {
939
- e = arguments.length <= 0 ? undefined : arguments[0];
940
- }
941
- if (e.type === 'touchmove') {
942
- report('catchtouchmove 转换后只能停止回调函数的冒泡,不能阻止滚动穿透。如要阻止滚动穿透,可以手动给编译后的 View 组件加上 catchMove 属性');
943
749
  }
944
- e.stopPropagation();
945
- isFunction(fn) && fn(e);
946
- }
947
- // ================ React 生命周期 ================
948
- }, {
949
- key: "componentWillMount",
950
- value: function componentWillMount() {
951
- this.safeExecute(_get(_getPrototypeOf(BaseComponent.prototype), "componentWillMount", this));
952
- this.executeLifeCycles(this.willMounts, this.current.router || {});
953
- this.triggerObservers(this.data, BaseComponent.defaultProps);
954
- this.triggerPropertiesObservers(BaseComponent.defaultProps, this.props);
955
- }
956
- }, {
957
- key: "componentDidMount",
958
- value: function componentDidMount() {
959
- this.safeExecute(_get(_getPrototypeOf(BaseComponent.prototype), "componentDidMount", this));
960
- this.executeLifeCycles(this.didMounts);
961
- }
962
- }, {
963
- key: "componentWillUnmount",
964
- value: function componentWillUnmount() {
965
- this.eventDestroyList.forEach(function (fn) {
966
- return fn();
967
- });
968
- this.safeExecute(_get(_getPrototypeOf(BaseComponent.prototype), "componentWillUnmount", this));
969
- this.executeLifeCycles(this.willUnmounts);
970
- }
971
- }, {
972
- key: "componentDidHide",
973
- value: function componentDidHide() {
974
- this.safeExecute(_get(_getPrototypeOf(BaseComponent.prototype), "componentDidHide", this));
975
- this.executeLifeCycles(this.didHides);
976
750
  }
977
- }, {
978
- key: "componentDidShow",
979
- value: function componentDidShow() {
980
- this.safeExecute(_get(_getPrototypeOf(BaseComponent.prototype), "componentDidShow", this), this.current.router || {});
981
- this.executeLifeCycles(this.didShows, this.current.router || {});
751
+ // mixins 不会覆盖已经设置的生命周期,加入到 this 是为了形如 this.created() 的调用
752
+ if (!isFunction(this[lifecycleName])) {
753
+ this[lifecycleName] = lifecycle;
982
754
  }
983
- }, {
984
- key: "componentWillReceiveProps",
985
- value: function componentWillReceiveProps(nextProps) {
986
- this.triggerObservers(nextProps, this.props);
987
- this.triggerPropertiesObservers(this.props, nextProps);
988
- this.safeExecute(_get(_getPrototypeOf(BaseComponent.prototype), "componentWillReceiveProps", this));
755
+ }
756
+ initLifeCycleListener(name, cb) {
757
+ // 组件的 ready、show、hide 需要利用页面事件触发
758
+ const {
759
+ router
760
+ } = this.current;
761
+ const lifecycleName = `on${name[0].toUpperCase()}${name.slice(1)}`;
762
+ cb = cb.bind(this);
763
+ (router === null || router === void 0 ? void 0 : router[lifecycleName]) && eventCenter.on(router[lifecycleName], cb);
764
+ // unMount 时需要取消事件监听
765
+ this.eventDestroyList.push(() => eventCenter.off(router[lifecycleName], cb));
766
+ }
767
+ executeLifeCycles(funcs, ...args) {
768
+ for (let i = 0; i < funcs.length; i++) {
769
+ const func = funcs[i];
770
+ this.safeExecute(func, ...args);
989
771
  }
990
- // ================ 小程序 App, Page, Component 实例属性与方法 ================
991
- }, {
992
- key: "is",
993
- get: function get() {
994
- return this.current.page.is;
772
+ }
773
+ triggerPropertiesObservers(prevProps, nextProps) {
774
+ this._observeProps.forEach(({
775
+ name: key,
776
+ observers
777
+ }) => {
778
+ const prop = prevProps === null || prevProps === void 0 ? void 0 : prevProps[key];
779
+ const nextProp = nextProps[key];
780
+ // 小程序是深比较不同之后才 trigger observer
781
+ if (!isEqual(prop, nextProp)) {
782
+ observers.forEach(observer => {
783
+ if (typeof observer === 'string') {
784
+ const ob = this[observer];
785
+ if (isFunction(ob)) {
786
+ ob.call(this, nextProp, prop, key);
787
+ }
788
+ } else if (isFunction(observer)) {
789
+ observer.call(this, nextProp, prop, key);
790
+ }
791
+ });
792
+ }
793
+ });
794
+ }
795
+ triggerObservers(current, prev) {
796
+ const observers = this.observers;
797
+ if (observers == null) {
798
+ return;
995
799
  }
996
- }, {
997
- key: "id",
998
- get: function get() {
999
- return this.current.page.id;
800
+ if (Object.keys(observers).length === 0) {
801
+ return;
1000
802
  }
1001
- }, {
1002
- key: "dataset",
1003
- get: function get() {
1004
- return this.current.page.dataset;
803
+ const result = diff(current, prev);
804
+ const resultKeys = Object.keys(result);
805
+ if (resultKeys.length === 0) {
806
+ return;
1005
807
  }
1006
- }, {
1007
- key: "componentMethodsProxy",
1008
- value: function componentMethodsProxy(method) {
1009
- var _this5 = this;
1010
- return function () {
1011
- var page = _this5.current.page;
1012
- if (page === null || page === void 0 ? void 0 : page[method]) {
1013
- return page[method].apply(page, arguments);
1014
- } else if (method === 'createSelectorQuery') {
1015
- return createSelectorQuery();
1016
- } else if (method === 'createIntersectionObserver') {
1017
- // @ts-ignore
1018
- return createIntersectionObserver.apply(void 0, arguments);
1019
- } else if (method === 'createMediaQueryObserver') {
1020
- return createMediaQueryObserver();
1021
- } else {
1022
- console.error("page \u4E0B\u6CA1\u6709 ".concat(method, " \u65B9\u6CD5"));
808
+ for (const observerKey in observers) {
809
+ if (/\*\*/.test(observerKey)) {
810
+ report('数据监听器 observers 不支持使用通配符 **。');
811
+ continue;
812
+ }
813
+ const keys = observerKey.split(',').map(k => k.trim());
814
+ let isModified = false;
815
+ for (let i = 0; i < keys.length; i++) {
816
+ const key = keys[i];
817
+ for (let j = 0; j < resultKeys.length; j++) {
818
+ const resultKey = resultKeys[j];
819
+ if (resultKey.startsWith(key) || key.startsWith(resultKey) && key.endsWith(']')) {
820
+ isModified = true;
821
+ }
1023
822
  }
1024
- };
1025
- }
1026
- }, {
1027
- key: "selectComponent",
1028
- value: function selectComponent() {
1029
- report(nonsupport.get('selectComponent'));
1030
- }
1031
- }, {
1032
- key: "selectAllComponents",
1033
- value: function selectAllComponents() {
1034
- report(nonsupport.get('selectAllComponents'));
823
+ }
824
+ if (isModified) {
825
+ observers[observerKey].apply(this, keys.map(key => safeGet(current, key)));
826
+ }
1035
827
  }
1036
- }, {
1037
- key: "selectOwnerComponent",
1038
- value: function selectOwnerComponent() {
1039
- report(nonsupport.get('selectOwnerComponent'));
828
+ }
829
+ defineProperty(target, key, data) {
830
+ Object.defineProperty(target, key, {
831
+ configurable: true,
832
+ enumerable: true,
833
+ set(value) {
834
+ data[key] = value;
835
+ },
836
+ get() {
837
+ return data[key];
838
+ }
839
+ });
840
+ }
841
+ privateStopNoop(...args) {
842
+ let e;
843
+ let fn;
844
+ if (args.length === 2) {
845
+ fn = args[0];
846
+ e = args[1];
847
+ } else if (args.length === 1) {
848
+ e = args[0];
1040
849
  }
1041
- }, {
1042
- key: "groupSetData",
1043
- value: function groupSetData() {
1044
- report(nonsupport.get('groupSetData'));
850
+ if (e.type === 'touchmove') {
851
+ report('catchtouchmove 转换后只能停止回调函数的冒泡,不能阻止滚动穿透。如要阻止滚动穿透,可以手动给编译后的 View 组件加上 catchMove 属性');
1045
852
  }
1046
- }]);
1047
- return BaseComponent;
1048
- }(ConnectComponent);
1049
- var props = weappConf.properties;
853
+ e.stopPropagation();
854
+ isFunction(fn) && fn(e);
855
+ }
856
+ // ================ React 生命周期 ================
857
+ componentWillMount() {
858
+ this.safeExecute(super.componentWillMount);
859
+ this.executeLifeCycles(this.willMounts, this.current.router || {});
860
+ this.triggerObservers(this.data, BaseComponent.defaultProps);
861
+ this.triggerPropertiesObservers(BaseComponent.defaultProps, this.props);
862
+ }
863
+ componentDidMount() {
864
+ this.safeExecute(super.componentDidMount);
865
+ this.executeLifeCycles(this.didMounts);
866
+ }
867
+ componentWillUnmount() {
868
+ this.eventDestroyList.forEach(fn => fn());
869
+ this.safeExecute(super.componentWillUnmount);
870
+ this.executeLifeCycles(this.willUnmounts);
871
+ }
872
+ componentDidHide() {
873
+ this.safeExecute(super.componentDidHide);
874
+ this.executeLifeCycles(this.didHides);
875
+ }
876
+ componentDidShow() {
877
+ this.safeExecute(super.componentDidShow, this.current.router || {});
878
+ this.executeLifeCycles(this.didShows, this.current.router || {});
879
+ }
880
+ componentWillReceiveProps(nextProps) {
881
+ this.triggerObservers(nextProps, this.props);
882
+ this.triggerPropertiesObservers(this.props, nextProps);
883
+ this.safeExecute(super.componentWillReceiveProps);
884
+ }
885
+ // ================ 小程序 App, Page, Component 实例属性与方法 ================
886
+ get is() {
887
+ return this.current.page.is;
888
+ }
889
+ get id() {
890
+ return this.current.page.id;
891
+ }
892
+ get dataset() {
893
+ return this.current.page.dataset;
894
+ }
895
+ componentMethodsProxy(method) {
896
+ return (...args) => {
897
+ const page = this.current.page;
898
+ if (page === null || page === void 0 ? void 0 : page[method]) {
899
+ return page[method](...args);
900
+ } else if (method === 'createSelectorQuery') {
901
+ return createSelectorQuery();
902
+ } else if (method === 'createIntersectionObserver') {
903
+ // @ts-ignore
904
+ return createIntersectionObserver(...args);
905
+ } else if (method === 'createMediaQueryObserver') {
906
+ return createMediaQueryObserver();
907
+ } else {
908
+ console.error(`page 下没有 ${method} 方法`);
909
+ }
910
+ };
911
+ }
912
+ selectComponent() {
913
+ report(nonsupport.get('selectComponent'));
914
+ }
915
+ selectAllComponents() {
916
+ report(nonsupport.get('selectAllComponents'));
917
+ }
918
+ selectOwnerComponent() {
919
+ report(nonsupport.get('selectOwnerComponent'));
920
+ }
921
+ groupSetData() {
922
+ report(nonsupport.get('groupSetData'));
923
+ }
924
+ }
925
+ const props = weappConf.properties;
1050
926
  if (props) {
1051
- for (var propKey in props) {
1052
- var propValue = props[propKey];
927
+ for (const propKey in props) {
928
+ const propValue = props[propKey];
1053
929
  if (propValue != null && !isFunction(propValue)) {
1054
930
  if (propValue.value !== undefined) {
1055
931
  // 如果是 null 也赋值到 defaultProps
1056
- BaseComponent.defaultProps = Object.assign(_defineProperty({}, propKey, propValue.value), BaseComponent.defaultProps);
932
+ BaseComponent.defaultProps = Object.assign({
933
+ [propKey]: propValue.value
934
+ }, BaseComponent.defaultProps);
1057
935
  }
1058
936
  }
1059
937
  }
1060
938
  }
1061
- var staticOptions = ['externalClasses', 'relations', 'options'];
1062
- staticOptions.forEach(function (option) {
1063
- var value = weappConf[option];
939
+ const staticOptions = ['externalClasses', 'relations', 'options'];
940
+ staticOptions.forEach(option => {
941
+ const value = weappConf[option];
1064
942
  if (value != null) {
1065
943
  BaseComponent[option] = value;
1066
944
  }