-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
46 lines (37 loc) · 992 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { createApp } from './app.js';
export function createLayer() {
const layer = {
apps: [],
registerMicroApps,
start
}
function registerMicroApps(options) {
options.forEach(opt => {
layer.apps.push(createApp(opt));
})
layer.start();
}
async function start() {
window.__POWERED_BY_MICRO_TOY__ = true;
const unMountPromise = [];
const mountPromise = [];
layer.apps.forEach(app => {
const { isActive } = app;
if (isActive() && !app.isMount) {
mountPromise.push(() => app.mountApp());
}
if (!isActive() && app.isMount) {
unMountPromise.push(() => app.unMountApp());
}
})
await Promise.all(unMountPromise.map(fn => fn()));
Promise.all(mountPromise.map(fn => fn()));
}
// 路由拦截
const rawPushState = window.history.pushState;
window.history.pushState = function (...rest) {
rawPushState.apply(this, rest);
layer.start();
};
return layer;
}