UNPKG

4.21 kBJavaScriptView Raw
1import { __awaiter } from 'tslib';
2import { incrementId, eventCenter, createPageConfig, hooks, stringify, Current } from '@tarojs/runtime';
3import '../utils/index.js';
4import { RouterConfig } from './index.js';
5import MultiPageHandler from './multi-page.js';
6import { setMpaTitle } from '../utils/navigate.js';
7
8const createStampId = incrementId();
9const launchStampId = createStampId();
10// TODO 支持多路由 (APP 生命周期仅触发一次)
11/** Note: 关于多页面应用
12 * - 需要配置路由映射(根目录跳转、404 页面……)
13 * - app.onPageNotFound 事件不支持
14 * - 应用生命周期可能多次触发
15 * - TabBar 会多次加载
16 * - 不支持路由动画
17 */
18function createMultiRouter(history, app, config, framework) {
19 return __awaiter(this, void 0, void 0, function* () {
20 var _a, _b, _c, _d, _e, _f;
21 if (typeof app.onUnhandledRejection === 'function') {
22 window.addEventListener('unhandledrejection', app.onUnhandledRejection);
23 }
24 eventCenter.on('__taroH5SetNavigationBarTitle', setMpaTitle);
25 RouterConfig.config = config;
26 const handler = new MultiPageHandler(config, history);
27 const launchParam = {
28 path: config.pageName, // 多页面模式没新开一个页面相当于重启,所以直接使用当前页面路径
29 query: handler.getQuery(launchStampId),
30 scene: 0,
31 shareTicket: '',
32 referrerInfo: {}
33 };
34 eventCenter.trigger('__taroRouterLaunch', launchParam);
35 (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
36 app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
37 const pathName = config.pageName;
38 const pageConfig = handler.pageConfig;
39 eventCenter.trigger('__taroRouterChange', {
40 toLocation: {
41 path: pathName
42 }
43 });
44 let element;
45 try {
46 element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
47 if (element instanceof Array) {
48 element = element[0];
49 }
50 }
51 catch (error) {
52 throw new Error(error);
53 }
54 if (!element)
55 return;
56 let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
57 if (pageConfig) {
58 setMpaTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
59 if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
60 enablePullDownRefresh = pageConfig.enablePullDownRefresh;
61 }
62 }
63 const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
64 const loadConfig = Object.assign({}, pageConfig);
65 delete loadConfig['path'];
66 delete loadConfig['load'];
67 const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, pathName, framework, handler.PullDownRefresh) : el, pathName + stringify(launchParam), {}, loadConfig);
68 handler.load(page, pageConfig);
69 (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
70 window.addEventListener('visibilitychange', () => {
71 var _a, _b, _c;
72 const currentPath = ((_a = Current.page) === null || _a === void 0 ? void 0 : _a.path) || '';
73 const path = currentPath.substring(0, currentPath.indexOf('?'));
74 const param = {};
75 // app的 onShow/onHide 生命周期的路径信息为当前页面的路径
76 Object.assign(param, launchParam, { path });
77 if (document.visibilityState === 'visible') {
78 (_b = app.onShow) === null || _b === void 0 ? void 0 : _b.call(app, param);
79 }
80 else {
81 (_c = app.onHide) === null || _c === void 0 ? void 0 : _c.call(app, param);
82 }
83 });
84 });
85}
86
87export { createMultiRouter };