@tarojs/router 3.6.0-canary.5 → 3.6.0-canary.7

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/api.js CHANGED
@@ -39,6 +39,7 @@ function processNavigateUrl(option) {
39
39
  function navigate(option, method) {
40
40
  return __awaiter(this, void 0, void 0, function* () {
41
41
  return new Promise((resolve, reject) => {
42
+ stacks.method = method;
42
43
  const { success, complete, fail } = option;
43
44
  const unListen = history.listen(() => {
44
45
  const res = { errMsg: `${method}:ok` };
package/dist/index.cjs.js CHANGED
@@ -212,6 +212,8 @@ class Stacks {
212
212
  constructor() {
213
213
  this.stacks = [];
214
214
  this.backDelta = 0;
215
+ this.tabs = {};
216
+ this.methodName = '';
215
217
  }
216
218
  set delta(delta) {
217
219
  if (delta > 0) {
@@ -227,6 +229,12 @@ class Stacks {
227
229
  get delta() {
228
230
  return this.backDelta;
229
231
  }
232
+ set method(methodName) {
233
+ this.methodName = methodName;
234
+ }
235
+ get method() {
236
+ return this.methodName;
237
+ }
230
238
  get length() {
231
239
  return this.stacks.length;
232
240
  }
@@ -264,6 +272,20 @@ class Stacks {
264
272
  push(page) {
265
273
  return this.stacks.push(page);
266
274
  }
275
+ getTabs() {
276
+ return this.tabs;
277
+ }
278
+ pushTab(path) {
279
+ this.tabs[path] = this.last;
280
+ this.pop();
281
+ }
282
+ popTab(path) {
283
+ this.push(this.tabs[path]);
284
+ delete this.tabs[path];
285
+ }
286
+ removeTab(path) {
287
+ delete this.tabs[path];
288
+ }
267
289
  }
268
290
  const stacks = new Stacks();
269
291
 
@@ -294,6 +316,7 @@ function processNavigateUrl(option) {
294
316
  function navigate(option, method) {
295
317
  return __awaiter(this, void 0, void 0, function* () {
296
318
  return new Promise((resolve, reject) => {
319
+ stacks.method = method;
297
320
  const { success, complete, fail } = option;
298
321
  const unListen = exports.history.listen(() => {
299
322
  const res = { errMsg: `${method}:ok` };
@@ -572,6 +595,8 @@ class MultiPageHandler {
572
595
  }
573
596
  }
574
597
 
598
+ const createStampId$1 = runtime.incrementId();
599
+ const launchStampId$1 = createStampId$1();
575
600
  // TODO 支持多路由 (APP 生命周期仅触发一次)
576
601
  /** Note: 关于多页面应用
577
602
  * - 需要配置路由映射(根目录跳转、404 页面……)
@@ -587,7 +612,7 @@ function createMultiRouter(app, config, framework) {
587
612
  const handler = new MultiPageHandler(config);
588
613
  const launchParam = {
589
614
  path: config.pageName,
590
- query: handler.getQuery(),
615
+ query: handler.getQuery(launchStampId$1),
591
616
  scene: 0,
592
617
  shareTicket: '',
593
618
  referrerInfo: {}
@@ -710,9 +735,9 @@ class PageHandler {
710
735
  return [pagePath, homePage].includes(routePath) || ((_a = routesAlias.getConfig(pagePath)) === null || _a === void 0 ? void 0 : _a.includes(routePath));
711
736
  });
712
737
  }
713
- get isTabBar() {
738
+ isTabBar(pathname) {
714
739
  var _a;
715
- const routePath = addLeadingSlash(stripBasename(this.pathname, this.basename));
740
+ const routePath = addLeadingSlash(stripBasename(pathname, this.basename)).split('?')[0];
716
741
  const pagePath = ((_a = Object.entries(this.customRoutes).find(([, target]) => {
717
742
  if (typeof target === 'string') {
718
743
  return target === routePath;
@@ -748,12 +773,12 @@ class PageHandler {
748
773
  }
749
774
  return search.substr(1);
750
775
  }
751
- getQuery(stamp = 0, search = '', options = {}) {
776
+ getQuery(stamp = '', search = '', options = {}) {
752
777
  search = search ? `${search}&${this.search}` : this.search;
753
778
  const query = search
754
779
  ? queryString__default["default"].parse(search, { decode: false })
755
780
  : {};
756
- query.stamp = stamp.toString();
781
+ query.stamp = stamp;
757
782
  return Object.assign(Object.assign({}, query), options);
758
783
  }
759
784
  mount() {
@@ -796,18 +821,18 @@ class PageHandler {
796
821
  onLoad && (pageEl['__page'] = page);
797
822
  }
798
823
  }
799
- load(page, pageConfig = {}, stacksIndex = 0) {
824
+ load(page, pageConfig = {}, stampId, pageNo = 0) {
800
825
  var _a, _b;
801
826
  if (!page)
802
827
  return;
803
828
  // NOTE: 页面栈推入太晚可能导致 getCurrentPages 无法获取到当前页面实例
804
829
  stacks.push(page);
805
- const param = this.getQuery(stacks.length, '', page.options);
830
+ const param = this.getQuery(stampId, '', page.options);
806
831
  let pageEl = this.getPageContainer(page);
807
832
  if (pageEl) {
808
833
  setDisplay(pageEl);
809
- this.isTabBar && pageEl.classList.add('taro_tabbar_page');
810
- this.addAnimation(pageEl, stacksIndex === 0);
834
+ this.isTabBar(this.pathname) && pageEl.classList.add('taro_tabbar_page');
835
+ this.addAnimation(pageEl, pageNo === 0);
811
836
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
812
837
  this.bindPageEvents(page, pageEl, pageConfig);
813
838
  }
@@ -815,8 +840,8 @@ class PageHandler {
815
840
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
816
841
  var _a;
817
842
  pageEl = this.getPageContainer(page);
818
- this.isTabBar && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
819
- this.addAnimation(pageEl, stacksIndex === 0);
843
+ this.isTabBar(this.pathname) && (pageEl === null || pageEl === void 0 ? void 0 : pageEl.classList.add('taro_tabbar_page'));
844
+ this.addAnimation(pageEl, pageNo === 0);
820
845
  this.onReady(page, true);
821
846
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
822
847
  this.bindPageEvents(page, pageEl, pageConfig);
@@ -854,15 +879,15 @@ class PageHandler {
854
879
  if (delta >= 1)
855
880
  this.unload(stacks.last, delta);
856
881
  }
857
- show(page, pageConfig = {}, stacksIndex = 0) {
882
+ show(page, pageConfig = {}, pageNo = 0) {
858
883
  var _a, _b;
859
884
  if (!page)
860
885
  return;
861
- const param = this.getQuery(stacks.length, '', page.options);
886
+ const param = this.getQuery(page['$taroParams']['stamp'], '', page.options);
862
887
  let pageEl = this.getPageContainer(page);
863
888
  if (pageEl) {
864
889
  setDisplay(pageEl);
865
- this.addAnimation(pageEl, stacksIndex === 0);
890
+ this.addAnimation(pageEl, pageNo === 0);
866
891
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
867
892
  this.bindPageEvents(page, pageEl, pageConfig);
868
893
  }
@@ -870,7 +895,7 @@ class PageHandler {
870
895
  (_b = page.onLoad) === null || _b === void 0 ? void 0 : _b.call(page, param, () => {
871
896
  var _a;
872
897
  pageEl = this.getPageContainer(page);
873
- this.addAnimation(pageEl, stacksIndex === 0);
898
+ this.addAnimation(pageEl, pageNo === 0);
874
899
  this.onReady(page, false);
875
900
  (_a = page.onShow) === null || _a === void 0 ? void 0 : _a.call(page);
876
901
  this.bindPageEvents(page, pageEl, pageConfig);
@@ -940,6 +965,8 @@ class PageHandler {
940
965
  }
941
966
  }
942
967
 
968
+ const createStampId = runtime.incrementId();
969
+ let launchStampId = createStampId();
943
970
  function createRouter(app, config, framework) {
944
971
  var _a, _b;
945
972
  RouterConfig.config = config;
@@ -957,7 +984,7 @@ function createRouter(app, config, framework) {
957
984
  const router = new UniversalRouter__default["default"](routes, { baseUrl: basename || '' });
958
985
  const launchParam = {
959
986
  path: handler.homePage,
960
- query: handler.getQuery(stacks.length),
987
+ query: handler.getQuery(launchStampId),
961
988
  scene: 0,
962
989
  shareTicket: '',
963
990
  referrerInfo: {}
@@ -966,7 +993,7 @@ function createRouter(app, config, framework) {
966
993
  (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
967
994
  app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
968
995
  const render = ({ location, action }) => __awaiter(this, void 0, void 0, function* () {
969
- var _c, _d, _e, _f, _g;
996
+ var _c, _d, _e, _f, _g, _h;
970
997
  handler.pathname = decodeURI(location.pathname);
971
998
  runtime.eventCenter.trigger('__taroRouterChange', {
972
999
  toLocation: {
@@ -1005,8 +1032,45 @@ function createRouter(app, config, framework) {
1005
1032
  }
1006
1033
  const currentPage = runtime.Current.page;
1007
1034
  const pathname = handler.pathname;
1035
+ const methodName = (_g = stacks.method) !== null && _g !== void 0 ? _g : '';
1036
+ const cacheTabs = stacks.getTabs();
1008
1037
  let shouldLoad = false;
1009
- if (action === 'POP') {
1038
+ stacks.method = '';
1039
+ if (methodName === 'reLaunch') {
1040
+ handler.unload(currentPage, stacks.length);
1041
+ // NOTE: 同时卸载缓存在tabs里面的页面实例
1042
+ for (const key in cacheTabs) {
1043
+ if (cacheTabs[key]) {
1044
+ handler.unload(cacheTabs[key]);
1045
+ stacks.removeTab(key);
1046
+ }
1047
+ }
1048
+ shouldLoad = true;
1049
+ }
1050
+ else if (currentPage && handler.isTabBar(handler.pathname)) {
1051
+ if (handler.isSamePage(currentPage))
1052
+ return;
1053
+ if (handler.isTabBar(currentPage.path)) {
1054
+ handler.hide(currentPage);
1055
+ stacks.pushTab(currentPage.path.split('?')[0]);
1056
+ }
1057
+ else if (stacks.length > 0) {
1058
+ const firstIns = stacks.getItem(0);
1059
+ if (handler.isTabBar(firstIns.path)) {
1060
+ handler.unload(currentPage, stacks.length - 1);
1061
+ stacks.pushTab(firstIns.path.split('?')[0]);
1062
+ }
1063
+ else {
1064
+ handler.unload(currentPage, stacks.length);
1065
+ }
1066
+ }
1067
+ if (cacheTabs[handler.pathname]) {
1068
+ stacks.popTab(handler.pathname);
1069
+ return handler.show(stacks.getItem(0), pageConfig, 0);
1070
+ }
1071
+ shouldLoad = true;
1072
+ }
1073
+ else if (action === 'POP') {
1010
1074
  // NOTE: 浏览器事件退后多次时,该事件只会被触发一次
1011
1075
  const prevIndex = stacks.getPrevIndex(pathname);
1012
1076
  const delta = stacks.getDelta(pathname);
@@ -1021,37 +1085,34 @@ function createRouter(app, config, framework) {
1021
1085
  }
1022
1086
  }
1023
1087
  }
1024
- else {
1025
- if (handler.isTabBar) {
1026
- if (handler.isSamePage(currentPage))
1027
- return;
1028
- const prevIndex = stacks.getPrevIndex(pathname, 0);
1029
- handler.hide(currentPage);
1030
- if (prevIndex > -1) {
1031
- // NOTE: tabbar 页且之前出现过,直接复用
1032
- return handler.show(stacks.getItem(prevIndex), pageConfig, prevIndex);
1033
- }
1034
- }
1035
- else if (action === 'REPLACE') {
1036
- const delta = stacks.getDelta(pathname);
1037
- // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1038
- handler.unload(currentPage, delta);
1039
- }
1040
- else if (action === 'PUSH') {
1041
- handler.hide(currentPage);
1042
- }
1088
+ else if (action === 'REPLACE') {
1089
+ const delta = stacks.getDelta(pathname);
1090
+ // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1091
+ handler.unload(currentPage, delta);
1092
+ shouldLoad = true;
1093
+ }
1094
+ else if (action === 'PUSH') {
1095
+ handler.hide(currentPage);
1043
1096
  shouldLoad = true;
1044
1097
  }
1045
1098
  if (shouldLoad || stacks.length < 1) {
1046
- const el = (_g = element.default) !== null && _g !== void 0 ? _g : element;
1099
+ const el = (_h = element.default) !== null && _h !== void 0 ? _h : element;
1047
1100
  const loadConfig = Object.assign({}, pageConfig);
1048
1101
  const stacksIndex = stacks.length;
1049
1102
  delete loadConfig['path'];
1050
1103
  delete loadConfig['load'];
1051
- const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(stacksIndex)), {}, loadConfig);
1104
+ let pageStampId = '';
1105
+ if (launchStampId) {
1106
+ pageStampId = launchStampId;
1107
+ launchStampId = '';
1108
+ }
1109
+ else {
1110
+ pageStampId = createStampId();
1111
+ }
1112
+ const page = runtime.createPageConfig(enablePullDownRefresh ? runtime.hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathname + runtime.stringify(handler.getQuery(pageStampId)), {}, loadConfig);
1052
1113
  if (params)
1053
1114
  page.options = params;
1054
- return handler.load(page, pageConfig, stacksIndex);
1115
+ return handler.load(page, pageConfig, pageStampId, stacksIndex);
1055
1116
  }
1056
1117
  });
1057
1118
  const routePath = addLeadingSlash(stripBasename(exports.history.location.pathname, handler.basename));