@tarojs/runtime 4.2.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.d.ts CHANGED
@@ -12,6 +12,7 @@ export { taroHistoryProvider as history, taroLocationProvider as location, taroW
12
12
  export { TaroElement } from './dom/element.js';
13
13
  export { TaroEvent, createEvent, eventHandler, eventHandlerTTDom } from './dom/event.js';
14
14
  export { FormElement } from './dom/form.js';
15
+ export { bumpNearestCtxEpochForRoot, getNearestCtx, isNearestCtxEnv } from './dom/nearest-ctx.js';
15
16
  export { TaroNode } from './dom/node.js';
16
17
  export { TaroRootElement } from './dom/root.js';
17
18
  export { Style } from './dom/style.js';
package/dist/index.js CHANGED
@@ -13,6 +13,7 @@ export { taroHistoryProvider as history, taroLocationProvider as location, taroW
13
13
  export { TaroElement } from './dom/element.js';
14
14
  export { TaroEvent, createEvent, eventHandler, eventHandlerTTDom } from './dom/event.js';
15
15
  export { FormElement } from './dom/form.js';
16
+ export { bumpNearestCtxEpochForRoot, getNearestCtx, isNearestCtxEnv } from './dom/nearest-ctx.js';
16
17
  export { TaroNode } from './dom/node.js';
17
18
  export { TaroRootElement } from './dom/root.js';
18
19
  export { Style } from './dom/style.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -242,9 +242,11 @@ declare class TaroRootElement extends TaroElement {
242
242
  private updateCallbacks;
243
243
  pendingUpdate: boolean;
244
244
  ctx: null | MpInstance;
245
+ nearestCtxEpoch: number;
245
246
  constructor();
246
247
  get _path(): string;
247
248
  get _root(): TaroRootElement;
249
+ bumpNearestCtxEpoch(): void;
248
250
  scheduleTask(fn: TFunc): void;
249
251
  enqueueUpdate(payload: UpdatePayload): void;
250
252
  performUpdate(initRender?: boolean, prerender?: TFunc): void;
@@ -268,6 +270,7 @@ declare class TaroNode extends TaroEventTarget {
268
270
  private updateChildNodes;
269
271
  private updateSingleChild;
270
272
  get _root(): TaroRootElement | null;
273
+ get _scope(): MpInstance | null | undefined;
271
274
  protected findIndex(refChild: TaroNode): number;
272
275
  get _path(): string;
273
276
  get nextSibling(): TaroNode | null;
@@ -591,6 +594,10 @@ declare const taroWindowProvider: TaroWindow;
591
594
  declare const taroLocationProvider: TaroLocation;
592
595
  declare const taroHistoryProvider: TaroHistory;
593
596
 
597
+ declare function isNearestCtxEnv(): boolean;
598
+ declare function getNearestCtx(node: TaroNode): MpInstance | null | undefined;
599
+ declare function bumpNearestCtxEpochForRoot(root: TaroRootElement | null | undefined): void;
600
+
594
601
  declare class SVGElement extends TaroElement {
595
602
  }
596
603
 
@@ -861,7 +868,7 @@ declare function getOnShowEventKey(path: string): string;
861
868
  declare function getOnHideEventKey(path: string): string;
862
869
  declare function createPageConfig(component: any, pageName?: string, data?: Record<string, unknown>, pageConfig?: PageConfig): PageInstance;
863
870
  declare function createComponentConfig(component: React.ComponentClass, componentName?: string, data?: Record<string, unknown>): any;
864
- declare function createRecursiveComponentConfig(componentName?: string): any;
871
+ declare function createRecursiveComponentConfig(componentName?: string, forceCustomWrapper?: boolean): any;
865
872
 
866
873
  /**
867
874
  * React also has a fancy function's name for this: `hydrate()`.
@@ -917,5 +924,5 @@ declare function convertNumber2PX(value: number): string;
917
924
 
918
925
  declare function handlePolyfill(): void;
919
926
 
920
- export { A, APP, BEHAVIORS, BODY, CATCHMOVE, CATCH_VIEW, CHANGE, CLASS, CLICK_VIEW, COMMENT, COMPILE_MODE, CONFIRM, CONTAINER, CONTEXT_ACTIONS, CURRENT_TARGET, CUSTOM_WRAPPER, Current, DATASET, DATE, DOCUMENT_ELEMENT_NAME, DOCUMENT_FRAGMENT, EVENT_CALLBACK_RESULT, EXTERNAL_CLASSES, FOCUS, FormElement, HEAD, HOOKS_APP_ID, HTML, History, ID, INPUT, KEY_CODE, Location, MutationObserver, OBJECT, ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, OPTIONS, PAGE_INIT, PROPERTY_THRESHOLD, PROPS, PURE_VIEW, ROOT_STR, SET_DATA, SET_TIMEOUT, STATIC_VIEW, STYLE, SVGElement, Style, TARGET, TARO_RUNTIME, TIME_STAMP, TOUCHMOVE, TYPE, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, UID, TaroURLProvider as URL, URLSearchParams$1 as URLSearchParams, VALUE, VIEW, addLeadingSlash, _caf as cancelAnimationFrame, convertNumber2PX, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, customWrapperCache, debounce, taroDocumentProvider as document, env, eventCenter, eventHandler, eventHandlerTTDom, eventSource, extend, getComponentsAlias, taroGetComputedStyleProvider as getComputedStyle, getCurrentInstance, getCurrentPage, getHomePage, getOnHideEventKey, getOnReadyEventKey, getOnShowEventKey, getPageInstance, getPath, handlePolyfill, hasBasename, taroHistoryProvider as history, hydrate, incrementId, injectPageInstance, isComment, isElement, isHasExtractProp, isParentBound, isText, taroLocationProvider as location, nav as navigator, nextTick, now, options, parseUrl, perf, removePageInstance, _raf as requestAnimationFrame, safeExecute, setInnerHTML, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
927
+ export { A, APP, BEHAVIORS, BODY, CATCHMOVE, CATCH_VIEW, CHANGE, CLASS, CLICK_VIEW, COMMENT, COMPILE_MODE, CONFIRM, CONTAINER, CONTEXT_ACTIONS, CURRENT_TARGET, CUSTOM_WRAPPER, Current, DATASET, DATE, DOCUMENT_ELEMENT_NAME, DOCUMENT_FRAGMENT, EVENT_CALLBACK_RESULT, EXTERNAL_CLASSES, FOCUS, FormElement, HEAD, HOOKS_APP_ID, HTML, History, ID, INPUT, KEY_CODE, Location, MutationObserver, OBJECT, ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, OPTIONS, PAGE_INIT, PROPERTY_THRESHOLD, PROPS, PURE_VIEW, ROOT_STR, SET_DATA, SET_TIMEOUT, STATIC_VIEW, STYLE, SVGElement, Style, TARGET, TARO_RUNTIME, TIME_STAMP, TOUCHMOVE, TYPE, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, UID, TaroURLProvider as URL, URLSearchParams$1 as URLSearchParams, VALUE, VIEW, addLeadingSlash, bumpNearestCtxEpochForRoot, _caf as cancelAnimationFrame, convertNumber2PX, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, customWrapperCache, debounce, taroDocumentProvider as document, env, eventCenter, eventHandler, eventHandlerTTDom, eventSource, extend, getComponentsAlias, taroGetComputedStyleProvider as getComputedStyle, getCurrentInstance, getCurrentPage, getHomePage, getNearestCtx, getOnHideEventKey, getOnReadyEventKey, getOnShowEventKey, getPageInstance, getPath, handlePolyfill, hasBasename, taroHistoryProvider as history, hydrate, incrementId, injectPageInstance, isComment, isElement, isHasExtractProp, isNearestCtxEnv, isParentBound, isText, taroLocationProvider as location, nav as navigator, nextTick, now, options, parseUrl, perf, removePageInstance, _raf as requestAnimationFrame, safeExecute, setInnerHTML, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
921
928
  export type { AddEventListenerOptions, AppInstance, Attributes, DataTree, EventHandler, EventListenerOptions, EventOptions, EventsType, HydratedData, Instance, KeyFrame, MiniData, MiniElementData, MiniTextData, MpEvent, MpInstance, Options$2 as Options, PageConfig, PageInstance, PageLifeCycle, PageProps, ReactAppInstance, ReactPageComponent, ReactPageInstance, ScrollTimelineOption, TFunc, UpdatePayload, UpdatePayloadValue };
@@ -1472,6 +1472,53 @@ class TaroEventTarget {
1472
1472
  }
1473
1473
  }
1474
1474
 
1475
+ const nearestCtxCache = new WeakMap();
1476
+ function isNearestCtxEnv() {
1477
+ return process.env.TARO_ENV === 'weapp' || process.env.TARO_ENV === 'jd';
1478
+ }
1479
+ function getNearestCtx(node) {
1480
+ if (!isNearestCtxEnv()) {
1481
+ return undefined;
1482
+ }
1483
+ const root = node._root;
1484
+ if (root == null) {
1485
+ return null;
1486
+ }
1487
+ const cached = nearestCtxCache.get(node);
1488
+ if (cached && cached.nearestCtxEpoch === root.nearestCtxEpoch) {
1489
+ return cached.value;
1490
+ }
1491
+ const value = resolveNearestCtxValue(node, root);
1492
+ nearestCtxCache.set(node, { value, nearestCtxEpoch: root.nearestCtxEpoch });
1493
+ return value;
1494
+ }
1495
+ function resolveNearestCtxValue(node, root) {
1496
+ var _a;
1497
+ if (node.nodeType === 1 /* NodeType.ELEMENT_NODE */) {
1498
+ const ctx = node.ctx;
1499
+ if (ctx != null)
1500
+ return ctx;
1501
+ }
1502
+ let current = node.parentNode;
1503
+ while (current) {
1504
+ if (current.nodeType === 1 /* NodeType.ELEMENT_NODE */) {
1505
+ const ctx = current.ctx;
1506
+ if (ctx != null)
1507
+ return ctx;
1508
+ }
1509
+ current = current.parentNode;
1510
+ }
1511
+ return (_a = root.ctx) !== null && _a !== void 0 ? _a : null;
1512
+ }
1513
+ function bumpNearestCtxEpochForRoot(root) {
1514
+ if (!isNearestCtxEnv() || root == null) {
1515
+ return;
1516
+ }
1517
+ if (typeof root.bumpNearestCtxEpoch === 'function') {
1518
+ root.bumpNearestCtxEpoch();
1519
+ }
1520
+ }
1521
+
1475
1522
  const CHILDNODES = "cn" /* Shortcuts.Childnodes */;
1476
1523
  const nodeId = incrementId();
1477
1524
  class TaroNode extends TaroEventTarget {
@@ -1511,6 +1558,9 @@ class TaroNode extends TaroEventTarget {
1511
1558
  var _a;
1512
1559
  return ((_a = this.parentNode) === null || _a === void 0 ? void 0 : _a._root) || null;
1513
1560
  }
1561
+ get _scope() {
1562
+ return getNearestCtx(this);
1563
+ }
1514
1564
  findIndex(refChild) {
1515
1565
  const index = this.childNodes.indexOf(refChild);
1516
1566
  ensure(index !== -1, 'The node to be replaced is not a child of this node.');
@@ -1656,6 +1706,9 @@ class TaroNode extends TaroEventTarget {
1656
1706
  }
1657
1707
  }
1658
1708
  }
1709
+ if (this._root) {
1710
+ bumpNearestCtxEpochForRoot(this._root);
1711
+ }
1659
1712
  MutationObserver$1.record({
1660
1713
  type: "childList" /* MutationRecordType.CHILD_LIST */,
1661
1714
  target: this,
@@ -1732,6 +1785,9 @@ class TaroNode extends TaroEventTarget {
1732
1785
  if (this._root && doUpdate !== false) {
1733
1786
  this.updateChildNodes();
1734
1787
  }
1788
+ if (this._root) {
1789
+ bumpNearestCtxEpochForRoot(this._root);
1790
+ }
1735
1791
  return child;
1736
1792
  }
1737
1793
  remove(options) {
@@ -3643,6 +3699,7 @@ class TaroRootElement extends TaroElement {
3643
3699
  this.updateCallbacks = [];
3644
3700
  this.pendingUpdate = false;
3645
3701
  this.ctx = null;
3702
+ this.nearestCtxEpoch = 0;
3646
3703
  this.nodeName = ROOT_STR;
3647
3704
  this.tagName = ROOT_STR.toUpperCase();
3648
3705
  }
@@ -3652,6 +3709,9 @@ class TaroRootElement extends TaroElement {
3652
3709
  get _root() {
3653
3710
  return this;
3654
3711
  }
3712
+ bumpNearestCtxEpoch() {
3713
+ this.nearestCtxEpoch++;
3714
+ }
3655
3715
  scheduleTask(fn) {
3656
3716
  // 这里若使用微任务可略微提前setData的执行时机,但在部分场景下可能会出现连续setData两次,造成更大的性能问题
3657
3717
  setTimeout(fn);
@@ -4215,6 +4275,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4215
4275
  pageElement.sync();
4216
4276
  }
4217
4277
  else {
4278
+ bumpNearestCtxEpochForRoot(pageElement);
4218
4279
  pageElement.performUpdate(true, cb);
4219
4280
  }
4220
4281
  }
@@ -4243,6 +4304,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4243
4304
  unmounting = false;
4244
4305
  instances.delete($taroPath);
4245
4306
  if (pageElement) {
4307
+ bumpNearestCtxEpochForRoot(pageElement);
4246
4308
  pageElement.ctx = null;
4247
4309
  pageElement = null;
4248
4310
  }
@@ -4405,36 +4467,42 @@ function createComponentConfig(component, componentName, data) {
4405
4467
  });
4406
4468
  return config;
4407
4469
  }
4408
- function createRecursiveComponentConfig(componentName) {
4470
+ function createRecursiveComponentConfig(componentName, forceCustomWrapper = false) {
4409
4471
  const isCustomWrapper = componentName === CUSTOM_WRAPPER;
4410
4472
  const [ATTACHED, DETACHED] = hooks.call('getMiniLifecycleImpl').component;
4411
- const lifeCycles = isCustomWrapper
4473
+ const lifeCycles = isCustomWrapper || forceCustomWrapper
4412
4474
  ? {
4413
4475
  [ATTACHED]() {
4414
- var _a, _b;
4476
+ var _a, _b, _c, _d;
4415
4477
  if (process.env.TARO_ENV === 'tt' && isEnableTTDom()) {
4416
4478
  return;
4417
4479
  }
4418
- 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);
4480
+ 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);
4419
4481
  if (isString(componentId)) {
4420
- customWrapperCache.set(componentId, this);
4482
+ if (isCustomWrapper) {
4483
+ customWrapperCache.set(componentId, this);
4484
+ }
4421
4485
  const el = env.document.getElementById(componentId);
4422
4486
  if (el) {
4423
4487
  el.ctx = this;
4488
+ bumpNearestCtxEpochForRoot(el._root);
4424
4489
  }
4425
4490
  }
4426
4491
  },
4427
4492
  [DETACHED]() {
4428
- var _a, _b;
4493
+ var _a, _b, _c, _d;
4429
4494
  if (process.env.TARO_ENV === 'tt' && isEnableTTDom()) {
4430
4495
  return;
4431
4496
  }
4432
- 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);
4497
+ 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);
4433
4498
  if (isString(componentId)) {
4434
- customWrapperCache.delete(componentId);
4499
+ if (isCustomWrapper) {
4500
+ customWrapperCache.delete(componentId);
4501
+ }
4435
4502
  const el = env.document.getElementById(componentId);
4436
4503
  if (el) {
4437
4504
  el.ctx = null;
4505
+ bumpNearestCtxEpochForRoot(el._root);
4438
4506
  }
4439
4507
  }
4440
4508
  }
@@ -4458,7 +4526,7 @@ function createRecursiveComponentConfig(componentName) {
4458
4526
  }
4459
4527
  }, options: Object.assign(Object.assign({}, extraOptions), { virtualHost: !isCustomWrapper }), methods: {
4460
4528
  eh: eventHandler
4461
- } }, lifeCycles), { isCustomWrapper });
4529
+ } }, lifeCycles), { isCustomWrapper, forceCustomWrapper });
4462
4530
  }
4463
4531
 
4464
4532
  const TIMEOUT = 100;
@@ -5270,5 +5338,5 @@ if (process.env.SUPPORT_TARO_POLYFILL !== 'disabled' && process.env.TARO_PLATFOR
5270
5338
  handlePolyfill();
5271
5339
  }
5272
5340
 
5273
- export { A, APP, BEHAVIORS, BODY, CATCHMOVE, CATCH_VIEW, CHANGE, CLASS, CLICK_VIEW, COMMENT, COMPILE_MODE, CONFIRM, CONTAINER, CONTEXT_ACTIONS, CURRENT_TARGET, CUSTOM_WRAPPER, Current, DATASET, DATE, DOCUMENT_ELEMENT_NAME, DOCUMENT_FRAGMENT, EVENT_CALLBACK_RESULT, EXTERNAL_CLASSES, FOCUS, FormElement, HEAD, HOOKS_APP_ID, HTML, History, ID, INPUT, KEY_CODE, Location, MutationObserver$1 as MutationObserver, OBJECT, ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, OPTIONS, PAGE_INIT, PROPERTY_THRESHOLD, PROPS, PURE_VIEW, ROOT_STR, SET_DATA, SET_TIMEOUT, STATIC_VIEW, STYLE, SVGElement, Style, TARGET, TARO_RUNTIME, TIME_STAMP, TOUCHMOVE, TYPE, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, UID, TaroURLProvider as URL, URLSearchParams, VALUE, VIEW, addLeadingSlash, _caf as cancelAnimationFrame, convertNumber2PX, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, customWrapperCache, debounce, taroDocumentProvider as document, env, eventCenter, eventHandler, eventHandlerTTDom, eventSource, extend, getComponentsAlias, taroGetComputedStyleProvider as getComputedStyle, getCurrentInstance, getCurrentPage, getHomePage, getOnHideEventKey, getOnReadyEventKey, getOnShowEventKey, getPageInstance, getPath, handlePolyfill, hasBasename, taroHistoryProvider as history, hydrate, incrementId, injectPageInstance, isComment, isElement, isHasExtractProp, isParentBound, isText, taroLocationProvider as location, nav as navigator, nextTick, now, options, parseUrl, perf, removePageInstance, _raf as requestAnimationFrame, safeExecute, setInnerHTML, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
5341
+ export { A, APP, BEHAVIORS, BODY, CATCHMOVE, CATCH_VIEW, CHANGE, CLASS, CLICK_VIEW, COMMENT, COMPILE_MODE, CONFIRM, CONTAINER, CONTEXT_ACTIONS, CURRENT_TARGET, CUSTOM_WRAPPER, Current, DATASET, DATE, DOCUMENT_ELEMENT_NAME, DOCUMENT_FRAGMENT, EVENT_CALLBACK_RESULT, EXTERNAL_CLASSES, FOCUS, FormElement, HEAD, HOOKS_APP_ID, HTML, History, ID, INPUT, KEY_CODE, Location, MutationObserver$1 as MutationObserver, OBJECT, ON_HIDE, ON_LOAD, ON_READY, ON_SHOW, OPTIONS, PAGE_INIT, PROPERTY_THRESHOLD, PROPS, PURE_VIEW, ROOT_STR, SET_DATA, SET_TIMEOUT, STATIC_VIEW, STYLE, SVGElement, Style, TARGET, TARO_RUNTIME, TIME_STAMP, TOUCHMOVE, TYPE, TaroElement, TaroEvent, TaroNode, TaroRootElement, TaroText, UID, TaroURLProvider as URL, URLSearchParams, VALUE, VIEW, addLeadingSlash, bumpNearestCtxEpochForRoot, _caf as cancelAnimationFrame, convertNumber2PX, createComponentConfig, createEvent, createPageConfig, createRecursiveComponentConfig, customWrapperCache, debounce, taroDocumentProvider as document, env, eventCenter, eventHandler, eventHandlerTTDom, eventSource, extend, getComponentsAlias, taroGetComputedStyleProvider as getComputedStyle, getCurrentInstance, getCurrentPage, getHomePage, getNearestCtx, getOnHideEventKey, getOnReadyEventKey, getOnShowEventKey, getPageInstance, getPath, handlePolyfill, hasBasename, taroHistoryProvider as history, hydrate, incrementId, injectPageInstance, isComment, isElement, isHasExtractProp, isNearestCtxEnv, isParentBound, isText, taroLocationProvider as location, nav as navigator, nextTick, now, options, parseUrl, perf, removePageInstance, _raf as requestAnimationFrame, safeExecute, setInnerHTML, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
5274
5342
  //# sourceMappingURL=runtime.esm.js.map