Skip to content

Commit 7a3c673

Browse files
author
guqiankun.gqk
committed
chore: 增加多实例配置
1 parent 05be148 commit 7a3c673

13 files changed

Lines changed: 303 additions & 193 deletions

File tree

packages/alex/src/api/createApp.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ export function createApp({ appConfig, runtimeConfig }: IConfig): IAppInstance {
9191
opts.workspaceDir = makeWorkspaceDir(opts.workspaceDir);
9292
// 托管 preference 逻辑
9393
registerLocalStorageProvider(opts);
94+
95+
console.log(' ==> opts',opts.injector);
96+
9497
const app = new ClientApp(opts) as IAppInstance;
9598

9699
Object.defineProperty(app, 'currentThemeType', {

packages/alex/src/api/createMutapleApp.tsx

Whitespace-only changes.

packages/alex/src/api/renderApp.tsx

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { REPORT_NAME, RuntimeConfig } from '@alipay/alex-core';
55
import { createApp } from './createApp';
66
import { Root } from '../core/Root';
77
import { RootProps, LandingProps } from '../core/types';
8-
import { useConstant } from '../core/hooks';
8+
import { setSingleInjector, singleInjector, useConstant } from '../core/hooks';
99
import { IConfig, IAppInstance } from './types';
1010
import styles from '../core/style.module.less';
11+
import { setInjector } from '@opensumi/di/dist/helper';
12+
import { Injector } from '@opensumi/di';
1113

1214
export interface IAppRendererProps extends IConfig {
1315
onLoad?(app: IAppInstance): void;
@@ -119,3 +121,69 @@ export const AppRenderer: React.FC<IAppRendererProps> = ({ onLoad, Landing, ...o
119121
</Root>
120122
);
121123
};
124+
125+
// 缓存apprender 每次渲染都不卸载组件
126+
export const AppRenderer2: React.FC<IAppRendererProps> = ({ onLoad, Landing, ...opts }) => {
127+
128+
const app = useConstant(() => createApp({
129+
...opts,
130+
// @ts-ignore
131+
injector: singleInjector
132+
}));
133+
const themeType = useConstant(() => app.currentThemeType);
134+
const appElementRef = useRef<React.ReactElement | null>(null);
135+
setSingleInjector(app.injector)
136+
137+
console.log('app', app);
138+
// 确保回调始终为最新
139+
// TODO: 用 PropsService
140+
const runtimeConfig: RuntimeConfig = app.injector.get(RuntimeConfig);
141+
runtimeConfig.workspace = opts.runtimeConfig.workspace;
142+
143+
const [state, setState] = useState<{
144+
status: RootProps['status'];
145+
error?: RootProps['error'];
146+
}>(() => ({ status: 'loading' }));
147+
148+
useEffect(() => {
149+
app
150+
.start((appElement) => {
151+
appElementRef.current = appElement;
152+
setState({ status: 'success' });
153+
return Promise.resolve();
154+
})
155+
.then(() => {
156+
onLoad?.(app);
157+
})
158+
.catch((err: Error) => {
159+
setState({ error: err?.message || localize('error.unknown'), status: 'error' });
160+
161+
(app.injector.get(IReporterService) as IReporterService).point(
162+
REPORT_NAME.ALEX_APP_START_ERROR,
163+
err?.message,
164+
{
165+
error: err,
166+
}
167+
);
168+
getDebugLogger().error(err);
169+
setTimeout(() => {
170+
throw err;
171+
});
172+
});
173+
174+
return () => {
175+
// app.destroy();
176+
};
177+
}, []);
178+
179+
const rootClassName = useMemo(
180+
() => (opts.runtimeConfig.hideEditorTab ? styles['hide-editor-tab'] : ''),
181+
[opts.runtimeConfig.hideEditorTab]
182+
);
183+
184+
return (
185+
<Root {...state} theme={themeType} Landing={Landing} className={rootClassName}>
186+
{appElementRef.current}
187+
</Root>
188+
);
189+
};

packages/alex/src/core/alex.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class AlexContribution implements KeybindingContribution {
2121
keybindings.unregisterKeybinding(binding);
2222
});
2323
}
24+
25+
if(this.runtimeConfig.registerKeybindings?.length){
26+
keybindings.registerKeybindings(this.runtimeConfig.registerKeybindings)
27+
}
2428
}
2529
}
2630

packages/alex/src/core/hooks.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { Injector } from '@opensumi/di';
12
import { useRef } from 'react';
3+
import { IAppInstance } from '../api/types';
24

35
export const useConstant = <T>(fn: () => T): T => {
46
const valueRef = useRef<{ v: T }>();
@@ -7,3 +9,16 @@ export const useConstant = <T>(fn: () => T): T => {
79
}
810
return valueRef.current.v;
911
};
12+
13+
14+
export let singleInjector: Injector | null = null;
15+
16+
export function setSingleInjector(inject) {
17+
singleInjector = inject;
18+
}
19+
20+
export let singleApp: IAppInstance| null = null
21+
22+
export function setSingleApp(app: IAppInstance){
23+
singleApp = app;
24+
}

packages/core/src/client/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ export { IClientAppOpts };
140140
// 先 dispose,待 opensumi 修复
141141
// @ts-ignore
142142
export class ClientApp extends BasicClientApp {
143-
private clearInjector: () => void;
144-
145-
private disposeSideEffect = () => {};
146143

147144
private modules: ModuleConstructor[] = [];
148145

packages/core/src/common/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Keybinding } from '@opensumi/ide-core-browser';
12
import { IReporter } from '@opensumi/ide-core-common';
23
import { FileSystemConfiguration, FileSystemInstance } from '../server/node';
34

@@ -103,6 +104,13 @@ export interface RuntimeConfig {
103104
* 配置需注销的快捷键
104105
*/
105106
unregisterKeybindings?: string[];
107+
/**
108+
* 配置快捷键
109+
* 可以通过 plugin 注册对应 command 执行的命令
110+
* opensumi keybinding
111+
* https://opensumi.com/zh/docs/integrate/universal-integrate-case/custom-keybinding
112+
*/
113+
registerKeybindings?: Keybinding[];
106114
/**
107115
* 文本搜索相关的配置,用于开启了左侧搜索面板的配置选项
108116
*/

packages/core/src/server/core/fs-launch.contribution.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IServerApp, RuntimeConfig, RootFS, AppConfig } from '../../common/types
55
import { LaunchContribution } from './app';
66
import { FileSystemContribution } from './base';
77
import { BrowserFS } from '../node';
8+
import { setSingleInjector, singleInjector } from '@alipay/alex/lib/core/hooks';
89

910
@Domain(LaunchContribution)
1011
export class FileSystemLaunchContribution implements LaunchContribution {

0 commit comments

Comments
 (0)