@tarojs/runtime 4.1.12-beta.32 → 4.1.12-beta.34

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,48 @@ 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
+ root === null || root === void 0 ? void 0 : root.bumpNearestCtxEpoch();
1516
+ }
1517
+
1476
1518
  const CHILDNODES = "cn" /* Shortcuts.Childnodes */;
1477
1519
  const nodeId = incrementId();
1478
1520
  class TaroNode extends TaroEventTarget {
@@ -1512,6 +1554,9 @@ class TaroNode extends TaroEventTarget {
1512
1554
  var _a;
1513
1555
  return ((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a._root) || null;
1514
1556
  }
1557
+ get _scope() {
1558
+ return getNearestCtx(this);
1559
+ }
1515
1560
  findIndex(refChild) {
1516
1561
  const index = this.childNodes.indexOf(refChild);
1517
1562
  shared.ensure(index !== -1, 'The node to be replaced is not a child of this node.');
@@ -1657,6 +1702,9 @@ class TaroNode extends TaroEventTarget {
1657
1702
  }
1658
1703
  }
1659
1704
  }
1705
+ if (this._root) {
1706
+ bumpNearestCtxEpochForRoot(this._root);
1707
+ }
1660
1708
  MutationObserver$1.record({
1661
1709
  type: "childList" /* MutationRecordType.CHILD_LIST */,
1662
1710
  target: this,
@@ -1733,6 +1781,9 @@ class TaroNode extends TaroEventTarget {
1733
1781
  if (this._root && doUpdate !== false) {
1734
1782
  this.updateChildNodes();
1735
1783
  }
1784
+ if (this._root) {
1785
+ bumpNearestCtxEpochForRoot(this._root);
1786
+ }
1736
1787
  return child;
1737
1788
  }
1738
1789
  remove(options) {
@@ -3630,6 +3681,7 @@ class TaroRootElement extends TaroElement {
3630
3681
  this.updateCallbacks = [];
3631
3682
  this.pendingUpdate = false;
3632
3683
  this.ctx = null;
3684
+ this.nearestCtxEpoch = 0;
3633
3685
  this.nodeName = ROOT_STR;
3634
3686
  this.tagName = ROOT_STR.toUpperCase();
3635
3687
  }
@@ -3639,6 +3691,9 @@ class TaroRootElement extends TaroElement {
3639
3691
  get _root() {
3640
3692
  return this;
3641
3693
  }
3694
+ bumpNearestCtxEpoch() {
3695
+ this.nearestCtxEpoch++;
3696
+ }
3642
3697
  scheduleTask(fn) {
3643
3698
  // 这里若使用微任务可略微提前setData的执行时机,但在部分场景下可能会出现连续setData两次,造成更大的性能问题
3644
3699
  setTimeout(fn);
@@ -4061,6 +4116,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4061
4116
  loadResolver();
4062
4117
  if (process.env.TARO_PLATFORM !== 'web') {
4063
4118
  pageElement.ctx = this;
4119
+ bumpNearestCtxEpochForRoot(pageElement);
4064
4120
  pageElement.performUpdate(true, cb);
4065
4121
  }
4066
4122
  else {
@@ -4088,6 +4144,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4088
4144
  unmounting = false;
4089
4145
  instances.delete($taroPath);
4090
4146
  if (pageElement) {
4147
+ bumpNearestCtxEpochForRoot(pageElement);
4091
4148
  pageElement.ctx = null;
4092
4149
  pageElement = null;
4093
4150
  }
@@ -4248,19 +4305,22 @@ function createComponentConfig(component, componentName, data) {
4248
4305
  });
4249
4306
  return config;
4250
4307
  }
4251
- function createRecursiveComponentConfig(componentName) {
4308
+ function createRecursiveComponentConfig(componentName, forceCustomWrapper = false) {
4252
4309
  const isCustomWrapper = componentName === CUSTOM_WRAPPER;
4253
4310
  const [ATTACHED, DETACHED] = shared.hooks.call('getMiniLifecycleImpl').component;
4254
- const lifeCycles = isCustomWrapper
4311
+ const lifeCycles = isCustomWrapper || forceCustomWrapper
4255
4312
  ? {
4256
4313
  [ATTACHED]() {
4257
4314
  var _a, _b;
4258
4315
  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);
4259
4316
  if (shared.isString(componentId)) {
4260
- customWrapperCache.set(componentId, this);
4317
+ if (isCustomWrapper) {
4318
+ customWrapperCache.set(componentId, this);
4319
+ }
4261
4320
  const el = env.document.getElementById(componentId);
4262
4321
  if (el) {
4263
4322
  el.ctx = this;
4323
+ bumpNearestCtxEpochForRoot(el._root);
4264
4324
  }
4265
4325
  }
4266
4326
  },
@@ -4268,10 +4328,13 @@ function createRecursiveComponentConfig(componentName) {
4268
4328
  var _a, _b;
4269
4329
  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);
4270
4330
  if (shared.isString(componentId)) {
4271
- customWrapperCache.delete(componentId);
4331
+ if (isCustomWrapper) {
4332
+ customWrapperCache.delete(componentId);
4333
+ }
4272
4334
  const el = env.document.getElementById(componentId);
4273
4335
  if (el) {
4274
4336
  el.ctx = null;
4337
+ bumpNearestCtxEpochForRoot(el._root);
4275
4338
  }
4276
4339
  }
4277
4340
  }
@@ -4295,7 +4358,7 @@ function createRecursiveComponentConfig(componentName) {
4295
4358
  }
4296
4359
  }, options: Object.assign(Object.assign({}, extraOptions), { virtualHost: !isCustomWrapper }), methods: {
4297
4360
  eh: eventHandler
4298
- } }, lifeCycles), { isCustomWrapper });
4361
+ } }, lifeCycles), { isCustomWrapper, forceCustomWrapper });
4299
4362
  }
4300
4363
 
4301
4364
  const TIMEOUT = 100;
@@ -5181,6 +5244,7 @@ exports.URLSearchParams = URLSearchParams;
5181
5244
  exports.VALUE = VALUE;
5182
5245
  exports.VIEW = VIEW;
5183
5246
  exports.addLeadingSlash = addLeadingSlash;
5247
+ exports.bumpNearestCtxEpochForRoot = bumpNearestCtxEpochForRoot;
5184
5248
  exports.cancelAnimationFrame = _caf;
5185
5249
  exports.convertNumber2PX = convertNumber2PX;
5186
5250
  exports.createComponentConfig = createComponentConfig;
@@ -5200,6 +5264,7 @@ exports.getComputedStyle = taroGetComputedStyleProvider;
5200
5264
  exports.getCurrentInstance = getCurrentInstance;
5201
5265
  exports.getCurrentPage = getCurrentPage;
5202
5266
  exports.getHomePage = getHomePage;
5267
+ exports.getNearestCtx = getNearestCtx;
5203
5268
  exports.getOnHideEventKey = getOnHideEventKey;
5204
5269
  exports.getOnReadyEventKey = getOnReadyEventKey;
5205
5270
  exports.getOnShowEventKey = getOnShowEventKey;
@@ -5214,6 +5279,7 @@ exports.injectPageInstance = injectPageInstance;
5214
5279
  exports.isComment = isComment;
5215
5280
  exports.isElement = isElement;
5216
5281
  exports.isHasExtractProp = isHasExtractProp;
5282
+ exports.isNearestCtxEnv = isNearestCtxEnv;
5217
5283
  exports.isParentBound = isParentBound;
5218
5284
  exports.isText = isText;
5219
5285
  exports.location = taroLocationProvider;