@tarojs/runtime 4.2.1-beta.3 → 4.2.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export { taroHistoryProvider as history, taroLocationProvider as location, taroW
12
12
  export { TaroElement } from './dom/element.js';
13
13
  export { TaroEvent, createEvent, eventHandler, eventHandlerTTDom } from './dom/event.js';
14
14
  export { FormElement } from './dom/form.js';
15
+ export { bumpNearestCtxEpochForRoot, getNearestCtx, isNearestCtxEnv } from './dom/nearest-ctx.js';
15
16
  export { TaroNode } from './dom/node.js';
16
17
  export { TaroRootElement } from './dom/root.js';
17
18
  export { Style } from './dom/style.js';
package/dist/index.js CHANGED
@@ -13,6 +13,7 @@ export { taroHistoryProvider as history, taroLocationProvider as location, taroW
13
13
  export { TaroElement } from './dom/element.js';
14
14
  export { TaroEvent, createEvent, eventHandler, eventHandlerTTDom } from './dom/event.js';
15
15
  export { FormElement } from './dom/form.js';
16
+ export { bumpNearestCtxEpochForRoot, getNearestCtx, isNearestCtxEnv } from './dom/nearest-ctx.js';
16
17
  export { TaroNode } from './dom/node.js';
17
18
  export { TaroRootElement } from './dom/root.js';
18
19
  export { Style } from './dom/style.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -242,9 +242,11 @@ declare class TaroRootElement extends TaroElement {
242
242
  private updateCallbacks;
243
243
  pendingUpdate: boolean;
244
244
  ctx: null | MpInstance;
245
+ nearestCtxEpoch: number;
245
246
  constructor();
246
247
  get _path(): string;
247
248
  get _root(): TaroRootElement;
249
+ bumpNearestCtxEpoch(): void;
248
250
  scheduleTask(fn: TFunc): void;
249
251
  enqueueUpdate(payload: UpdatePayload): void;
250
252
  performUpdate(initRender?: boolean, prerender?: TFunc): void;
@@ -268,6 +270,7 @@ declare class TaroNode extends TaroEventTarget {
268
270
  private updateChildNodes;
269
271
  private updateSingleChild;
270
272
  get _root(): TaroRootElement | null;
273
+ get _scope(): MpInstance | null | undefined;
271
274
  protected findIndex(refChild: TaroNode): number;
272
275
  get _path(): string;
273
276
  get nextSibling(): TaroNode | null;
@@ -591,6 +594,10 @@ declare const taroWindowProvider: TaroWindow;
591
594
  declare const taroLocationProvider: TaroLocation;
592
595
  declare const taroHistoryProvider: TaroHistory;
593
596
 
597
+ declare function isNearestCtxEnv(): boolean;
598
+ declare function getNearestCtx(node: TaroNode): MpInstance | null | undefined;
599
+ declare function bumpNearestCtxEpochForRoot(root: TaroRootElement | null | undefined): void;
600
+
594
601
  declare class SVGElement extends TaroElement {
595
602
  }
596
603
 
@@ -861,7 +868,7 @@ declare function getOnShowEventKey(path: string): string;
861
868
  declare function getOnHideEventKey(path: string): string;
862
869
  declare function createPageConfig(component: any, pageName?: string, data?: Record<string, unknown>, pageConfig?: PageConfig): PageInstance;
863
870
  declare function createComponentConfig(component: React.ComponentClass, componentName?: string, data?: Record<string, unknown>): any;
864
- declare function createRecursiveComponentConfig(componentName?: string): any;
871
+ declare function createRecursiveComponentConfig(componentName?: string, forceCustomWrapper?: boolean): any;
865
872
 
866
873
  /**
867
874
  * React also has a fancy function's name for this: `hydrate()`.
@@ -917,5 +924,5 @@ declare function convertNumber2PX(value: number): string;
917
924
 
918
925
  declare function handlePolyfill(): void;
919
926
 
920
- export { A, APP, BEHAVIORS, BODY, CATCHMOVE, CATCH_VIEW, CHANGE, CLASS, CLICK_VIEW, COMMENT, COMPILE_MODE, CONFIRM, CONTAINER, CONTEXT_ACTIONS, CURRENT_TARGET, CUSTOM_WRAPPER, Current, DATASET, DATE, DOCUMENT_ELEMENT_NAME, DOCUMENT_FRAGMENT, EVENT_CALLBACK_RESULT, EXTERNAL_CLASSES, FOCUS, FormElement, HEAD, HOOKS_APP_ID, HTML, History, ID, INPUT, KEY_CODE, Location, MutationObserver, OBJECT, ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, OPTIONS, PAGE_INIT, PROPERTY_THRESHOLD, PROPS, PURE_VIEW, ROOT_STR, SET_DATA, SET_TIMEOUT, STATIC_VIEW, STYLE, SVGElement, Style, TARGET, TARO_RUNTIME, TIME_STAMP, TOUCHMOVE, TYPE, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, UID, TaroURLProvider as URL, URLSearchParams$1 as URLSearchParams, VALUE, VIEW, addLeadingSlash, _caf as cancelAnimationFrame, convertNumber2PX, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, customWrapperCache, debounce, taroDocumentProvider as document, env, eventCenter, eventHandler, eventHandlerTTDom, eventSource, extend, getComponentsAlias, taroGetComputedStyleProvider as getComputedStyle, getCurrentInstance, getCurrentPage, getHomePage, getOnHideEventKey, getOnReadyEventKey, getOnShowEventKey, getPageInstance, getPath, handlePolyfill, hasBasename, taroHistoryProvider as history, hydrate, incrementId, injectPageInstance, isComment, isElement, isHasExtractProp, isParentBound, isText, taroLocationProvider as location, nav as navigator, nextTick, now, options, parseUrl, perf, removePageInstance, _raf as requestAnimationFrame, safeExecute, setInnerHTML, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
927
+ export { A, APP, BEHAVIORS, BODY, CATCHMOVE, CATCH_VIEW, CHANGE, CLASS, CLICK_VIEW, COMMENT, COMPILE_MODE, CONFIRM, CONTAINER, CONTEXT_ACTIONS, CURRENT_TARGET, CUSTOM_WRAPPER, Current, DATASET, DATE, DOCUMENT_ELEMENT_NAME, DOCUMENT_FRAGMENT, EVENT_CALLBACK_RESULT, EXTERNAL_CLASSES, FOCUS, FormElement, HEAD, HOOKS_APP_ID, HTML, History, ID, INPUT, KEY_CODE, Location, MutationObserver, OBJECT, ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, OPTIONS, PAGE_INIT, PROPERTY_THRESHOLD, PROPS, PURE_VIEW, ROOT_STR, SET_DATA, SET_TIMEOUT, STATIC_VIEW, STYLE, SVGElement, Style, TARGET, TARO_RUNTIME, TIME_STAMP, TOUCHMOVE, TYPE, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, UID, TaroURLProvider as URL, URLSearchParams$1 as URLSearchParams, VALUE, VIEW, addLeadingSlash, bumpNearestCtxEpochForRoot, _caf as cancelAnimationFrame, convertNumber2PX, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, customWrapperCache, debounce, taroDocumentProvider as document, env, eventCenter, eventHandler, eventHandlerTTDom, eventSource, extend, getComponentsAlias, taroGetComputedStyleProvider as getComputedStyle, getCurrentInstance, getCurrentPage, getHomePage, getNearestCtx, getOnHideEventKey, getOnReadyEventKey, getOnShowEventKey, getPageInstance, getPath, handlePolyfill, hasBasename, taroHistoryProvider as history, hydrate, incrementId, injectPageInstance, isComment, isElement, isHasExtractProp, isNearestCtxEnv, isParentBound, isText, taroLocationProvider as location, nav as navigator, nextTick, now, options, parseUrl, perf, removePageInstance, _raf as requestAnimationFrame, safeExecute, setInnerHTML, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
921
928
  export type { AddEventListenerOptions, AppInstance, Attributes, DataTree, EventHandler, EventListenerOptions, EventOptions, EventsType, HydratedData, Instance, KeyFrame, MiniData, MiniElementData, MiniTextData, MpEvent, MpInstance, Options$2 as Options, PageConfig, PageInstance, PageLifeCycle, PageProps, ReactAppInstance, ReactPageComponent, ReactPageInstance, ScrollTimelineOption, TFunc, UpdatePayload, UpdatePayloadValue };
@@ -1472,6 +1472,53 @@ class TaroEventTarget {
1472
1472
  }
1473
1473
  }
1474
1474
 
1475
+ const nearestCtxCache = new WeakMap();
1476
+ function isNearestCtxEnv() {
1477
+ return process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'jd';
1478
+ }
1479
+ function getNearestCtx(node) {
1480
+ if (!isNearestCtxEnv()) {
1481
+ return undefined;
1482
+ }
1483
+ const root = node._root;
1484
+ if (root == null) {
1485
+ return null;
1486
+ }
1487
+ const cached = nearestCtxCache.get(node);
1488
+ if (cached && cached.nearestCtxEpoch === root.nearestCtxEpoch) {
1489
+ return cached.value;
1490
+ }
1491
+ const value = resolveNearestCtxValue(node, root);
1492
+ nearestCtxCache.set(node, { value, nearestCtxEpoch: root.nearestCtxEpoch });
1493
+ return value;
1494
+ }
1495
+ function resolveNearestCtxValue(node, root) {
1496
+ var _a;
1497
+ if (node.nodeType === 1 /* NodeType.ELEMENT_NODE */) {
1498
+ const ctx = node.ctx;
1499
+ if (ctx != null)
1500
+ return ctx;
1501
+ }
1502
+ let current = node.parentNode;
1503
+ while (current) {
1504
+ if (current.nodeType === 1 /* NodeType.ELEMENT_NODE */) {
1505
+ const ctx = current.ctx;
1506
+ if (ctx != null)
1507
+ return ctx;
1508
+ }
1509
+ current = current.parentNode;
1510
+ }
1511
+ return (_a = root.ctx) !== null && _a !== void 0 ? _a : null;
1512
+ }
1513
+ function bumpNearestCtxEpochForRoot(root) {
1514
+ if (!isNearestCtxEnv() || root == null) {
1515
+ return;
1516
+ }
1517
+ if (typeof root.bumpNearestCtxEpoch === 'function') {
1518
+ root.bumpNearestCtxEpoch();
1519
+ }
1520
+ }
1521
+
1475
1522
  const CHILDNODES = "cn" /* Shortcuts.Childnodes */;
1476
1523
  const nodeId = incrementId();
1477
1524
  class TaroNode extends TaroEventTarget {
@@ -1511,6 +1558,9 @@ class TaroNode extends TaroEventTarget {
1511
1558
  var _a;
1512
1559
  return ((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a._root) || null;
1513
1560
  }
1561
+ get _scope() {
1562
+ return getNearestCtx(this);
1563
+ }
1514
1564
  findIndex(refChild) {
1515
1565
  const index = this.childNodes.indexOf(refChild);
1516
1566
  ensure(index !== -1, 'The node to be replaced is not a child of this node.');
@@ -1656,6 +1706,9 @@ class TaroNode extends TaroEventTarget {
1656
1706
  }
1657
1707
  }
1658
1708
  }
1709
+ if (this._root) {
1710
+ bumpNearestCtxEpochForRoot(this._root);
1711
+ }
1659
1712
  MutationObserver$1.record({
1660
1713
  type: "childList" /* MutationRecordType.CHILD_LIST */,
1661
1714
  target: this,
@@ -1732,6 +1785,9 @@ class TaroNode extends TaroEventTarget {
1732
1785
  if (this._root && doUpdate !== false) {
1733
1786
  this.updateChildNodes();
1734
1787
  }
1788
+ if (this._root) {
1789
+ bumpNearestCtxEpochForRoot(this._root);
1790
+ }
1735
1791
  return child;
1736
1792
  }
1737
1793
  remove(options) {
@@ -2427,7 +2483,7 @@ class TaroElement extends TaroNode {
2427
2483
  hooks.call('modifyRemoveEventListener', this, sideEffect, getComponentsAlias);
2428
2484
  if (sideEffect !== false && !this.isAnyEventBinded() && SPECIAL_NODES.indexOf(name) > -1) {
2429
2485
  const componentsAlias = getComponentsAlias();
2430
- const value = isHasExtractProp(this) ? `static-${name}` : `pure-${name}`;
2486
+ const value = name === VIEW && !isHasExtractProp(this) ? PURE_VIEW : `static-${name}`;
2431
2487
  const valueAlias = componentsAlias[value]._num;
2432
2488
  this.enqueueUpdate({
2433
2489
  path: `${this._path}.${"nn" /* Shortcuts.NodeName */}`,
@@ -3643,6 +3699,7 @@ class TaroRootElement extends TaroElement {
3643
3699
  this.updateCallbacks = [];
3644
3700
  this.pendingUpdate = false;
3645
3701
  this.ctx = null;
3702
+ this.nearestCtxEpoch = 0;
3646
3703
  this.nodeName = ROOT_STR;
3647
3704
  this.tagName = ROOT_STR.toUpperCase();
3648
3705
  }
@@ -3652,6 +3709,9 @@ class TaroRootElement extends TaroElement {
3652
3709
  get _root() {
3653
3710
  return this;
3654
3711
  }
3712
+ bumpNearestCtxEpoch() {
3713
+ this.nearestCtxEpoch++;
3714
+ }
3655
3715
  scheduleTask(fn) {
3656
3716
  // 这里若使用微任务可略微提前setData的执行时机,但在部分场景下可能会出现连续setData两次,造成更大的性能问题
3657
3717
  setTimeout(fn);
@@ -4216,6 +4276,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4216
4276
  loadResolver();
4217
4277
  if (process.env.TARO_PLATFORM !== 'web') {
4218
4278
  pageElement.ctx = this;
4279
+ bumpNearestCtxEpochForRoot(pageElement);
4219
4280
  if (process.env.TARO_ENV === 'tt' && isEnableTTDom()) {
4220
4281
  pageElement.sync();
4221
4282
  }
@@ -4248,6 +4309,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4248
4309
  unmounting = false;
4249
4310
  instances.delete($taroPath);
4250
4311
  if (pageElement) {
4312
+ bumpNearestCtxEpochForRoot(pageElement);
4251
4313
  pageElement.ctx = null;
4252
4314
  pageElement = null;
4253
4315
  }
@@ -4410,36 +4472,42 @@ function createComponentConfig(component, componentName, data) {
4410
4472
  });
4411
4473
  return config;
4412
4474
  }
4413
- function createRecursiveComponentConfig(componentName) {
4475
+ function createRecursiveComponentConfig(componentName, forceCustomWrapper = false) {
4414
4476
  const isCustomWrapper = componentName === CUSTOM_WRAPPER;
4415
4477
  const [ATTACHED, DETACHED] = hooks.call('getMiniLifecycleImpl').component;
4416
- const lifeCycles = isCustomWrapper
4478
+ const lifeCycles = isCustomWrapper || forceCustomWrapper
4417
4479
  ? {
4418
4480
  [ATTACHED]() {
4419
- var _a, _b;
4481
+ var _a, _b, _c, _d;
4420
4482
  if (process.env.TARO_ENV === 'tt' && isEnableTTDom()) {
4421
4483
  return;
4422
4484
  }
4423
- const componentId = ((_a = this.data.i) === null || _a === void 0 ? void 0 : _a.sid) || ((_b = this.props.i) === null || _b === void 0 ? void 0 : _b.sid);
4485
+ const componentId = ((_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.i) === null || _b === void 0 ? void 0 : _b.sid) || ((_d = (_c = this.props) === null || _c === void 0 ? void 0 : _c.i) === null || _d === void 0 ? void 0 : _d.sid);
4424
4486
  if (isString(componentId)) {
4425
- customWrapperCache.set(componentId, this);
4487
+ if (isCustomWrapper) {
4488
+ customWrapperCache.set(componentId, this);
4489
+ }
4426
4490
  const el = env.document.getElementById(componentId);
4427
4491
  if (el) {
4428
4492
  el.ctx = this;
4493
+ bumpNearestCtxEpochForRoot(el._root);
4429
4494
  }
4430
4495
  }
4431
4496
  },
4432
4497
  [DETACHED]() {
4433
- var _a, _b;
4498
+ var _a, _b, _c, _d;
4434
4499
  if (process.env.TARO_ENV === 'tt' && isEnableTTDom()) {
4435
4500
  return;
4436
4501
  }
4437
- const componentId = ((_a = this.data.i) === null || _a === void 0 ? void 0 : _a.sid) || ((_b = this.props.i) === null || _b === void 0 ? void 0 : _b.sid);
4502
+ const componentId = ((_b = (_a = this.data) === null || _a === void 0 ? void 0 : _a.i) === null || _b === void 0 ? void 0 : _b.sid) || ((_d = (_c = this.props) === null || _c === void 0 ? void 0 : _c.i) === null || _d === void 0 ? void 0 : _d.sid);
4438
4503
  if (isString(componentId)) {
4439
- customWrapperCache.delete(componentId);
4504
+ if (isCustomWrapper) {
4505
+ customWrapperCache.delete(componentId);
4506
+ }
4440
4507
  const el = env.document.getElementById(componentId);
4441
4508
  if (el) {
4442
4509
  el.ctx = null;
4510
+ bumpNearestCtxEpochForRoot(el._root);
4443
4511
  }
4444
4512
  }
4445
4513
  }
@@ -4463,7 +4531,7 @@ function createRecursiveComponentConfig(componentName) {
4463
4531
  }
4464
4532
  }, options: Object.assign(Object.assign({}, extraOptions), { virtualHost: !isCustomWrapper }), methods: {
4465
4533
  eh: eventHandler
4466
- } }, lifeCycles), { isCustomWrapper });
4534
+ } }, lifeCycles), { isCustomWrapper, forceCustomWrapper });
4467
4535
  }
4468
4536
 
4469
4537
  const TIMEOUT = 100;
@@ -5275,5 +5343,5 @@ if (process.env.SUPPORT_TARO_POLYFILL !== 'disabled' && process.env.TARO_PLATFOR
5275
5343
  handlePolyfill();
5276
5344
  }
5277
5345
 
5278
- export { A, APP, BEHAVIORS, BODY, CATCHMOVE, CATCH_VIEW, CHANGE, CLASS, CLICK_VIEW, COMMENT, COMPILE_MODE, CONFIRM, CONTAINER, CONTEXT_ACTIONS, CURRENT_TARGET, CUSTOM_WRAPPER, Current, DATASET, DATE, DOCUMENT_ELEMENT_NAME, DOCUMENT_FRAGMENT, EVENT_CALLBACK_RESULT, EXTERNAL_CLASSES, FOCUS, FormElement, HEAD, HOOKS_APP_ID, HTML, History, ID, INPUT, KEY_CODE, Location, MutationObserver$1 as MutationObserver, OBJECT, ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, OPTIONS, PAGE_INIT, PROPERTY_THRESHOLD, PROPS, PURE_VIEW, ROOT_STR, SET_DATA, SET_TIMEOUT, STATIC_VIEW, STYLE, SVGElement, Style, TARGET, TARO_RUNTIME, TIME_STAMP, TOUCHMOVE, TYPE, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, UID, TaroURLProvider as URL, URLSearchParams, VALUE, VIEW, addLeadingSlash, _caf as cancelAnimationFrame, convertNumber2PX, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, customWrapperCache, debounce, taroDocumentProvider as document, env, eventCenter, eventHandler, eventHandlerTTDom, eventSource, extend, getComponentsAlias, taroGetComputedStyleProvider as getComputedStyle, getCurrentInstance, getCurrentPage, getHomePage, getOnHideEventKey, getOnReadyEventKey, getOnShowEventKey, getPageInstance, getPath, handlePolyfill, hasBasename, taroHistoryProvider as history, hydrate, incrementId, injectPageInstance, isComment, isElement, isHasExtractProp, isParentBound, isText, taroLocationProvider as location, nav as navigator, nextTick, now, options, parseUrl, perf, removePageInstance, _raf as requestAnimationFrame, safeExecute, setInnerHTML, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
5346
+ export { A, APP, BEHAVIORS, BODY, CATCHMOVE, CATCH_VIEW, CHANGE, CLASS, CLICK_VIEW, COMMENT, COMPILE_MODE, CONFIRM, CONTAINER, CONTEXT_ACTIONS, CURRENT_TARGET, CUSTOM_WRAPPER, Current, DATASET, DATE, DOCUMENT_ELEMENT_NAME, DOCUMENT_FRAGMENT, EVENT_CALLBACK_RESULT, EXTERNAL_CLASSES, FOCUS, FormElement, HEAD, HOOKS_APP_ID, HTML, History, ID, INPUT, KEY_CODE, Location, MutationObserver$1 as MutationObserver, OBJECT, ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, OPTIONS, PAGE_INIT, PROPERTY_THRESHOLD, PROPS, PURE_VIEW, ROOT_STR, SET_DATA, SET_TIMEOUT, STATIC_VIEW, STYLE, SVGElement, Style, TARGET, TARO_RUNTIME, TIME_STAMP, TOUCHMOVE, TYPE, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, UID, TaroURLProvider as URL, URLSearchParams, VALUE, VIEW, addLeadingSlash, bumpNearestCtxEpochForRoot, _caf as cancelAnimationFrame, convertNumber2PX, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, customWrapperCache, debounce, taroDocumentProvider as document, env, eventCenter, eventHandler, eventHandlerTTDom, eventSource, extend, getComponentsAlias, taroGetComputedStyleProvider as getComputedStyle, getCurrentInstance, getCurrentPage, getHomePage, getNearestCtx, getOnHideEventKey, getOnReadyEventKey, getOnShowEventKey, getPageInstance, getPath, handlePolyfill, hasBasename, taroHistoryProvider as history, hydrate, incrementId, injectPageInstance, isComment, isElement, isHasExtractProp, isNearestCtxEnv, isParentBound, isText, taroLocationProvider as location, nav as navigator, nextTick, now, options, parseUrl, perf, removePageInstance, _raf as requestAnimationFrame, safeExecute, setInnerHTML, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
5279
5347
  //# sourceMappingURL=runtime.esm.js.map