@tarojs/runtime 4.2.2-alpha.2 → 4.2.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -1473,6 +1473,53 @@ class TaroEventTarget {
1473
1473
  }
1474
1474
  }
1475
1475
 
1476
+ const nearestCtxCache = new WeakMap();
1477
+ function isNearestCtxEnv() {
1478
+ return process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'jd';
1479
+ }
1480
+ function getNearestCtx(node) {
1481
+ if (!isNearestCtxEnv()) {
1482
+ return undefined;
1483
+ }
1484
+ const root = node._root;
1485
+ if (root == null) {
1486
+ return null;
1487
+ }
1488
+ const cached = nearestCtxCache.get(node);
1489
+ if (cached && cached.nearestCtxEpoch === root.nearestCtxEpoch) {
1490
+ return cached.value;
1491
+ }
1492
+ const value = resolveNearestCtxValue(node, root);
1493
+ nearestCtxCache.set(node, { value, nearestCtxEpoch: root.nearestCtxEpoch });
1494
+ return value;
1495
+ }
1496
+ function resolveNearestCtxValue(node, root) {
1497
+ var _a;
1498
+ if (node.nodeType === 1 /* NodeType.ELEMENT_NODE */) {
1499
+ const ctx = node.ctx;
1500
+ if (ctx != null)
1501
+ return ctx;
1502
+ }
1503
+ let current = node.parentNode;
1504
+ while (current) {
1505
+ if (current.nodeType === 1 /* NodeType.ELEMENT_NODE */) {
1506
+ const ctx = current.ctx;
1507
+ if (ctx != null)
1508
+ return ctx;
1509
+ }
1510
+ current = current.parentNode;
1511
+ }
1512
+ return (_a = root.ctx) !== null && _a !== void 0 ? _a : null;
1513
+ }
1514
+ function bumpNearestCtxEpochForRoot(root) {
1515
+ if (!isNearestCtxEnv() || root == null) {
1516
+ return;
1517
+ }
1518
+ if (typeof root.bumpNearestCtxEpoch === 'function') {
1519
+ root.bumpNearestCtxEpoch();
1520
+ }
1521
+ }
1522
+
1476
1523
  const CHILDNODES = "cn" /* Shortcuts.Childnodes */;
1477
1524
  const nodeId = incrementId();
1478
1525
  class TaroNode extends TaroEventTarget {
@@ -1512,6 +1559,9 @@ class TaroNode extends TaroEventTarget {
1512
1559
  var _a;
1513
1560
  return ((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a._root) || null;
1514
1561
  }
1562
+ get _scope() {
1563
+ return getNearestCtx(this);
1564
+ }
1515
1565
  findIndex(refChild) {
1516
1566
  const index = this.childNodes.indexOf(refChild);
1517
1567
  shared.ensure(index !== -1, 'The node to be replaced is not a child of this node.');
@@ -1657,6 +1707,9 @@ class TaroNode extends TaroEventTarget {
1657
1707
  }
1658
1708
  }
1659
1709
  }
1710
+ if (this._root) {
1711
+ bumpNearestCtxEpochForRoot(this._root);
1712
+ }
1660
1713
  MutationObserver$1.record({
1661
1714
  type: "childList" /* MutationRecordType.CHILD_LIST */,
1662
1715
  target: this,
@@ -1733,6 +1786,9 @@ class TaroNode extends TaroEventTarget {
1733
1786
  if (this._root && doUpdate !== false) {
1734
1787
  this.updateChildNodes();
1735
1788
  }
1789
+ if (this._root) {
1790
+ bumpNearestCtxEpochForRoot(this._root);
1791
+ }
1736
1792
  return child;
1737
1793
  }
1738
1794
  remove(options) {
@@ -3644,6 +3700,7 @@ class TaroRootElement extends TaroElement {
3644
3700
  this.updateCallbacks = [];
3645
3701
  this.pendingUpdate = false;
3646
3702
  this.ctx = null;
3703
+ this.nearestCtxEpoch = 0;
3647
3704
  this.nodeName = ROOT_STR;
3648
3705
  this.tagName = ROOT_STR.toUpperCase();
3649
3706
  }
@@ -3653,6 +3710,9 @@ class TaroRootElement extends TaroElement {
3653
3710
  get _root() {
3654
3711
  return this;
3655
3712
  }
3713
+ bumpNearestCtxEpoch() {
3714
+ this.nearestCtxEpoch++;
3715
+ }
3656
3716
  scheduleTask(fn) {
3657
3717
  // 这里若使用微任务可略微提前setData的执行时机,但在部分场景下可能会出现连续setData两次,造成更大的性能问题
3658
3718
  setTimeout(fn);
@@ -4217,6 +4277,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4217
4277
  loadResolver();
4218
4278
  if (process.env.TARO_PLATFORM !== 'web') {
4219
4279
  pageElement.ctx = this;
4280
+ bumpNearestCtxEpochForRoot(pageElement);
4220
4281
  if (process.env.TARO_ENV === 'tt' && shared.isEnableTTDom()) {
4221
4282
  pageElement.sync();
4222
4283
  }
@@ -4249,6 +4310,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4249
4310
  unmounting = false;
4250
4311
  instances.delete($taroPath);
4251
4312
  if (pageElement) {
4313
+ bumpNearestCtxEpochForRoot(pageElement);
4252
4314
  pageElement.ctx = null;
4253
4315
  pageElement = null;
4254
4316
  }
@@ -4411,36 +4473,42 @@ function createComponentConfig(component, componentName, data) {
4411
4473
  });
4412
4474
  return config;
4413
4475
  }
4414
- function createRecursiveComponentConfig(componentName) {
4476
+ function createRecursiveComponentConfig(componentName, forceCustomWrapper = false) {
4415
4477
  const isCustomWrapper = componentName === CUSTOM_WRAPPER;
4416
4478
  const [ATTACHED, DETACHED] = shared.hooks.call('getMiniLifecycleImpl').component;
4417
- const lifeCycles = isCustomWrapper
4479
+ const lifeCycles = isCustomWrapper || forceCustomWrapper
4418
4480
  ? {
4419
4481
  [ATTACHED]() {
4420
- var _a, _b;
4482
+ var _a, _b, _c, _d;
4421
4483
  if (process.env.TARO_ENV === 'tt' && shared.isEnableTTDom()) {
4422
4484
  return;
4423
4485
  }
4424
- 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);
4486
+ 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);
4425
4487
  if (shared.isString(componentId)) {
4426
- customWrapperCache.set(componentId, this);
4488
+ if (isCustomWrapper) {
4489
+ customWrapperCache.set(componentId, this);
4490
+ }
4427
4491
  const el = env.document.getElementById(componentId);
4428
4492
  if (el) {
4429
4493
  el.ctx = this;
4494
+ bumpNearestCtxEpochForRoot(el._root);
4430
4495
  }
4431
4496
  }
4432
4497
  },
4433
4498
  [DETACHED]() {
4434
- var _a, _b;
4499
+ var _a, _b, _c, _d;
4435
4500
  if (process.env.TARO_ENV === 'tt' && shared.isEnableTTDom()) {
4436
4501
  return;
4437
4502
  }
4438
- 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);
4503
+ 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);
4439
4504
  if (shared.isString(componentId)) {
4440
- customWrapperCache.delete(componentId);
4505
+ if (isCustomWrapper) {
4506
+ customWrapperCache.delete(componentId);
4507
+ }
4441
4508
  const el = env.document.getElementById(componentId);
4442
4509
  if (el) {
4443
4510
  el.ctx = null;
4511
+ bumpNearestCtxEpochForRoot(el._root);
4444
4512
  }
4445
4513
  }
4446
4514
  }
@@ -4464,7 +4532,7 @@ function createRecursiveComponentConfig(componentName) {
4464
4532
  }
4465
4533
  }, options: Object.assign(Object.assign({}, extraOptions), { virtualHost: !isCustomWrapper }), methods: {
4466
4534
  eh: eventHandler
4467
- } }, lifeCycles), { isCustomWrapper });
4535
+ } }, lifeCycles), { isCustomWrapper, forceCustomWrapper });
4468
4536
  }
4469
4537
 
4470
4538
  const TIMEOUT = 100;
@@ -5350,6 +5418,7 @@ exports.URLSearchParams = URLSearchParams;
5350
5418
  exports.VALUE = VALUE;
5351
5419
  exports.VIEW = VIEW;
5352
5420
  exports.addLeadingSlash = addLeadingSlash;
5421
+ exports.bumpNearestCtxEpochForRoot = bumpNearestCtxEpochForRoot;
5353
5422
  exports.cancelAnimationFrame = _caf;
5354
5423
  exports.convertNumber2PX = convertNumber2PX;
5355
5424
  exports.createComponentConfig = createComponentConfig;
@@ -5370,6 +5439,7 @@ exports.getComputedStyle = taroGetComputedStyleProvider;
5370
5439
  exports.getCurrentInstance = getCurrentInstance;
5371
5440
  exports.getCurrentPage = getCurrentPage;
5372
5441
  exports.getHomePage = getHomePage;
5442
+ exports.getNearestCtx = getNearestCtx;
5373
5443
  exports.getOnHideEventKey = getOnHideEventKey;
5374
5444
  exports.getOnReadyEventKey = getOnReadyEventKey;
5375
5445
  exports.getOnShowEventKey = getOnShowEventKey;
@@ -5384,6 +5454,7 @@ exports.injectPageInstance = injectPageInstance;
5384
5454
  exports.isComment = isComment;
5385
5455
  exports.isElement = isElement;
5386
5456
  exports.isHasExtractProp = isHasExtractProp;
5457
+ exports.isNearestCtxEnv = isNearestCtxEnv;
5387
5458
  exports.isParentBound = isParentBound;
5388
5459
  exports.isText = isText;
5389
5460
  exports.location = taroLocationProvider;