@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.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 } 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 } 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -241,9 +241,11 @@ declare class TaroRootElement extends TaroElement {
241
241
  private updateCallbacks;
242
242
  pendingUpdate: boolean;
243
243
  ctx: null | MpInstance;
244
+ nearestCtxEpoch: number;
244
245
  constructor();
245
246
  get _path(): string;
246
247
  get _root(): TaroRootElement;
248
+ bumpNearestCtxEpoch(): void;
247
249
  scheduleTask(fn: TFunc): void;
248
250
  enqueueUpdate(payload: UpdatePayload): void;
249
251
  performUpdate(initRender?: boolean, prerender?: TFunc): void;
@@ -267,6 +269,7 @@ declare class TaroNode extends TaroEventTarget {
267
269
  private updateChildNodes;
268
270
  private updateSingleChild;
269
271
  get _root(): TaroRootElement | null;
272
+ get _scope(): MpInstance | null | undefined;
270
273
  protected findIndex(refChild: TaroNode): number;
271
274
  get _path(): string;
272
275
  get nextSibling(): TaroNode | null;
@@ -589,6 +592,10 @@ declare const taroWindowProvider: TaroWindow;
589
592
  declare const taroLocationProvider: TaroLocation;
590
593
  declare const taroHistoryProvider: TaroHistory;
591
594
 
595
+ declare function isNearestCtxEnv(): boolean;
596
+ declare function getNearestCtx(node: TaroNode): MpInstance | null | undefined;
597
+ declare function bumpNearestCtxEpochForRoot(root: TaroRootElement | null | undefined): void;
598
+
592
599
  declare class SVGElement extends TaroElement {
593
600
  }
594
601
 
@@ -857,7 +864,7 @@ declare function getOnShowEventKey(path: string): string;
857
864
  declare function getOnHideEventKey(path: string): string;
858
865
  declare function createPageConfig(component: any, pageName?: string, data?: Record<string, unknown>, pageConfig?: PageConfig): PageInstance;
859
866
  declare function createComponentConfig(component: React.ComponentClass, componentName?: string, data?: Record<string, unknown>): any;
860
- declare function createRecursiveComponentConfig(componentName?: string): any;
867
+ declare function createRecursiveComponentConfig(componentName?: string, forceCustomWrapper?: boolean): any;
861
868
 
862
869
  /**
863
870
  * React also has a fancy function's name for this: `hydrate()`.
@@ -913,5 +920,5 @@ declare function convertNumber2PX(value: number): string;
913
920
 
914
921
  declare function handlePolyfill(): void;
915
922
 
916
- 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, 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, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
923
+ 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, 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, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
917
924
  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) {
@@ -3629,6 +3685,7 @@ class TaroRootElement extends TaroElement {
3629
3685
  this.updateCallbacks = [];
3630
3686
  this.pendingUpdate = false;
3631
3687
  this.ctx = null;
3688
+ this.nearestCtxEpoch = 0;
3632
3689
  this.nodeName = ROOT_STR;
3633
3690
  this.tagName = ROOT_STR.toUpperCase();
3634
3691
  }
@@ -3638,6 +3695,9 @@ class TaroRootElement extends TaroElement {
3638
3695
  get _root() {
3639
3696
  return this;
3640
3697
  }
3698
+ bumpNearestCtxEpoch() {
3699
+ this.nearestCtxEpoch++;
3700
+ }
3641
3701
  scheduleTask(fn) {
3642
3702
  // 这里若使用微任务可略微提前setData的执行时机,但在部分场景下可能会出现连续setData两次,造成更大的性能问题
3643
3703
  setTimeout(fn);
@@ -4060,6 +4120,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4060
4120
  loadResolver();
4061
4121
  if (process.env.TARO_PLATFORM !== 'web') {
4062
4122
  pageElement.ctx = this;
4123
+ bumpNearestCtxEpochForRoot(pageElement);
4063
4124
  pageElement.performUpdate(true, cb);
4064
4125
  }
4065
4126
  else {
@@ -4087,6 +4148,7 @@ function createPageConfig(component, pageName, data, pageConfig) {
4087
4148
  unmounting = false;
4088
4149
  instances.delete($taroPath);
4089
4150
  if (pageElement) {
4151
+ bumpNearestCtxEpochForRoot(pageElement);
4090
4152
  pageElement.ctx = null;
4091
4153
  pageElement = null;
4092
4154
  }
@@ -4247,30 +4309,36 @@ function createComponentConfig(component, componentName, data) {
4247
4309
  });
4248
4310
  return config;
4249
4311
  }
4250
- function createRecursiveComponentConfig(componentName) {
4312
+ function createRecursiveComponentConfig(componentName, forceCustomWrapper = false) {
4251
4313
  const isCustomWrapper = componentName === CUSTOM_WRAPPER;
4252
4314
  const [ATTACHED, DETACHED] = hooks.call('getMiniLifecycleImpl').component;
4253
- const lifeCycles = isCustomWrapper
4315
+ const lifeCycles = isCustomWrapper || forceCustomWrapper
4254
4316
  ? {
4255
4317
  [ATTACHED]() {
4256
- var _a, _b;
4257
- 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);
4318
+ var _a, _b, _c, _d;
4319
+ 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);
4258
4320
  if (isString(componentId)) {
4259
- customWrapperCache.set(componentId, this);
4321
+ if (isCustomWrapper) {
4322
+ customWrapperCache.set(componentId, this);
4323
+ }
4260
4324
  const el = env.document.getElementById(componentId);
4261
4325
  if (el) {
4262
4326
  el.ctx = this;
4327
+ bumpNearestCtxEpochForRoot(el._root);
4263
4328
  }
4264
4329
  }
4265
4330
  },
4266
4331
  [DETACHED]() {
4267
- var _a, _b;
4268
- 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);
4332
+ var _a, _b, _c, _d;
4333
+ 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);
4269
4334
  if (isString(componentId)) {
4270
- customWrapperCache.delete(componentId);
4335
+ if (isCustomWrapper) {
4336
+ customWrapperCache.delete(componentId);
4337
+ }
4271
4338
  const el = env.document.getElementById(componentId);
4272
4339
  if (el) {
4273
4340
  el.ctx = null;
4341
+ bumpNearestCtxEpochForRoot(el._root);
4274
4342
  }
4275
4343
  }
4276
4344
  }
@@ -4294,7 +4362,7 @@ function createRecursiveComponentConfig(componentName) {
4294
4362
  }
4295
4363
  }, options: Object.assign(Object.assign({}, extraOptions), { virtualHost: !isCustomWrapper }), methods: {
4296
4364
  eh: eventHandler
4297
- } }, lifeCycles), { isCustomWrapper });
4365
+ } }, lifeCycles), { isCustomWrapper, forceCustomWrapper });
4298
4366
  }
4299
4367
 
4300
4368
  const TIMEOUT = 100;
@@ -5106,5 +5174,5 @@ if (process.env.SUPPORT_TARO_POLYFILL !== 'disabled' && process.env.TARO_PLATFOR
5106
5174
  handlePolyfill();
5107
5175
  }
5108
5176
 
5109
- 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, 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, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
5177
+ 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, 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, shortcutAttr, stringify, stripBasename, stripSuffix, stripTrailing, throttle, taroWindowProvider as window };
5110
5178
  //# sourceMappingURL=runtime.esm.js.map