@tarojs/with-weapp 3.6.0-beta.2 → 3.6.0-beta.4

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.js CHANGED
@@ -34,19 +34,25 @@ var json = JSON;
34
34
  var clone = function clone(obj) {
35
35
  return json.parse(stringify(obj));
36
36
  };
37
+
37
38
  var isArray = Array.isArray || function (x) {
38
39
  return {}.toString.call(x) === '[object Array]';
39
40
  };
41
+
40
42
  var objectKeys = Object.keys || function (obj) {
41
43
  var has = Object.prototype.hasOwnProperty || function () {
42
44
  return true;
43
45
  };
46
+
44
47
  var keys = [];
48
+
45
49
  for (var key in obj) {
46
50
  if (has.call(obj, key)) keys.push(key);
47
51
  }
52
+
48
53
  return keys;
49
54
  };
55
+
50
56
  function stringify(obj, opts) {
51
57
  if (!opts) opts = {};
52
58
  if (typeof opts === 'function') opts = {
@@ -55,9 +61,11 @@ function stringify(obj, opts) {
55
61
  var space = opts.space || '';
56
62
  if (typeof space === 'number') space = Array(space + 1).join(' ');
57
63
  var cycles = typeof opts.cycles === 'boolean' ? opts.cycles : false;
64
+
58
65
  var replacer = opts.replacer || function (key, value) {
59
66
  return value;
60
67
  };
68
+
61
69
  var cmp = opts.cmp && function (f) {
62
70
  return function (node) {
63
71
  return function (a, b) {
@@ -73,41 +81,53 @@ function stringify(obj, opts) {
73
81
  };
74
82
  };
75
83
  }(opts.cmp);
84
+
76
85
  var seen = [];
77
86
  return function stringify(parent, key, node, level) {
78
87
  var indent = space ? '\n' + new Array(level + 1).join(space) : '';
79
88
  var colonSeparator = space ? ': ' : ':';
89
+
80
90
  if (node && node.toJSON && typeof node.toJSON === 'function') {
81
91
  node = node.toJSON();
82
92
  }
93
+
83
94
  node = replacer.call(parent, key, node);
95
+
84
96
  if (node === undefined) {
85
97
  return;
86
98
  }
99
+
87
100
  if (_typeof__default["default"](node) !== 'object' || node === null) {
88
101
  return json.stringify(node);
89
102
  }
103
+
90
104
  if (isArray(node)) {
91
105
  var out = [];
106
+
92
107
  for (var i = 0; i < node.length; i++) {
93
108
  var item = stringify(node, i, node[i], level + 1) || json.stringify(null);
94
109
  out.push(indent + space + item);
95
110
  }
111
+
96
112
  return '[' + out.join(',') + indent + ']';
97
113
  } else {
98
114
  if (seen.indexOf(node) !== -1) {
99
115
  if (cycles) return json.stringify('__cycle__');
100
116
  throw new TypeError('Converting circular structure to JSON');
101
117
  } else seen.push(node);
118
+
102
119
  var keys = objectKeys(node).sort(cmp && cmp(node));
103
120
  var _out = [];
121
+
104
122
  for (var _i = 0; _i < keys.length; _i++) {
105
123
  var _key = keys[_i];
106
124
  var value = stringify(node, _key, node[_key], level + 1);
107
125
  if (!value) continue;
108
126
  var keyValue = json.stringify(_key) + colonSeparator + value;
127
+
109
128
  _out.push(indent + space + keyValue);
110
129
  }
130
+
111
131
  seen.splice(seen.indexOf(node), 1);
112
132
  return '{' + _out.join(',') + indent + '}';
113
133
  }
@@ -123,24 +143,29 @@ var FUNCTIONTYPE = '[object Function]';
123
143
  function diff(current, pre) {
124
144
  var result = {};
125
145
  syncKeys(current, pre);
146
+
126
147
  _diff(current, pre, '', result);
148
+
127
149
  return result;
128
150
  }
151
+
129
152
  function syncKeys(current, pre) {
130
153
  if (current === pre) return;
131
154
  var rootCurrentType = type(current);
132
155
  var rootPreType = type(pre);
156
+
133
157
  if (rootCurrentType == OBJECTTYPE && rootPreType == OBJECTTYPE) {
134
158
  // if(Object.keys(current).length >= Object.keys(pre).length){
135
159
  for (var key in pre) {
136
160
  var currentValue = current[key];
161
+
137
162
  if (currentValue === undefined) {
138
163
  current[key] = null;
139
164
  } else {
140
165
  syncKeys(currentValue, pre[key]);
141
166
  }
142
- }
143
- // }
167
+ } // }
168
+
144
169
  } else if (rootCurrentType == ARRAYTYPE && rootPreType == ARRAYTYPE) {
145
170
  if (current.length >= pre.length) {
146
171
  pre.forEach(function (item, index) {
@@ -149,10 +174,12 @@ function syncKeys(current, pre) {
149
174
  }
150
175
  }
151
176
  }
177
+
152
178
  function _diff(current, pre, path, result) {
153
179
  if (current === pre) return;
154
180
  var rootCurrentType = type(current);
155
181
  var rootPreType = type(pre);
182
+
156
183
  if (rootCurrentType == OBJECTTYPE) {
157
184
  if (rootPreType != OBJECTTYPE || Object.keys(current).length < Object.keys(pre).length) {
158
185
  setResult(result, path, current);
@@ -162,6 +189,7 @@ function _diff(current, pre, path, result) {
162
189
  var preValue = pre[key];
163
190
  var currentType = type(currentValue);
164
191
  var preType = type(preValue);
192
+
165
193
  if (currentType != ARRAYTYPE && currentType != OBJECTTYPE) {
166
194
  if (currentValue != pre[key]) {
167
195
  setResult(result, (path == '' ? '' : path + '.') + key, currentValue);
@@ -188,6 +216,7 @@ function _diff(current, pre, path, result) {
188
216
  }
189
217
  }
190
218
  };
219
+
191
220
  for (var key in current) {
192
221
  _loop(key);
193
222
  }
@@ -208,17 +237,21 @@ function _diff(current, pre, path, result) {
208
237
  setResult(result, path, current);
209
238
  }
210
239
  }
240
+
211
241
  function setResult(result, k, v) {
212
242
  if (type(v) != FUNCTIONTYPE) {
213
243
  result[k] = v;
214
244
  }
215
245
  }
246
+
216
247
  function type(obj) {
217
248
  return Object.prototype.toString.call(obj);
218
249
  }
219
250
 
220
251
  var _lifecycleMap;
252
+
221
253
  var TaroLifeCycles;
254
+
222
255
  (function (TaroLifeCycles) {
223
256
  TaroLifeCycles["WillMount"] = "componentWillMount";
224
257
  TaroLifeCycles["DidMount"] = "componentDidMount";
@@ -226,27 +259,38 @@ var TaroLifeCycles;
226
259
  TaroLifeCycles["DidHide"] = "componentDidHide";
227
260
  TaroLifeCycles["WillUnmount"] = "componentWillUnmount";
228
261
  })(TaroLifeCycles || (TaroLifeCycles = {}));
262
+
229
263
  var lifecycleMap = (_lifecycleMap = {}, _defineProperty__default["default"](_lifecycleMap, TaroLifeCycles.WillMount, ['created']), _defineProperty__default["default"](_lifecycleMap, TaroLifeCycles.DidMount, ['attached']), _defineProperty__default["default"](_lifecycleMap, TaroLifeCycles.DidShow, ['onShow']), _defineProperty__default["default"](_lifecycleMap, TaroLifeCycles.DidHide, ['onHide']), _defineProperty__default["default"](_lifecycleMap, TaroLifeCycles.WillUnmount, ['detached', 'onUnload']), _lifecycleMap);
230
264
  var lifecycles = new Set(['ready']);
265
+
231
266
  for (var key in lifecycleMap) {
232
267
  var lifecycle = lifecycleMap[key];
233
268
  lifecycle.forEach(function (l) {
234
269
  return lifecycles.add(l);
235
270
  });
236
271
  }
272
+
237
273
  var uniquePageLifecycle = ['onPullDownRefresh', 'onReachBottom', 'onShareAppMessage', 'onShareTimeline', 'onAddToFavorites', 'onPageScroll', 'onResize', 'onTabItemTap'];
238
274
  var appOptions = ['onLaunch', 'onShow', 'onHide', 'onError', 'onPageNotFound', 'onUnhandledRejection', 'onThemeChange'];
239
275
 
240
276
  /**
241
277
  * Simple bind, faster than native
242
278
  */
243
- function bind(fn /*: Function */, ctx /*: Object */) {
279
+ function bind(fn
280
+ /*: Function */
281
+ , ctx
282
+ /*: Object */
283
+ ) {
244
284
  if (!fn) return false;
285
+
245
286
  function boundFn(a) {
246
- var l /*: number */ = arguments.length;
287
+ var l
288
+ /*: number */
289
+ = arguments.length;
247
290
  return l ? l > 1 ? fn.apply(ctx, arguments) : fn.call(ctx, a) : fn.call(ctx);
248
- }
249
- // record original fn length
291
+ } // record original fn length
292
+
293
+
250
294
  boundFn._length = fn.length;
251
295
  return boundFn;
252
296
  }
@@ -257,30 +301,40 @@ function safeGet(obj, propsArg, defaultValue) {
257
301
  if (!obj) {
258
302
  return defaultValue;
259
303
  }
304
+
260
305
  var props, prop;
306
+
261
307
  if (Array.isArray(propsArg)) {
262
308
  props = propsArg.slice(0);
263
309
  }
310
+
264
311
  if (typeof propsArg === 'string') {
265
312
  props = propsArg.replace(/\[(.+?)\]/g, '.$1');
266
313
  props = props.split('.');
267
314
  }
315
+
268
316
  if (_typeof__default["default"](propsArg) === 'symbol') {
269
317
  props = [propsArg];
270
318
  }
319
+
271
320
  if (!Array.isArray(props)) {
272
321
  throw new Error('props arg must be an array, a string or a symbol');
273
322
  }
323
+
274
324
  while (props.length) {
275
325
  prop = props.shift();
326
+
276
327
  if (!obj) {
277
328
  return defaultValue;
278
329
  }
330
+
279
331
  obj = obj[prop];
332
+
280
333
  if (obj === undefined) {
281
334
  return defaultValue;
282
335
  }
283
336
  }
337
+
284
338
  return obj;
285
339
  }
286
340
  function safeSet(obj, props, value) {
@@ -288,30 +342,39 @@ function safeSet(obj, props, value) {
288
342
  props = props.replace(/\[(.+?)\]/g, '.$1');
289
343
  props = props.split('.');
290
344
  }
345
+
291
346
  if (_typeof__default["default"](props) === 'symbol') {
292
347
  props = [props];
293
348
  }
349
+
294
350
  var lastProp = props.pop();
351
+
295
352
  if (!lastProp) {
296
353
  return false;
297
354
  }
355
+
298
356
  var thisProp;
357
+
299
358
  while (thisProp = props.shift()) {
300
359
  if (typeof obj[thisProp] === 'undefined') {
301
360
  obj[thisProp] = {};
302
- }
303
- // 直接按路径修改 this.state 可能会导致 nextProps 也被修改
361
+ } // 直接按路径修改 this.state 可能会导致 nextProps 也被修改
304
362
  // 因此按路径寻找时,每一层都复制一遍
363
+
364
+
305
365
  if (Array.isArray(obj[thisProp])) {
306
366
  obj[thisProp] = _toConsumableArray__default["default"](obj[thisProp]);
307
367
  } else if (_typeof__default["default"](obj[thisProp]) === 'object') {
308
368
  obj[thisProp] = Object.assign({}, obj[thisProp]);
309
369
  }
370
+
310
371
  obj = obj[thisProp];
372
+
311
373
  if (!obj || _typeof__default["default"](obj) !== 'object') {
312
374
  return false;
313
375
  }
314
376
  }
377
+
315
378
  obj[lastProp] = value;
316
379
  return true;
317
380
  }
@@ -323,18 +386,22 @@ function flattenBehaviors(behavior, behaviorMap) {
323
386
  if (typeof behavior === 'string') {
324
387
  return report("\u4E0D\u652F\u6301\u4F7F\u7528\u5185\u7F6E Behavior: [".concat(behavior, "]"));
325
388
  }
389
+
326
390
  var subBehaviors = behavior.behaviors;
391
+
327
392
  if (subBehaviors === null || subBehaviors === void 0 ? void 0 : subBehaviors.length) {
328
393
  subBehaviors.forEach(function (subBehavior) {
329
394
  return flattenBehaviors(subBehavior, behaviorMap);
330
395
  });
331
396
  }
397
+
332
398
  Object.keys(behavior).forEach(function (key) {
333
399
  // 不支持的属性
334
400
  if (nonsupport.has(key)) {
335
401
  var advise = nonsupport.get(key);
336
402
  return report(advise);
337
403
  }
404
+
338
405
  if (behaviorMap.has(key)) {
339
406
  var list = behaviorMap.get(key);
340
407
  var value = behavior[key];
@@ -344,10 +411,15 @@ function flattenBehaviors(behavior, behaviorMap) {
344
411
  }
345
412
 
346
413
  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; } } }; }
414
+
347
415
  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); }
416
+
348
417
  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; }
418
+
349
419
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf__default["default"](Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf__default["default"](this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn__default["default"](this, result); }; }
420
+
350
421
  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; } }
422
+
351
423
  function defineGetter(component, key, getter) {
352
424
  Object.defineProperty(component, key, {
353
425
  enumerable: true,
@@ -356,55 +428,71 @@ function defineGetter(component, key, getter) {
356
428
  if (getter === 'props') {
357
429
  return component.props;
358
430
  }
431
+
359
432
  return Object.assign(Object.assign({}, component.state), component.props);
360
433
  }
361
434
  });
362
435
  }
436
+
363
437
  function isFunction(o) {
364
438
  return typeof o === 'function';
365
439
  }
440
+
366
441
  function withWeapp(weappConf) {
367
442
  var isApp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
443
+
368
444
  if (_typeof__default["default"](weappConf) === 'object' && Object.keys(weappConf).length === 0) {
369
445
  report('withWeapp 请传入“App/页面/组件“的配置对象。如果原生写法使用了基类,请将基类组合后的配置对象传入,详情请参考文档。');
370
446
  }
447
+
371
448
  return function (ConnectComponent) {
372
449
  var _a;
450
+
373
451
  var behaviorMap = new Map([['properties', []], ['data', []], ['methods', []], ['created', []], ['attached', []], ['ready', []], ['detached', []]]);
374
452
  var behaviorProperties = {};
453
+
375
454
  if ((_a = weappConf.behaviors) === null || _a === void 0 ? void 0 : _a.length) {
376
455
  var behaviors = weappConf.behaviors;
377
456
  behaviors.forEach(function (behavior) {
378
457
  return flattenBehaviors(behavior, behaviorMap);
379
458
  });
380
459
  var propertiesList = behaviorMap.get('properties');
460
+
381
461
  if (propertiesList.length) {
382
462
  propertiesList.forEach(function (property) {
383
463
  Object.assign(behaviorProperties, property);
384
464
  });
385
465
  Object.keys(behaviorProperties).forEach(function (propName) {
386
466
  var propValue = behaviorProperties[propName];
467
+
387
468
  if (!weappConf.properties) {
388
469
  weappConf.properties = {};
389
470
  }
471
+
390
472
  if (!weappConf.properties.hasOwnProperty(propName)) {
391
473
  if (propValue && _typeof__default["default"](propValue) === 'object' && propValue.value) {
392
474
  propValue.value = clone(propValue.value);
393
475
  }
476
+
394
477
  weappConf.properties[propName] = propValue;
395
478
  }
396
479
  });
397
480
  }
398
481
  }
482
+
399
483
  var BaseComponent = /*#__PURE__*/function (_ConnectComponent) {
400
484
  _inherits__default["default"](BaseComponent, _ConnectComponent);
485
+
401
486
  var _super = _createSuper(BaseComponent);
487
+
402
488
  function BaseComponent(props) {
403
489
  var _this;
490
+
404
491
  _classCallCheck__default["default"](this, BaseComponent);
492
+
405
493
  _this = _super.call(this, props);
406
- _this._observeProps = [];
407
- // mixins 可以多次调用生命周期
494
+ _this._observeProps = []; // mixins 可以多次调用生命周期
495
+
408
496
  _this.willMounts = [];
409
497
  _this.didMounts = [];
410
498
  _this.didHides = [];
@@ -413,44 +501,57 @@ function withWeapp(weappConf) {
413
501
  _this.eventDestroyList = [];
414
502
  _this.current = runtime.getCurrentInstance();
415
503
  _this.taroGlobalData = Object.create(null);
504
+
416
505
  _this.safeExecute = function (func) {
417
506
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
418
507
  args[_key - 1] = arguments[_key];
419
508
  }
509
+
420
510
  if (isFunction(func)) func.apply(_assertThisInitialized__default["default"](_this), args);
421
511
  };
512
+
422
513
  _this.setData = function (obj, callback) {
423
514
  var oldState;
515
+
424
516
  if (_this.observers && Object.keys(Object.keys(_this.observers))) {
425
517
  oldState = clone(_this.state);
426
518
  }
519
+
427
520
  Object.keys(obj).forEach(function (key) {
428
521
  safeSet(_this.state, key, obj[key]);
429
522
  });
523
+
430
524
  _this.setState(_this.state, function () {
431
525
  _this.triggerObservers(_this.state, oldState);
526
+
432
527
  if (callback) {
433
528
  callback.call(_assertThisInitialized__default["default"](_this));
434
529
  }
435
530
  });
436
531
  };
532
+
437
533
  _this.triggerEvent = function (eventName, detail, options) {
438
534
  if (options) {
439
535
  report('triggerEvent 不支持事件选项。');
440
- }
441
- // eventName support kebab case
536
+ } // eventName support kebab case
537
+
538
+
442
539
  if (eventName.match(/[a-z]+-[a-z]+/g)) {
443
540
  eventName = eventName.replace(/-[a-z]/g, function (match) {
444
541
  return match[1].toUpperCase();
445
542
  });
446
543
  }
544
+
447
545
  var props = _this.props;
448
546
  var dataset = {};
547
+
449
548
  for (var key in props) {
450
549
  if (!key.startsWith('data-')) continue;
451
550
  dataset[key.replace(/^data-/, '')] = props[key];
452
551
  }
552
+
453
553
  var func = props["on".concat(eventName[0].toUpperCase()).concat(eventName.slice(1))];
554
+
454
555
  if (isFunction(func)) {
455
556
  func.call(_assertThisInitialized__default["default"](_this), {
456
557
  type: eventName,
@@ -466,6 +567,7 @@ function withWeapp(weappConf) {
466
567
  });
467
568
  }
468
569
  };
570
+
469
571
  _this.hasBehavior = _this.componentMethodsProxy('hasBehavior');
470
572
  _this.createSelectorQuery = _this.componentMethodsProxy('createSelectorQuery');
471
573
  _this.createIntersectionObserver = _this.componentMethodsProxy('createIntersectionObserver');
@@ -477,18 +579,21 @@ function withWeapp(weappConf) {
477
579
  _this.clearAnimation = _this.componentMethodsProxy('clearAnimation');
478
580
  _this.setUpdatePerformanceListener = _this.componentMethodsProxy('setUpdatePerformanceListener');
479
581
  _this.state = _this.state || {};
582
+
480
583
  _this.init(weappConf);
584
+
481
585
  defineGetter(_assertThisInitialized__default["default"](_this), 'data', 'state');
482
586
  defineGetter(_assertThisInitialized__default["default"](_this), 'properties', 'props');
483
587
  return _this;
484
588
  }
589
+
485
590
  _createClass__default["default"](BaseComponent, [{
486
591
  key: "initProps",
487
592
  value: function initProps(props) {
488
593
  for (var propKey in props) {
489
594
  if (props.hasOwnProperty(propKey)) {
490
- var propValue = props[propKey];
491
- // propValue 可能是 null, 构造函数, 对象
595
+ var propValue = props[propKey]; // propValue 可能是 null, 构造函数, 对象
596
+
492
597
  if (propValue && !isFunction(propValue)) {
493
598
  if (propValue.observer) {
494
599
  this._observeProps.push({
@@ -504,16 +609,20 @@ function withWeapp(weappConf) {
504
609
  key: "init",
505
610
  value: function init(options) {
506
611
  var _this2 = this;
507
- var _a, _b, _c;
508
- // 处理 Behaviors
612
+
613
+ var _a, _b, _c; // 处理 Behaviors
614
+
615
+
509
616
  if ((_a = options.behaviors) === null || _a === void 0 ? void 0 : _a.length) {
510
617
  var _iterator = _createForOfIteratorHelper(behaviorMap.entries()),
511
- _step;
618
+ _step;
619
+
512
620
  try {
513
621
  var _loop = function _loop() {
514
622
  var _step$value = _slicedToArray__default["default"](_step.value, 2),
515
- key = _step$value[0],
516
- list = _step$value[1];
623
+ key = _step$value[0],
624
+ list = _step$value[1];
625
+
517
626
  switch (key) {
518
627
  case 'created':
519
628
  case 'attached':
@@ -525,6 +634,7 @@ function withWeapp(weappConf) {
525
634
  break;
526
635
  }
527
636
  };
637
+
528
638
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
529
639
  _loop();
530
640
  }
@@ -534,56 +644,71 @@ function withWeapp(weappConf) {
534
644
  _iterator.f();
535
645
  }
536
646
  }
647
+
537
648
  for (var confKey in options) {
538
649
  // 不支持的属性
539
650
  if (nonsupport.has(confKey)) {
540
651
  var advise = nonsupport.get(confKey);
541
652
  report(advise);
542
653
  }
654
+
543
655
  var confValue = options[confKey];
656
+
544
657
  switch (confKey) {
545
658
  case 'behaviors':
546
659
  break;
660
+
547
661
  case 'data':
548
662
  {
549
663
  if (isApp) {
550
664
  this[confKey] = confValue;
665
+
551
666
  if (!appOptions.includes(confKey)) {
552
667
  this.defineProperty(this.taroGlobalData, confKey, this);
553
668
  }
554
669
  } else {
555
670
  this.state = Object.assign(Object.assign({}, confValue), this.state);
556
671
  }
672
+
557
673
  break;
558
674
  }
675
+
559
676
  case 'properties':
560
677
  this.initProps(Object.assign(behaviorProperties, confValue));
561
678
  break;
679
+
562
680
  case 'methods':
563
681
  for (var key in confValue) {
564
682
  var method = confValue[key];
565
683
  this[key] = bind(method, this);
566
684
  }
685
+
567
686
  break;
687
+
568
688
  case 'lifetimes':
569
689
  for (var _key2 in confValue) {
570
690
  this.initLifeCycles(_key2, confValue[_key2]);
571
691
  }
692
+
572
693
  break;
694
+
573
695
  case 'pageLifetimes':
574
696
  for (var _key3 in confValue) {
575
697
  var cb = confValue[_key3];
698
+
576
699
  switch (_key3) {
577
700
  case 'show':
578
701
  {
579
702
  this.initLifeCycleListener('show', cb);
580
703
  break;
581
704
  }
705
+
582
706
  case 'hide':
583
707
  {
584
708
  this.initLifeCycleListener('hide', cb);
585
709
  break;
586
710
  }
711
+
587
712
  case 'resize':
588
713
  {
589
714
  report('不支持组件所在页面的生命周期 pageLifetimes.resize。');
@@ -591,53 +716,67 @@ function withWeapp(weappConf) {
591
716
  }
592
717
  }
593
718
  }
719
+
594
720
  break;
721
+
595
722
  default:
596
723
  if (lifecycles.has(confKey)) {
597
724
  // 优先使用 lifetimes 中定义的生命周期
598
725
  if ((_b = options.lifetimes) === null || _b === void 0 ? void 0 : _b[confKey]) {
599
726
  break;
600
727
  }
728
+
601
729
  var lifecycle = options[confKey];
602
730
  this.initLifeCycles(confKey, lifecycle);
603
731
  } else if (isFunction(confValue)) {
604
732
  this[confKey] = bind(confValue, this);
733
+
605
734
  if (isApp && !appOptions.includes(confKey)) {
606
735
  this.defineProperty(this.taroGlobalData, confKey, this);
607
- }
608
- // 原生页面和 Taro 页面中共计只能定义一次的生命周期
736
+ } // 原生页面和 Taro 页面中共计只能定义一次的生命周期
737
+
738
+
609
739
  if (uniquePageLifecycle.includes(confKey) && ConnectComponent.prototype[confKey]) {
610
740
  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"));
611
741
  }
612
742
  } else {
613
743
  this[confKey] = confValue;
744
+
614
745
  if (isApp && !appOptions.includes(confKey)) {
615
746
  this.defineProperty(this.taroGlobalData, confKey, this);
616
747
  }
617
748
  }
749
+
618
750
  break;
619
751
  }
620
- }
621
- // 处理 Behaviors
752
+ } // 处理 Behaviors
753
+
754
+
622
755
  if ((_c = options.behaviors) === null || _c === void 0 ? void 0 : _c.length) {
623
756
  (function () {
624
757
  var behaviorData = {};
625
758
  var methods = {};
759
+
626
760
  var _iterator2 = _createForOfIteratorHelper(behaviorMap.entries()),
627
- _step2;
761
+ _step2;
762
+
628
763
  try {
629
764
  var _loop2 = function _loop2() {
630
765
  var _step2$value = _slicedToArray__default["default"](_step2.value, 2),
631
- key = _step2$value[0],
632
- list = _step2$value[1];
766
+ key = _step2$value[0],
767
+ list = _step2$value[1];
768
+
633
769
  switch (key) {
634
770
  case 'data':
635
771
  [].concat(_toConsumableArray__default["default"](list), [_this2.state]).forEach(function (dataObject, index) {
636
772
  Object.keys(dataObject).forEach(function (dataKey) {
637
773
  var value = dataObject[dataKey];
638
774
  var preValue = behaviorData[dataKey];
775
+
639
776
  var valueType = _typeof__default["default"](value);
777
+
640
778
  var preValueType = _typeof__default["default"](preValue);
779
+
641
780
  if (valueType === 'object') {
642
781
  if (!value) {
643
782
  behaviorData[dataKey] = value;
@@ -654,6 +793,7 @@ function withWeapp(weappConf) {
654
793
  });
655
794
  _this2.state = behaviorData;
656
795
  break;
796
+
657
797
  case 'methods':
658
798
  list.forEach(function (methodsObject) {
659
799
  Object.assign(methods, methodsObject);
@@ -665,10 +805,12 @@ function withWeapp(weappConf) {
665
805
  }
666
806
  });
667
807
  break;
808
+
668
809
  default:
669
810
  break;
670
811
  }
671
812
  };
813
+
672
814
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
673
815
  _loop2();
674
816
  }
@@ -688,14 +830,17 @@ function withWeapp(weappConf) {
688
830
  var advise = nonsupport.get(lifecycleName);
689
831
  return report(advise);
690
832
  }
833
+
691
834
  if (lifecycleName === 'ready') {
692
835
  // 如果组件是延时渲染的,页面 onReady 的事件已经 emit 了,因此使用 componentDidMount + nextTick 模拟
693
836
  if (this.current.page.onReady.called) {
694
837
  this.didMounts.push(function () {
695
838
  var _this3 = this;
839
+
696
840
  for (var _len2 = arguments.length, args = new Array(_len2), _key4 = 0; _key4 < _len2; _key4++) {
697
841
  args[_key4] = arguments[_key4];
698
842
  }
843
+
699
844
  taro.nextTick(function () {
700
845
  if (isFunction(lifecycle)) {
701
846
  lifecycle.apply(_this3, args);
@@ -708,28 +853,34 @@ function withWeapp(weappConf) {
708
853
  } else {
709
854
  for (var lifecycleKey in lifecycleMap) {
710
855
  var cycleNames = lifecycleMap[lifecycleKey];
856
+
711
857
  if (cycleNames.indexOf(lifecycleName) !== -1) {
712
858
  switch (lifecycleKey) {
713
859
  case TaroLifeCycles.DidHide:
714
860
  this.didHides.push(lifecycle);
715
861
  break;
862
+
716
863
  case TaroLifeCycles.DidMount:
717
864
  this.didMounts.push(lifecycle);
718
865
  break;
866
+
719
867
  case TaroLifeCycles.DidShow:
720
868
  this.didShows.push(lifecycle);
721
869
  break;
870
+
722
871
  case TaroLifeCycles.WillMount:
723
872
  this.willMounts.push(lifecycle);
724
873
  break;
874
+
725
875
  case TaroLifeCycles.WillUnmount:
726
876
  this.willUnmounts.push(lifecycle);
727
877
  break;
728
878
  }
729
879
  }
730
880
  }
731
- }
732
- // mixins 不会覆盖已经设置的生命周期,加入到 this 是为了形如 this.created() 的调用
881
+ } // mixins 不会覆盖已经设置的生命周期,加入到 this 是为了形如 this.created() 的调用
882
+
883
+
733
884
  if (!isFunction(this[lifecycleName])) {
734
885
  this[lifecycleName] = lifecycle;
735
886
  }
@@ -741,8 +892,8 @@ function withWeapp(weappConf) {
741
892
  var router = this.current.router;
742
893
  var lifecycleName = "on".concat(name[0].toUpperCase()).concat(name.slice(1));
743
894
  cb = cb.bind(this);
744
- (router === null || router === void 0 ? void 0 : router[lifecycleName]) && taro.eventCenter.on(router[lifecycleName], cb);
745
- // unMount 时需要取消事件监听
895
+ (router === null || router === void 0 ? void 0 : router[lifecycleName]) && taro.eventCenter.on(router[lifecycleName], cb); // unMount 时需要取消事件监听
896
+
746
897
  this.eventDestroyList.push(function () {
747
898
  return taro.eventCenter.off(router[lifecycleName], cb);
748
899
  });
@@ -753,6 +904,7 @@ function withWeapp(weappConf) {
753
904
  for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key5 = 1; _key5 < _len3; _key5++) {
754
905
  args[_key5 - 1] = arguments[_key5];
755
906
  }
907
+
756
908
  for (var i = 0; i < funcs.length; i++) {
757
909
  var func = funcs[i];
758
910
  this.safeExecute.apply(this, [func].concat(args));
@@ -762,15 +914,17 @@ function withWeapp(weappConf) {
762
914
  key: "triggerPropertiesObservers",
763
915
  value: function triggerPropertiesObservers(prevProps, nextProps) {
764
916
  var _this4 = this;
917
+
765
918
  this._observeProps.forEach(function (_ref) {
766
919
  var key = _ref.name,
767
- observer = _ref.observer;
920
+ observer = _ref.observer;
768
921
  var prop = prevProps === null || prevProps === void 0 ? void 0 : prevProps[key];
769
- var nextProp = nextProps[key];
770
- // 小程序是深比较不同之后才 trigger observer
922
+ var nextProp = nextProps[key]; // 小程序是深比较不同之后才 trigger observer
923
+
771
924
  if (!isEqual(prop, nextProp)) {
772
925
  if (typeof observer === 'string') {
773
926
  var ob = _this4[observer];
927
+
774
928
  if (isFunction(ob)) {
775
929
  ob.call(_this4, nextProp, prop, key);
776
930
  }
@@ -784,35 +938,45 @@ function withWeapp(weappConf) {
784
938
  key: "triggerObservers",
785
939
  value: function triggerObservers(current, prev) {
786
940
  var observers = this.observers;
941
+
787
942
  if (observers == null) {
788
943
  return;
789
944
  }
945
+
790
946
  if (Object.keys(observers).length === 0) {
791
947
  return;
792
948
  }
949
+
793
950
  var result = diff(current, prev);
794
951
  var resultKeys = Object.keys(result);
952
+
795
953
  if (resultKeys.length === 0) {
796
954
  return;
797
955
  }
956
+
798
957
  for (var observerKey in observers) {
799
958
  if (/\*\*/.test(observerKey)) {
800
959
  report('数据监听器 observers 不支持使用通配符 **。');
801
960
  continue;
802
961
  }
962
+
803
963
  var keys = observerKey.split(',').map(function (k) {
804
964
  return k.trim();
805
965
  });
806
966
  var isModified = false;
967
+
807
968
  for (var i = 0; i < keys.length; i++) {
808
969
  var key = keys[i];
970
+
809
971
  for (var j = 0; j < resultKeys.length; j++) {
810
972
  var resultKey = resultKeys[j];
973
+
811
974
  if (resultKey.startsWith(key) || key.startsWith(resultKey) && key.endsWith(']')) {
812
975
  isModified = true;
813
976
  }
814
977
  }
815
978
  }
979
+
816
980
  if (isModified) {
817
981
  observers[observerKey].apply(this, keys.map(function (key) {
818
982
  return safeGet(current, key);
@@ -839,19 +1003,22 @@ function withWeapp(weappConf) {
839
1003
  value: function privateStopNoop() {
840
1004
  var e;
841
1005
  var fn;
1006
+
842
1007
  if (arguments.length === 2) {
843
1008
  fn = arguments.length <= 0 ? undefined : arguments[0];
844
1009
  e = arguments.length <= 1 ? undefined : arguments[1];
845
1010
  } else if (arguments.length === 1) {
846
1011
  e = arguments.length <= 0 ? undefined : arguments[0];
847
1012
  }
1013
+
848
1014
  if (e.type === 'touchmove') {
849
1015
  report('catchtouchmove 转换后只能停止回调函数的冒泡,不能阻止滚动穿透。如要阻止滚动穿透,可以手动给编译后的 View 组件加上 catchMove 属性');
850
1016
  }
1017
+
851
1018
  e.stopPropagation();
852
1019
  isFunction(fn) && fn(e);
853
- }
854
- // ================ React 生命周期 ================
1020
+ } // ================ React 生命周期 ================
1021
+
855
1022
  }, {
856
1023
  key: "componentWillMount",
857
1024
  value: function componentWillMount() {
@@ -893,8 +1060,8 @@ function withWeapp(weappConf) {
893
1060
  this.triggerObservers(nextProps, this.props);
894
1061
  this.triggerPropertiesObservers(this.props, nextProps);
895
1062
  this.safeExecute(_get__default["default"](_getPrototypeOf__default["default"](BaseComponent.prototype), "componentWillReceiveProps", this));
896
- }
897
- // ================ 小程序 App, Page, Component 实例属性与方法 ================
1063
+ } // ================ 小程序 App, Page, Component 实例属性与方法 ================
1064
+
898
1065
  }, {
899
1066
  key: "is",
900
1067
  get: function get() {
@@ -914,8 +1081,10 @@ function withWeapp(weappConf) {
914
1081
  key: "componentMethodsProxy",
915
1082
  value: function componentMethodsProxy(method) {
916
1083
  var _this5 = this;
1084
+
917
1085
  return function () {
918
1086
  var page = _this5.current.page;
1087
+
919
1088
  if (page === null || page === void 0 ? void 0 : page[method]) {
920
1089
  return page[method].apply(page, arguments);
921
1090
  } else {
@@ -944,12 +1113,16 @@ function withWeapp(weappConf) {
944
1113
  report(nonsupport.get('groupSetData'));
945
1114
  }
946
1115
  }]);
1116
+
947
1117
  return BaseComponent;
948
1118
  }(ConnectComponent);
1119
+
949
1120
  var props = weappConf.properties;
1121
+
950
1122
  if (props) {
951
1123
  for (var propKey in props) {
952
1124
  var propValue = props[propKey];
1125
+
953
1126
  if (propValue != null && !isFunction(propValue)) {
954
1127
  if (propValue.value !== undefined) {
955
1128
  // 如果是 null 也赋值到 defaultProps
@@ -958,9 +1131,11 @@ function withWeapp(weappConf) {
958
1131
  }
959
1132
  }
960
1133
  }
1134
+
961
1135
  var staticOptions = ['externalClasses', 'relations', 'options'];
962
1136
  staticOptions.forEach(function (option) {
963
1137
  var value = weappConf[option];
1138
+
964
1139
  if (value != null) {
965
1140
  BaseComponent[option] = value;
966
1141
  }