UNPKG

3.88 kBJavaScriptView Raw
1var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10/* eslint-disable dot-notation */
11import { createPageConfig, eventCenter, hooks, incrementId, stringify, } from '@tarojs/runtime';
12import { setTitle } from '../utils/navigate';
13import { RouterConfig } from '.';
14import MultiPageHandler from './multi-page';
15const createStampId = incrementId();
16const launchStampId = createStampId();
17// TODO 支持多路由 (APP 生命周期仅触发一次)
18/** Note: 关于多页面应用
19 * - 需要配置路由映射(根目录跳转、404 页面……)
20 * - app.onPageNotFound 事件不支持
21 * - 应用生命周期可能多次触发
22 * - TabBar 会多次加载
23 * - 不支持路由动画
24 */
25export function createMultiRouter(app, config, framework) {
26 var _a, _b, _c, _d, _e, _f;
27 return __awaiter(this, void 0, void 0, function* () {
28 if (typeof app.onUnhandledRejection === 'function') {
29 window.addEventListener('unhandledrejection', app.onUnhandledRejection);
30 }
31 RouterConfig.config = config;
32 const handler = new MultiPageHandler(config);
33 const launchParam = {
34 path: config.pageName,
35 query: handler.getQuery(launchStampId),
36 scene: 0,
37 shareTicket: '',
38 referrerInfo: {}
39 };
40 eventCenter.trigger('__taroRouterLaunch', launchParam);
41 (_a = app.onLaunch) === null || _a === void 0 ? void 0 : _a.call(app, launchParam);
42 app.onError && window.addEventListener('error', e => { var _a; return (_a = app.onError) === null || _a === void 0 ? void 0 : _a.call(app, e.message); });
43 const pathName = config.pageName;
44 const pageConfig = handler.pageConfig;
45 eventCenter.trigger('__taroRouterChange', {
46 toLocation: {
47 path: pathName
48 }
49 });
50 let element;
51 try {
52 element = yield ((_b = pageConfig.load) === null || _b === void 0 ? void 0 : _b.call(pageConfig));
53 if (element instanceof Array) {
54 element = element[0];
55 }
56 }
57 catch (error) {
58 throw new Error(error);
59 }
60 if (!element)
61 return;
62 let enablePullDownRefresh = ((_c = config === null || config === void 0 ? void 0 : config.window) === null || _c === void 0 ? void 0 : _c.enablePullDownRefresh) || false;
63 if (pageConfig) {
64 setTitle((_d = pageConfig.navigationBarTitleText) !== null && _d !== void 0 ? _d : document.title);
65 if (typeof pageConfig.enablePullDownRefresh === 'boolean') {
66 enablePullDownRefresh = pageConfig.enablePullDownRefresh;
67 }
68 }
69 const el = (_e = element.default) !== null && _e !== void 0 ? _e : element;
70 const loadConfig = Object.assign({}, pageConfig);
71 delete loadConfig['path'];
72 delete loadConfig['load'];
73 const page = createPageConfig(enablePullDownRefresh ? hooks.call('createPullDownComponent', el, location.pathname, framework, handler.PullDownRefresh) : el, pathName + stringify(launchParam), {}, loadConfig);
74 handler.load(page, pageConfig);
75 (_f = app.onShow) === null || _f === void 0 ? void 0 : _f.call(app, launchParam);
76 });
77}