@tarojs/runtime 4.1.12-beta.4 → 4.1.12-beta.40

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) {
@@ -3630,6 +3686,7 @@ class TaroRootElement extends TaroElement {
3630
3686
  this.updateCallbacks = [];
3631
3687
  this.pendingUpdate = false;
3632
3688
  this.ctx = null;
3689
+ this.nearestCtxEpoch = 0;
3633
3690
  this.nodeName = ROOT_STR;
3634
3691
  this.tagName = ROOT_STR.toUpperCase();
3635
3692
  }
@@ -3639,6 +3696,9 @@ class TaroRootElement extends TaroElement {
3639
3696
  get _root() {
3640
3697
  return this;
3641
3698
  }
3699
+ bumpNearestCtxEpoch() {
3700
+ this.nearestCtxEpoch++;
3701
+ }
3642
3702
  scheduleTask(fn) {
3643
3703
  // 这里若使用微任务可略微提前setData的执行时机,但在部分场景下可能会出现连续setData两次,造成更大的性能问题
3644
3704
  setTimeout(fn);
@@ -4061,6 +4121,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4061
4121
  loadResolver();
4062
4122
  if (process.env.TARO_PLATFORM !== 'web') {
4063
4123
  pageElement.ctx = this;
4124
+ bumpNearestCtxEpochForRoot(pageElement);
4064
4125
  pageElement.performUpdate(true, cb);
4065
4126
  }
4066
4127
  else {
@@ -4088,6 +4149,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4088
4149
  unmounting = false;
4089
4150
  instances.delete($taroPath);
4090
4151
  if (pageElement) {
4152
+ bumpNearestCtxEpochForRoot(pageElement);
4091
4153
  pageElement.ctx = null;
4092
4154
  pageElement = null;
4093
4155
  }
@@ -4248,30 +4310,36 @@ function createComponentConfig(component, componentName, data) {
4248
4310
  });
4249
4311
  return config;
4250
4312
  }
4251
- function createRecursiveComponentConfig(componentName) {
4313
+ function createRecursiveComponentConfig(componentName, forceCustomWrapper = false) {
4252
4314
  const isCustomWrapper = componentName === CUSTOM_WRAPPER;
4253
4315
  const [ATTACHED, DETACHED] = shared.hooks.call('getMiniLifecycleImpl').component;
4254
- const lifeCycles = isCustomWrapper
4316
+ const lifeCycles = isCustomWrapper || forceCustomWrapper
4255
4317
  ? {
4256
4318
  [ATTACHED]() {
4257
- var _a, _b;
4258
- 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);
4319
+ var _a, _b, _c, _d;
4320
+ 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);
4259
4321
  if (shared.isString(componentId)) {
4260
- customWrapperCache.set(componentId, this);
4322
+ if (isCustomWrapper) {
4323
+ customWrapperCache.set(componentId, this);
4324
+ }
4261
4325
  const el = env.document.getElementById(componentId);
4262
4326
  if (el) {
4263
4327
  el.ctx = this;
4328
+ bumpNearestCtxEpochForRoot(el._root);
4264
4329
  }
4265
4330
  }
4266
4331
  },
4267
4332
  [DETACHED]() {
4268
- var _a, _b;
4269
- 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);
4333
+ var _a, _b, _c, _d;
4334
+ 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);
4270
4335
  if (shared.isString(componentId)) {
4271
- customWrapperCache.delete(componentId);
4336
+ if (isCustomWrapper) {
4337
+ customWrapperCache.delete(componentId);
4338
+ }
4272
4339
  const el = env.document.getElementById(componentId);
4273
4340
  if (el) {
4274
4341
  el.ctx = null;
4342
+ bumpNearestCtxEpochForRoot(el._root);
4275
4343
  }
4276
4344
  }
4277
4345
  }
@@ -4295,7 +4363,7 @@ function createRecursiveComponentConfig(componentName) {
4295
4363
  }
4296
4364
  }, options: Object.assign(Object.assign({}, extraOptions), { virtualHost: !isCustomWrapper }), methods: {
4297
4365
  eh: eventHandler
4298
- } }, lifeCycles), { isCustomWrapper });
4366
+ } }, lifeCycles), { isCustomWrapper, forceCustomWrapper });
4299
4367
  }
4300
4368
 
4301
4369
  const TIMEOUT = 100;
@@ -5181,6 +5249,7 @@ exports.URLSearchParams = URLSearchParams;
5181
5249
  exports.VALUE = VALUE;
5182
5250
  exports.VIEW = VIEW;
5183
5251
  exports.addLeadingSlash = addLeadingSlash;
5252
+ exports.bumpNearestCtxEpochForRoot = bumpNearestCtxEpochForRoot;
5184
5253
  exports.cancelAnimationFrame = _caf;
5185
5254
  exports.convertNumber2PX = convertNumber2PX;
5186
5255
  exports.createComponentConfig = createComponentConfig;
@@ -5200,6 +5269,7 @@ exports.getComputedStyle = taroGetComputedStyleProvider;
5200
5269
  exports.getCurrentInstance = getCurrentInstance;
5201
5270
  exports.getCurrentPage = getCurrentPage;
5202
5271
  exports.getHomePage = getHomePage;
5272
+ exports.getNearestCtx = getNearestCtx;
5203
5273
  exports.getOnHideEventKey = getOnHideEventKey;
5204
5274
  exports.getOnReadyEventKey = getOnReadyEventKey;
5205
5275
  exports.getOnShowEventKey = getOnShowEventKey;
@@ -5214,6 +5284,7 @@ exports.injectPageInstance = injectPageInstance;
5214
5284
  exports.isComment = isComment;
5215
5285
  exports.isElement = isElement;
5216
5286
  exports.isHasExtractProp = isHasExtractProp;
5287
+ exports.isNearestCtxEnv = isNearestCtxEnv;
5217
5288
  exports.isParentBound = isParentBound;
5218
5289
  exports.isText = isText;
5219
5290
  exports.location = taroLocationProvider;