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