|
1 |
| -import { invariant, InstantiationService } from '@alilc/lowcode-shared'; |
2 |
| -import type { AppOptions, RendererApplication } from './types'; |
3 |
| -import { CodeRuntimeService, ICodeRuntimeService } from './services/code-runtime'; |
| 1 | +import { |
| 2 | + invariant, |
| 3 | + InstantiationService, |
| 4 | + BeanContainer, |
| 5 | + CtorDescriptor, |
| 6 | + type Project, |
| 7 | + type Package, |
| 8 | +} from '@alilc/lowcode-shared'; |
| 9 | +import { CodeRuntimeService, ICodeRuntimeService, type CodeRuntimeOptions } from './code-runtime'; |
4 | 10 | import {
|
5 | 11 | IExtensionHostService,
|
6 | 12 | type RenderAdapter,
|
7 | 13 | type IRenderObject,
|
8 | 14 | ExtensionHostService,
|
9 |
| -} from './services/extension'; |
10 |
| -import { IPackageManagementService, PackageManagementService } from './services/package'; |
11 |
| -import { ISchemaService, SchemaService } from './services/schema'; |
12 |
| -import { ILifeCycleService, LifecyclePhase, LifeCycleService } from './services/lifeCycleService'; |
13 |
| -import { IRuntimeIntlService, RuntimeIntlService } from './services/runtimeIntlService'; |
14 |
| -import { IRuntimeUtilService, RuntimeUtilService } from './services/runtimeUtilService'; |
| 15 | + type Plugin, |
| 16 | +} from './extension'; |
| 17 | +import { IPackageManagementService, PackageManagementService } from './package'; |
| 18 | +import { ISchemaService, SchemaService } from './schema'; |
| 19 | +import { ILifeCycleService, LifecyclePhase, LifeCycleService } from './life-cycle'; |
| 20 | +import { IRuntimeIntlService, RuntimeIntlService } from './intl'; |
| 21 | +import { IRuntimeUtilService, RuntimeUtilService } from './util'; |
| 22 | +import { type ModelDataSourceCreator } from './model'; |
| 23 | + |
| 24 | +export interface AppOptions { |
| 25 | + schema: Project; |
| 26 | + packages?: Package[]; |
| 27 | + plugins?: Plugin[]; |
| 28 | + /** |
| 29 | + * code runtime 设置选项 |
| 30 | + */ |
| 31 | + codeRuntime?: CodeRuntimeOptions; |
| 32 | + /** |
| 33 | + * 数据源创建工厂函数 |
| 34 | + */ |
| 35 | + dataSourceCreator?: ModelDataSourceCreator; |
| 36 | +} |
| 37 | + |
| 38 | +export type RendererApplication<Render = unknown> = { |
| 39 | + readonly mode: 'development' | 'production'; |
| 40 | + |
| 41 | + readonly schema: Omit<ISchemaService, 'initialize'>; |
| 42 | + |
| 43 | + readonly packageManager: IPackageManagementService; |
| 44 | + |
| 45 | + use(plugin: Plugin): Promise<void>; |
| 46 | + |
| 47 | + destroy(): void; |
| 48 | +} & Render; |
15 | 49 |
|
16 | 50 | export function createRenderer<RenderObject = IRenderObject>(
|
17 | 51 | renderAdapter: RenderAdapter<RenderObject>,
|
18 | 52 | ): (options: AppOptions) => Promise<RendererApplication<RenderObject>> {
|
19 | 53 | invariant(typeof renderAdapter === 'function', 'The first parameter must be a function.');
|
20 | 54 |
|
21 |
| - const instantiationService = new InstantiationService(); |
22 |
| - |
23 |
| - // create services |
24 |
| - const lifeCycleService = new LifeCycleService(); |
25 |
| - instantiationService.container.set(ILifeCycleService, lifeCycleService); |
26 |
| - |
27 | 55 | return async (options) => {
|
| 56 | + // create services |
| 57 | + const container = new BeanContainer(); |
| 58 | + const lifeCycleService = new LifeCycleService(); |
| 59 | + container.set(ILifeCycleService, lifeCycleService); |
| 60 | + |
28 | 61 | const schemaService = new SchemaService(options.schema);
|
29 |
| - instantiationService.container.set(ISchemaService, schemaService); |
| 62 | + container.set(ISchemaService, schemaService); |
30 | 63 |
|
31 |
| - const codeRuntimeService = instantiationService.createInstance( |
32 |
| - CodeRuntimeService, |
33 |
| - options.codeRuntime, |
| 64 | + container.set( |
| 65 | + ICodeRuntimeService, |
| 66 | + new CtorDescriptor(CodeRuntimeService, [options.codeRuntime]), |
34 | 67 | );
|
35 |
| - instantiationService.container.set(ICodeRuntimeService, codeRuntimeService); |
36 |
| - |
37 |
| - const packageManagementService = instantiationService.createInstance(PackageManagementService); |
38 |
| - instantiationService.container.set(IPackageManagementService, packageManagementService); |
| 68 | + container.set(IPackageManagementService, new CtorDescriptor(PackageManagementService)); |
39 | 69 |
|
40 | 70 | const utils = schemaService.get('utils');
|
41 |
| - const runtimeUtilService = instantiationService.createInstance(RuntimeUtilService, utils); |
42 |
| - instantiationService.container.set(IRuntimeUtilService, runtimeUtilService); |
| 71 | + container.set(IRuntimeUtilService, new CtorDescriptor(RuntimeUtilService, [utils])); |
43 | 72 |
|
44 | 73 | const defaultLocale = schemaService.get('config.defaultLocale');
|
45 | 74 | const i18ns = schemaService.get('i18n', {});
|
46 |
| - const runtimeIntlService = instantiationService.createInstance( |
47 |
| - RuntimeIntlService, |
48 |
| - defaultLocale, |
49 |
| - i18ns, |
50 |
| - ); |
51 |
| - instantiationService.container.set(IRuntimeIntlService, runtimeIntlService); |
52 |
| - |
53 |
| - const extensionHostService = new ExtensionHostService( |
54 |
| - lifeCycleService, |
55 |
| - packageManagementService, |
56 |
| - schemaService, |
57 |
| - codeRuntimeService, |
58 |
| - runtimeIntlService, |
59 |
| - runtimeUtilService, |
| 75 | + container.set( |
| 76 | + IRuntimeIntlService, |
| 77 | + new CtorDescriptor(RuntimeIntlService, [defaultLocale, i18ns]), |
60 | 78 | );
|
61 |
| - instantiationService.container.set(IExtensionHostService, extensionHostService); |
| 79 | + |
| 80 | + container.set(IExtensionHostService, new CtorDescriptor(ExtensionHostService)); |
| 81 | + |
| 82 | + const instantiationService = new InstantiationService(container); |
62 | 83 |
|
63 | 84 | lifeCycleService.setPhase(LifecyclePhase.OptionsResolved);
|
64 | 85 |
|
| 86 | + const [extensionHostService, packageManagementService] = instantiationService.invokeFunction( |
| 87 | + (accessor) => { |
| 88 | + return [accessor.get(IExtensionHostService), accessor.get(IPackageManagementService)]; |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
65 | 92 | const renderObject = await renderAdapter(instantiationService);
|
66 | 93 |
|
67 | 94 | await extensionHostService.registerPlugin(options.plugins ?? []);
|
68 |
| - // 先加载插件提供 package loader |
| 95 | + |
69 | 96 | await packageManagementService.loadPackages(options.packages ?? []);
|
70 | 97 |
|
71 | 98 | lifeCycleService.setPhase(LifecyclePhase.Ready);
|
|
0 commit comments