1 | import { __awaiter } from 'tslib';
|
2 | import { incrementId, eventCenter, createPageConfig, hooks, stringify, Current } from '@tarojs/runtime';
|
3 | import '../utils/index.js';
|
4 | import { RouterConfig } from './index.js';
|
5 | import MultiPageHandler from './multi-page.js';
|
6 | import { setMpaTitle } from '../utils/navigate.js';
|
7 |
|
8 | const createStampId = incrementId();
|
9 | const launchStampId = createStampId();
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function 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 |
|
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 |
|
87 | export { createMultiRouter };
|