@tarojs/runtime 4.2.1-beta.0 → 4.2.1-beta.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/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);
@@ -4244,6 +4304,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4244
4304
  unmounting = false;
4245
4305
  instances.delete($taroPath);
4246
4306
  if (pageElement) {
4307
+ bumpNearestCtxEpochForRoot(pageElement);
4247
4308
  pageElement.ctx = null;
4248
4309
  pageElement = null;
4249
4310
  }
@@ -4406,10 +4467,10 @@ function createComponentConfig(component, componentName, data) {
4406
4467
  });
4407
4468
  return config;
4408
4469
  }
4409
- function createRecursiveComponentConfig(componentName) {
4470
+ function createRecursiveComponentConfig(componentName, forceCustomWrapper = false) {
4410
4471
  const isCustomWrapper = componentName === CUSTOM_WRAPPER;
4411
4472
  const [ATTACHED, DETACHED] = shared.hooks.call('getMiniLifecycleImpl').component;
4412
- const lifeCycles = isCustomWrapper
4473
+ const lifeCycles = isCustomWrapper || forceCustomWrapper
4413
4474
  ? {
4414
4475
  [ATTACHED]() {
4415
4476
  var _a, _b;
@@ -4418,10 +4479,13 @@ function createRecursiveComponentConfig(componentName) {
4418
4479
  }
4419
4480
  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);
4420
4481
  if (shared.isString(componentId)) {
4421
- customWrapperCache.set(componentId, this);
4482
+ if (isCustomWrapper) {
4483
+ customWrapperCache.set(componentId, this);
4484
+ }
4422
4485
  const el = env.document.getElementById(componentId);
4423
4486
  if (el) {
4424
4487
  el.ctx = this;
4488
+ bumpNearestCtxEpochForRoot(el._root);
4425
4489
  }
4426
4490
  }
4427
4491
  },
@@ -4432,10 +4496,13 @@ function createRecursiveComponentConfig(componentName) {
4432
4496
  }
4433
4497
  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);
4434
4498
  if (shared.isString(componentId)) {
4435
- customWrapperCache.delete(componentId);
4499
+ if (isCustomWrapper) {
4500
+ customWrapperCache.delete(componentId);
4501
+ }
4436
4502
  const el = env.document.getElementById(componentId);
4437
4503
  if (el) {
4438
4504
  el.ctx = null;
4505
+ bumpNearestCtxEpochForRoot(el._root);
4439
4506
  }
4440
4507
  }
4441
4508
  }
@@ -4459,7 +4526,7 @@ function createRecursiveComponentConfig(componentName) {
4459
4526
  }
4460
4527
  }, options: Object.assign(Object.assign({}, extraOptions), { virtualHost: !isCustomWrapper }), methods: {
4461
4528
  eh: eventHandler
4462
- } }, lifeCycles), { isCustomWrapper });
4529
+ } }, lifeCycles), { isCustomWrapper, forceCustomWrapper });
4463
4530
  }
4464
4531
 
4465
4532
  const TIMEOUT = 100;
@@ -5345,6 +5412,7 @@ exports.URLSearchParams = URLSearchParams;
5345
5412
  exports.VALUE = VALUE;
5346
5413
  exports.VIEW = VIEW;
5347
5414
  exports.addLeadingSlash = addLeadingSlash;
5415
+ exports.bumpNearestCtxEpochForRoot = bumpNearestCtxEpochForRoot;
5348
5416
  exports.cancelAnimationFrame = _caf;
5349
5417
  exports.convertNumber2PX = convertNumber2PX;
5350
5418
  exports.createComponentConfig = createComponentConfig;
@@ -5365,6 +5433,7 @@ exports.getComputedStyle = taroGetComputedStyleProvider;
5365
5433
  exports.getCurrentInstance = getCurrentInstance;
5366
5434
  exports.getCurrentPage = getCurrentPage;
5367
5435
  exports.getHomePage = getHomePage;
5436
+ exports.getNearestCtx = getNearestCtx;
5368
5437
  exports.getOnHideEventKey = getOnHideEventKey;
5369
5438
  exports.getOnReadyEventKey = getOnReadyEventKey;
5370
5439
  exports.getOnShowEventKey = getOnShowEventKey;
@@ -5379,6 +5448,7 @@ exports.injectPageInstance = injectPageInstance;
5379
5448
  exports.isComment = isComment;
5380
5449
  exports.isElement = isElement;
5381
5450
  exports.isHasExtractProp = isHasExtractProp;
5451
+ exports.isNearestCtxEnv = isNearestCtxEnv;
5382
5452
  exports.isParentBound = isParentBound;
5383
5453
  exports.isText = isText;
5384
5454
  exports.location = taroLocationProvider;