This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 364
/
createAppConfig.ts
132 lines (108 loc) · 3.47 KB
/
createAppConfig.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import './polyfills/Function';
import * as React from 'react';
import { ForwardRef } from 'react-is';
import render from './render';
import AppContainer from './AppContainer';
import {
AppInstanceContext,
RuntimeOptions,
AppLifecycle,
callbackName,
isClassComponent,
} from '@remax/framework-shared';
class DefaultAppComponent extends React.Component {
render() {
return React.createElement(React.Fragment, null, this.props.children);
}
}
export default function createAppConfig(this: any, App: any) {
const WrappedApp = RuntimeOptions.get('pluginDriver').onAppComponent(App);
const createConfig = (AppComponent: React.ComponentType<any> = DefaultAppComponent) => {
const config: any = {
_container: new AppContainer(),
_pages: [] as any[],
_instance: React.createRef<any>(),
onLaunch(options: any) {
this._container.context = this;
this._render();
return this.callLifecycle(AppLifecycle.launch, options);
},
callLifecycle(lifecycle: AppLifecycle, ...args: any[]) {
const callbacks = AppInstanceContext.lifecycleCallback[lifecycle] || [];
let result;
callbacks.forEach((callback: any) => {
result = callback(...args);
});
if (result) {
return result;
}
const callback = callbackName(lifecycle);
if (this._instance.current && this._instance.current[callback]) {
return this._instance.current[callback](...args);
}
},
_mount(pageInstance: any) {
/**
* 飞书开发者工具的问题,这里的 this 跟 getApp 拿到的不是同一个实例
*/
if (!this._container.context) {
this._container.context = this;
}
this._pages.push(pageInstance);
this._render();
},
_unmount(pageInstance: any) {
this._pages.splice(this._pages.indexOf(pageInstance), 1);
this._render();
},
_render() {
const props: any = {};
if (isClassComponent(AppComponent) || (AppComponent as any).$$typeof === ForwardRef) {
props.ref = this._instance;
}
return render(
React.createElement(
AppComponent,
props,
this._pages.map((p: any) => p.element)
),
this._container
);
},
onShow(options: any) {
return this.callLifecycle(AppLifecycle.show, options);
},
onHide() {
return this.callLifecycle(AppLifecycle.hide);
},
onError(error: any) {
return this.callLifecycle(AppLifecycle.error, error);
},
// 微信
onPageNotFound(options: any) {
return this.callLifecycle(AppLifecycle.pageNotFound, options);
},
// 微信
onUnhandledRejection(options: any) {
return this.callLifecycle(AppLifecycle.unhandledRejection, options);
},
// 微信
onThemeChange(options: any) {
return this.callLifecycle(AppLifecycle.themeChange, options);
},
};
const lifecycleEvent: any = {
// 阿里
onShareAppMessage(options: any) {
return this.callLifecycle(AppLifecycle.shareAppMessage, options);
},
};
(RuntimeOptions.get('appEvents') || []).forEach(eventName => {
if (lifecycleEvent[eventName]) {
config[eventName] = lifecycleEvent[eventName];
}
});
return RuntimeOptions.get('pluginDriver').onAppConfig(config);
};
return createConfig(WrappedApp);
}