@tarojs/runtime 3.4.0-beta.2 → 3.4.2

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/bom/raf.d.ts CHANGED
@@ -1,4 +1,5 @@
1
+ /// <reference types="node" />
1
2
  export declare let now: any;
2
- declare const raf: typeof requestAnimationFrame;
3
+ declare const raf: typeof requestAnimationFrame | ((callback: any) => NodeJS.Timeout);
3
4
  declare const caf: typeof cancelAnimationFrame;
4
5
  export { raf, caf };
@@ -13,14 +13,8 @@ export declare class TaroEvent {
13
13
  stopPropagation(): void;
14
14
  stopImmediatePropagation(): void;
15
15
  preventDefault(): void;
16
- get target(): {
17
- dataset: any;
18
- id?: string | undefined;
19
- };
20
- get currentTarget(): {
21
- dataset: any;
22
- id?: string | undefined;
23
- };
16
+ get target(): any;
17
+ get currentTarget(): any;
24
18
  }
25
19
  export declare function createEvent(event: MpEvent | string, node?: TaroElement): TaroEvent;
26
20
  export declare function eventHandler(event: MpEvent): void;
@@ -1,5 +1,4 @@
1
1
  /// <reference types="react" />
2
- import { eventHandler } from '../dom/event';
3
2
  import type { PageConfig } from '@tarojs/taro';
4
3
  import type { Instance, PageInstance, PageProps } from './instance';
5
4
  export declare function injectPageInstance(inst: Instance<PageProps>, id: string): void;
@@ -13,24 +12,4 @@ export declare function getOnShowEventKey(path: string): string;
13
12
  export declare function getOnHideEventKey(path: string): string;
14
13
  export declare function createPageConfig(component: any, pageName?: string, data?: Record<string, unknown>, pageConfig?: PageConfig): PageInstance;
15
14
  export declare function createComponentConfig(component: React.ComponentClass, componentName?: string, data?: Record<string, unknown>): any;
16
- export declare function createRecursiveComponentConfig(componentName?: string): {
17
- properties: {
18
- i: {
19
- type: ObjectConstructor;
20
- value: {
21
- nn: string;
22
- };
23
- };
24
- l: {
25
- type: StringConstructor;
26
- value: string;
27
- };
28
- };
29
- options: {
30
- addGlobalClass: boolean;
31
- virtualHost: boolean;
32
- };
33
- methods: {
34
- eh: typeof eventHandler;
35
- };
36
- };
15
+ export declare function createRecursiveComponentConfig(componentName?: string): any;
@@ -21,13 +21,13 @@ interface VueInternal {
21
21
  export interface PageProps {
22
22
  tid?: string;
23
23
  }
24
- export interface ReactPageComponent<T = PageProps> extends ComponentClass<T>, Show, PageInstance {
24
+ export interface ReactPageComponent<T = PageProps> extends ComponentClass<T>, PageInstance {
25
25
  }
26
- export interface ReactPageInstance<T = PageProps> extends Component<T>, Show, PageInstance {
26
+ export interface ReactPageInstance<T = PageProps> extends Component<T>, PageInstance {
27
27
  componentDidShow?(): void;
28
28
  componentDidHide?(): void;
29
29
  }
30
- export interface ReactAppInstance<T = AppInstance> extends Component<T>, Show, AppInstance {
30
+ export interface ReactAppInstance<T = AppInstance> extends Component<T>, AppInstance {
31
31
  }
32
32
  export interface PageLifeCycle extends Show {
33
33
  onPullDownRefresh?(): void;
@@ -63,15 +63,17 @@ export interface PageInstance extends PageLifeCycle {
63
63
  options?: Record<string, unknown>;
64
64
  }
65
65
  interface Show {
66
- componentDidShow?(options?: unknown): void;
67
- componentDidHide?(options?: unknown): void;
68
- onShow?(options?: unknown): void;
69
- onHide?(options?: unknown): void;
66
+ componentDidShow?(): void;
67
+ componentDidHide?(): void;
68
+ onShow?(): void;
69
+ onHide?(): void;
70
70
  }
71
71
  export interface AppInstance extends Show {
72
- onLaunch?(options?: string): void;
72
+ onLaunch?(options?: Record<string, unknown>): void;
73
73
  mount?(component: React.ComponentClass | ComponentOptions<VueCtor> | Vue3Component, id: string, cb: (...args: any[]) => void): void;
74
74
  mount?(component: React.ComponentClass | ComponentOptions<VueCtor> | Vue3Component, id: string, cb: () => void): void;
75
+ componentDidShow?(options?: Record<string, unknown>): void;
76
+ onShow?(options?: Record<string, unknown>): void;
75
77
  unmount?(id: string): void;
76
78
  unmount?(id: string, cb: () => void): void;
77
79
  onPageNotFound?(res: any): void;
@@ -74,7 +74,7 @@ export interface ModifySetAttrPayload {
74
74
  export interface ModifyRmAttrPayload {
75
75
  (element: TaroElement, key: string, payload: UpdatePayload): void;
76
76
  }
77
- export interface OnAddEvent<T extends TaroEventTarget = TaroEventTarget> {
77
+ export interface OnAddEvent<T extends TaroEventTarget = any> {
78
78
  (type: string, handler: EventHandler, options: any, node: T): void;
79
79
  }
80
80
  export interface PatchElement {
@@ -70,10 +70,9 @@ if (process.env.TARO_ENV === 'h5') {
70
70
  };
71
71
  // Load global or shim versions of Map, Set, and WeakMap
72
72
  var functionPrototype = Object.getPrototypeOf(Function);
73
- var usePolyfill = isObject(process) && process.env && process.env["REFLECT_METADATA_USE_MAP_POLYFILL"] === "true";
74
73
  var _Map = Map;
75
74
  var _Set = Set;
76
- var _WeakMap = !usePolyfill && isFunction(WeakMap) ? WeakMap : CreateWeakMapPolyfill();
75
+ var _WeakMap = isFunction(WeakMap) ? WeakMap : CreateWeakMapPolyfill();
77
76
  // [[Metadata]] internal slot
78
77
  // https://rbuckton.github.io/reflect-metadata/#ordinary-object-internal-methods-and-internal-slots
79
78
  var Metadata = new _WeakMap();
@@ -1260,7 +1259,8 @@ function shortcutAttr(key) {
1260
1259
  default:
1261
1260
  return key;
1262
1261
  }
1263
- }
1262
+ }
1263
+ const customWrapperCache = new Map();
1264
1264
 
1265
1265
  const SID_TARO_ELEMENT = '0';
1266
1266
  const SID_TARO_ELEMENT_FACTORY = '1';
@@ -1358,6 +1358,7 @@ let TaroEventTarget = class TaroEventTarget {
1358
1358
  }
1359
1359
  addEventListener(type, handler, options) {
1360
1360
  var _a, _b;
1361
+ type = type.toLowerCase();
1361
1362
  (_b = (_a = this.hooks).onAddEvent) === null || _b === void 0 ? void 0 : _b.call(_a, type, handler, options, this);
1362
1363
  if (type === 'regionchange') {
1363
1364
  // map 组件的 regionchange 事件非常特殊,详情:https://github.com/NervJS/taro/issues/5766
@@ -1365,8 +1366,6 @@ let TaroEventTarget = class TaroEventTarget {
1365
1366
  this.addEventListener('end', handler, options);
1366
1367
  return;
1367
1368
  }
1368
- type = type.toLowerCase();
1369
- const handlers = this.__handlers[type];
1370
1369
  let isCapture = Boolean(options);
1371
1370
  let isOnce = false;
1372
1371
  if (isObject(options)) {
@@ -1382,6 +1381,16 @@ let TaroEventTarget = class TaroEventTarget {
1382
1381
  return;
1383
1382
  }
1384
1383
  process.env.NODE_ENV !== 'production' && warn(isCapture, 'Taro 暂未实现 event 的 capture 特性。');
1384
+ // 某些框架,如 PReact 有委托的机制,handler 始终是同一个函数
1385
+ // 这会导致多层停止冒泡失败:view -> view(handler.stop = false) -> view(handler.stop = true)
1386
+ // 这样解决:view -> view(handlerA.stop = false) -> view(handlerB.stop = false)
1387
+ // 因此每次绑定事件都新建一个函数,如果带来了性能问题,可以把这段逻辑抽取到 PReact 插件中。
1388
+ const oldHandler = handler;
1389
+ handler = function () {
1390
+ oldHandler.apply(this, arguments); // this 指向 Element
1391
+ };
1392
+ handler.oldHandler = oldHandler;
1393
+ const handlers = this.__handlers[type];
1385
1394
  if (isArray(handlers)) {
1386
1395
  handlers.push(handler);
1387
1396
  }
@@ -1398,7 +1407,10 @@ let TaroEventTarget = class TaroEventTarget {
1398
1407
  if (!isArray(handlers)) {
1399
1408
  return;
1400
1409
  }
1401
- const index = handlers.indexOf(handler);
1410
+ const index = handlers.findIndex(item => {
1411
+ if (item === handler || item.oldHandler === handler)
1412
+ return true;
1413
+ });
1402
1414
  process.env.NODE_ENV !== 'production' && warn(index === -1, `事件: '${type}' 没有注册在 DOM 中,因此不会被移除。`);
1403
1415
  handlers.splice(index, 1);
1404
1416
  }
@@ -2605,24 +2617,33 @@ class Performance {
2605
2617
  }
2606
2618
  const perf = new Performance();
2607
2619
 
2608
- function findCustomWrapper(ctx, dataPathArr) {
2609
- let currentData = ctx.__data__ || ctx.data || ctx._data;
2610
- let wrapper;
2611
- let index;
2612
- dataPathArr.some((item, i) => {
2613
- const key = item.replace(/^\[(.+)\]$/, '$1');
2620
+ function findCustomWrapper(root, dataPathArr) {
2621
+ // ['root', 'cn', '[0]'] remove 'root' => ['cn', '[0]']
2622
+ const list = dataPathArr.slice(1);
2623
+ let currentData = root;
2624
+ let customWrapper;
2625
+ let splitedPath = '';
2626
+ list.some((item, i) => {
2627
+ const key = item
2628
+ // '[0]' => '0'
2629
+ .replace(/^\[(.+)\]$/, '$1')
2630
+ // 'cn' => 'childNodes'
2631
+ .replace(/\bcn\b/g, 'childNodes');
2614
2632
  currentData = currentData[key];
2615
2633
  if (isUndefined(currentData))
2616
2634
  return true;
2617
- if (currentData.nn === CUSTOM_WRAPPER) {
2618
- wrapper = currentData;
2619
- index = i;
2635
+ if (currentData.nodeName === CUSTOM_WRAPPER) {
2636
+ const res = customWrapperCache.get(currentData.sid);
2637
+ if (res) {
2638
+ customWrapper = res;
2639
+ splitedPath = dataPathArr.slice(i + 2).join('.');
2640
+ }
2620
2641
  }
2621
2642
  });
2622
- if (wrapper) {
2643
+ if (customWrapper) {
2623
2644
  return {
2624
- wrapper,
2625
- index: index
2645
+ customWrapper,
2646
+ splitedPath
2626
2647
  };
2627
2648
  }
2628
2649
  }
@@ -2690,17 +2711,12 @@ let TaroRootElement = class TaroRootElement extends TaroElement {
2690
2711
  // 更新渲染,区分 CustomWrapper 与页面级别的 setData
2691
2712
  for (const p in data) {
2692
2713
  const dataPathArr = p.split('.');
2693
- const found = findCustomWrapper(ctx, dataPathArr);
2714
+ const found = findCustomWrapper(this, dataPathArr);
2694
2715
  if (found) {
2695
2716
  // 此项数据使用 CustomWrapper 去更新
2696
- const { wrapper, index } = found;
2697
- const customWrapperId = `#${wrapper.uid}`;
2698
- const customWrapper = ctx.selectComponent(customWrapperId);
2699
- if (customWrapper) {
2700
- const splitedPath = dataPathArr.slice(index + 1).join('.');
2701
- // 合并同一个 customWrapper 的相关更新到一次 setData 中
2702
- customWrapperMap.set(customWrapper, Object.assign(Object.assign({}, (customWrapperMap.get(customWrapper) || {})), { [`i.${splitedPath}`]: data[p] }));
2703
- }
2717
+ const { customWrapper, splitedPath } = found;
2718
+ // 合并同一个 customWrapper 的相关更新到一次 setData 中
2719
+ customWrapperMap.set(customWrapper, Object.assign(Object.assign({}, (customWrapperMap.get(customWrapper) || {})), { [`i.${splitedPath}`]: data[p] }));
2704
2720
  }
2705
2721
  else {
2706
2722
  // 此项数据使用页面去更新
@@ -2814,17 +2830,27 @@ class TaroEvent {
2814
2830
  this.defaultPrevented = true;
2815
2831
  }
2816
2832
  get target() {
2817
- var _a, _b, _c;
2818
- const element = getDocument().getElementById((_a = this.mpEvent) === null || _a === void 0 ? void 0 : _a.target.id);
2819
- return Object.assign(Object.assign(Object.assign({}, (_b = this.mpEvent) === null || _b === void 0 ? void 0 : _b.target), (_c = this.mpEvent) === null || _c === void 0 ? void 0 : _c.detail), { dataset: element !== null ? element.dataset : EMPTY_OBJ });
2833
+ var _a, _b;
2834
+ const target = Object.create(((_a = this.mpEvent) === null || _a === void 0 ? void 0 : _a.target) || null);
2835
+ const element = getDocument().getElementById(target.id);
2836
+ target.dataset = element !== null ? element.dataset : EMPTY_OBJ;
2837
+ for (const key in (_b = this.mpEvent) === null || _b === void 0 ? void 0 : _b.detail) {
2838
+ target[key] = this.mpEvent.detail[key];
2839
+ }
2840
+ return target;
2820
2841
  }
2821
2842
  get currentTarget() {
2822
- var _a, _b, _c;
2823
- const element = getDocument().getElementById((_a = this.mpEvent) === null || _a === void 0 ? void 0 : _a.currentTarget.id);
2843
+ var _a, _b;
2844
+ const currentTarget = Object.create(((_a = this.mpEvent) === null || _a === void 0 ? void 0 : _a.currentTarget) || null);
2845
+ const element = getDocument().getElementById(currentTarget.id);
2824
2846
  if (element === null) {
2825
2847
  return this.target;
2826
2848
  }
2827
- return Object.assign(Object.assign(Object.assign({}, (_b = this.mpEvent) === null || _b === void 0 ? void 0 : _b.currentTarget), (_c = this.mpEvent) === null || _c === void 0 ? void 0 : _c.detail), { dataset: element.dataset });
2849
+ currentTarget.dataset = element.dataset;
2850
+ for (const key in (_b = this.mpEvent) === null || _b === void 0 ? void 0 : _b.detail) {
2851
+ currentTarget[key] = this.mpEvent.detail[key];
2852
+ }
2853
+ return currentTarget;
2828
2854
  }
2829
2855
  }
2830
2856
  function createEvent(event, node) {
@@ -4292,11 +4318,11 @@ if (process.env.TARO_ENV && process.env.TARO_ENV !== 'h5') {
4292
4318
  if (!(DATE in window$1)) {
4293
4319
  window$1.Date = Date;
4294
4320
  }
4295
- window$1.setTimeout = function (cb, delay) {
4296
- setTimeout(cb, delay);
4321
+ window$1.setTimeout = function (...args) {
4322
+ return setTimeout(...args);
4297
4323
  };
4298
- window$1.clearTimeout = function (seed) {
4299
- clearTimeout(seed);
4324
+ window$1.clearTimeout = function (...args) {
4325
+ return clearTimeout(...args);
4300
4326
  };
4301
4327
  document$1.defaultView = window$1;
4302
4328
  }
@@ -4433,11 +4459,8 @@ function stringify(obj) {
4433
4459
  return path === '' ? path : '?' + path;
4434
4460
  }
4435
4461
  function getPath(id, options) {
4436
- let path = id;
4437
- if (process.env.TARO_ENV !== 'h5') {
4438
- path = id + stringify(options);
4439
- }
4440
- return path;
4462
+ const idx = id.indexOf('?');
4463
+ return `${idx > -1 ? id.substring(0, idx) : id}${stringify(process.env.TARO_ENV === 'h5' ? { stamp: (options === null || options === void 0 ? void 0 : options.stamp) || '' } : options)}`;
4441
4464
  }
4442
4465
  function getOnReadyEventKey(path) {
4443
4466
  return path + '.' + ON_READY;
@@ -4479,6 +4502,9 @@ function createPageConfig(component, pageName, data, pageConfig) {
4479
4502
  // this.$taroPath 是页面唯一标识,不可变,因此页面参数 options 也不可变
4480
4503
  this.$taroPath = getPath(id, options);
4481
4504
  const $taroPath = this.$taroPath;
4505
+ if (process.env.TARO_ENV === 'h5') {
4506
+ config.path = this.$taroPath;
4507
+ }
4482
4508
  // this.$taroParams 作为暴露给开发者的页面参数对象,可以被随意修改
4483
4509
  if (this.$taroParams == null) {
4484
4510
  this.$taroParams = Object.assign({}, options);
@@ -4529,13 +4555,13 @@ function createPageConfig(component, pageName, data, pageConfig) {
4529
4555
  raf(() => eventCenter.trigger(getOnReadyEventKey(id)));
4530
4556
  this.onReady.called = true;
4531
4557
  },
4532
- [ONSHOW]() {
4558
+ [ONSHOW](options = {}) {
4533
4559
  hasLoaded.then(() => {
4534
4560
  // 设置 Current 的 page 和 router
4535
4561
  Current.page = this;
4536
4562
  setCurrentRouter(this);
4537
4563
  // 触发生命周期
4538
- safeExecute(this.$taroPath, ON_SHOW);
4564
+ safeExecute(this.$taroPath, ON_SHOW, options);
4539
4565
  // 通过事件触发子组件的生命周期
4540
4566
  raf(() => eventCenter.trigger(getOnShowEventKey(id)));
4541
4567
  });
@@ -4584,9 +4610,6 @@ function createPageConfig(component, pageName, data, pageConfig) {
4584
4610
  if (!isUndefined(data)) {
4585
4611
  config.data = data;
4586
4612
  }
4587
- if (process.env.TARO_ENV === 'h5') {
4588
- config.path = id;
4589
- }
4590
4613
  (_c = hooks.modifyPageObject) === null || _c === void 0 ? void 0 : _c.call(hooks, config);
4591
4614
  return config;
4592
4615
  }
@@ -4631,8 +4654,26 @@ function createComponentConfig(component, componentName, data) {
4631
4654
  return config;
4632
4655
  }
4633
4656
  function createRecursiveComponentConfig(componentName) {
4634
- return {
4635
- properties: {
4657
+ const isCustomWrapper = componentName === CUSTOM_WRAPPER;
4658
+ const lifeCycles = isCustomWrapper
4659
+ ? {
4660
+ attached() {
4661
+ var _a;
4662
+ const componentId = (_a = this.data.i) === null || _a === void 0 ? void 0 : _a.sid;
4663
+ if (isString(componentId)) {
4664
+ customWrapperCache.set(componentId, this);
4665
+ }
4666
+ },
4667
+ detached() {
4668
+ var _a;
4669
+ const componentId = (_a = this.data.i) === null || _a === void 0 ? void 0 : _a.sid;
4670
+ if (isString(componentId)) {
4671
+ customWrapperCache.delete(componentId);
4672
+ }
4673
+ }
4674
+ }
4675
+ : EMPTY_OBJ;
4676
+ return Object.assign({ properties: {
4636
4677
  i: {
4637
4678
  type: Object,
4638
4679
  value: {
@@ -4643,15 +4684,12 @@ function createRecursiveComponentConfig(componentName) {
4643
4684
  type: String,
4644
4685
  value: ''
4645
4686
  }
4646
- },
4647
- options: {
4687
+ }, options: {
4648
4688
  addGlobalClass: true,
4649
- virtualHost: componentName !== CUSTOM_WRAPPER
4650
- },
4651
- methods: {
4689
+ virtualHost: !isCustomWrapper
4690
+ }, methods: {
4652
4691
  eh: eventHandler
4653
- }
4654
- };
4692
+ } }, lifeCycles);
4655
4693
  }
4656
4694
 
4657
4695
  function removeLeadingSlash(path) {