Skip to content

Commit ddc2473

Browse files
committed
refactor: adjust dicts
1 parent d4bdf14 commit ddc2473

32 files changed

+118
-123
lines changed

packages/react-renderer/src/runtime/createComponent.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ export function createComponent(
116116

117117
useEffect(() => {
118118
// trigger lifeCycles
119-
// componentDidMount?.();
120119
model.triggerLifeCycle('componentDidMount');
121120

122121
// 当 state 改变之后调用

packages/react-renderer/src/runtime/elements.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ export function WidgetComponent(props: WidgetRendererProps) {
142142
},
143143
);
144144

145-
if (process.env.NODE_ENV === 'development') {
146-
// development 模式下 把 widget 的内容作为 prop ,便于排查问题
145+
if (__DEV__) {
147146
processedProps.widget = widget;
148147
}
149148

packages/renderer-core/src/services/code-runtime/codeRuntime.ts renamed to packages/renderer-core/src/code-runtime/codeRuntime.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
5353
super();
5454

5555
if (options.evalCodeFunction) this._evalCodeFunction = options.evalCodeFunction;
56-
this._codeScope = this.addDispose(
56+
this._codeScope = this._addDispose(
5757
options.parentScope
5858
? options.parentScope.createChild<T>(options.initScopeValue ?? {})
5959
: new CodeScope(options.initScopeValue ?? {}),
@@ -122,7 +122,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
122122
onResolve(handler: NodeResolverHandler): IDisposable {
123123
this._resolveHandlers.push(handler);
124124

125-
return this.addDispose(
125+
return this._addDispose(
126126
toDisposable(() => {
127127
this._resolveHandlers = this._resolveHandlers.filter((h) => h !== handler);
128128
}),
@@ -132,7 +132,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
132132
createChild<V extends StringDictionary = StringDictionary>(
133133
options?: Omit<CodeRuntimeOptions<V>, 'parentScope'>,
134134
): ICodeRuntime<V> {
135-
return this.addDispose(
135+
return this._addDispose(
136136
new CodeRuntime({
137137
initScopeValue: options?.initScopeValue,
138138
parentScope: this._codeScope,

packages/renderer-core/src/services/code-runtime/codeRuntimeService.ts renamed to packages/renderer-core/src/code-runtime/codeRuntimeService.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export class CodeRuntimeService extends Disposable implements ICodeRuntimeServic
2929
) {
3030
super();
3131

32-
this._rootRuntime = this.addDispose(new CodeRuntime(options));
33-
this.addDispose(
32+
this._rootRuntime = this._addDispose(new CodeRuntime(options));
33+
this._addDispose(
3434
this.schemaService.onSchemaUpdate(({ key, data }) => {
3535
if (key === 'constants') {
3636
this.rootRuntime.getScope().set('constants', data);
@@ -44,7 +44,7 @@ export class CodeRuntimeService extends Disposable implements ICodeRuntimeServic
4444
): ICodeRuntime<T> {
4545
this._throwIfDisposed();
4646

47-
return this.addDispose(
47+
return this._addDispose(
4848
options.parentScope ? new CodeRuntime(options) : this.rootRuntime.createChild<T>(options),
4949
);
5050
}

packages/renderer-core/src/services/code-runtime/codeScope.ts renamed to packages/renderer-core/src/code-runtime/codeScope.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class CodeScope<T extends StringDictionary = StringDictionary>
6666
}
6767

6868
createChild<V extends StringDictionary = StringDictionary>(initValue: Partial<V>): ICodeScope<V> {
69-
const childScope = this.addDispose(new CodeScope(initValue));
69+
const childScope = this._addDispose(new CodeScope(initValue));
7070
childScope.node.prev = this.node;
7171

7272
return childScope;

packages/renderer-core/src/createRenderer.ts

+68-41
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,98 @@
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';
410
import {
511
IExtensionHostService,
612
type RenderAdapter,
713
type IRenderObject,
814
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;
1549

1650
export function createRenderer<RenderObject = IRenderObject>(
1751
renderAdapter: RenderAdapter<RenderObject>,
1852
): (options: AppOptions) => Promise<RendererApplication<RenderObject>> {
1953
invariant(typeof renderAdapter === 'function', 'The first parameter must be a function.');
2054

21-
const instantiationService = new InstantiationService();
22-
23-
// create services
24-
const lifeCycleService = new LifeCycleService();
25-
instantiationService.container.set(ILifeCycleService, lifeCycleService);
26-
2755
return async (options) => {
56+
// create services
57+
const container = new BeanContainer();
58+
const lifeCycleService = new LifeCycleService();
59+
container.set(ILifeCycleService, lifeCycleService);
60+
2861
const schemaService = new SchemaService(options.schema);
29-
instantiationService.container.set(ISchemaService, schemaService);
62+
container.set(ISchemaService, schemaService);
3063

31-
const codeRuntimeService = instantiationService.createInstance(
32-
CodeRuntimeService,
33-
options.codeRuntime,
64+
container.set(
65+
ICodeRuntimeService,
66+
new CtorDescriptor(CodeRuntimeService, [options.codeRuntime]),
3467
);
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));
3969

4070
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]));
4372

4473
const defaultLocale = schemaService.get('config.defaultLocale');
4574
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]),
6078
);
61-
instantiationService.container.set(IExtensionHostService, extensionHostService);
79+
80+
container.set(IExtensionHostService, new CtorDescriptor(ExtensionHostService));
81+
82+
const instantiationService = new InstantiationService(container);
6283

6384
lifeCycleService.setPhase(LifecyclePhase.OptionsResolved);
6485

86+
const [extensionHostService, packageManagementService] = instantiationService.invokeFunction(
87+
(accessor) => {
88+
return [accessor.get(IExtensionHostService), accessor.get(IPackageManagementService)];
89+
},
90+
);
91+
6592
const renderObject = await renderAdapter(instantiationService);
6693

6794
await extensionHostService.registerPlugin(options.plugins ?? []);
68-
// 先加载插件提供 package loader
95+
6996
await packageManagementService.loadPackages(options.packages ?? []);
7097

7198
lifeCycleService.setPhase(LifecyclePhase.Ready);

packages/renderer-core/src/services/extension/boosts.ts renamed to packages/renderer-core/src/extension/boosts.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { type StringDictionary } from '@alilc/lowcode-shared';
22
import { isObject } from 'lodash-es';
33
import { ICodeRuntime, ICodeRuntimeService } from '../code-runtime';
4-
import { IRuntimeUtilService } from '../runtimeUtilService';
5-
import { IRuntimeIntlService } from '../runtimeIntlService';
4+
import { IRuntimeUtilService } from '../util/utilService';
5+
import { IRuntimeIntlService } from '../intlService';
66

77
export type IBoosts<Extends> = IBoostsApi & Extends & { [key: string]: any };
88

packages/renderer-core/src/services/extension/extensionHostService.ts renamed to packages/renderer-core/src/extension/extensionHostService.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { type Plugin, type PluginContext } from './plugin';
33
import { BoostsManager } from './boosts';
44
import { IPackageManagementService } from '../package';
55
import { ISchemaService } from '../schema';
6-
import { ILifeCycleService } from '../lifeCycleService';
6+
import { ILifeCycleService } from '../life-cycle/lifeCycleService';
77
import { ICodeRuntimeService } from '../code-runtime';
8-
import { IRuntimeIntlService } from '../runtimeIntlService';
9-
import { IRuntimeUtilService } from '../runtimeUtilService';
8+
import { IRuntimeIntlService } from '../intl';
9+
import { IRuntimeUtilService } from '../util';
1010

1111
export interface IExtensionHostService {
1212
readonly boostsManager: BoostsManager;
@@ -28,12 +28,12 @@ export class ExtensionHostService extends Disposable implements IExtensionHostSe
2828
private _pluginSetupContext: PluginContext;
2929

3030
constructor(
31-
lifeCycleService: ILifeCycleService,
32-
packageManagementService: IPackageManagementService,
33-
schemaService: ISchemaService,
34-
codeRuntimeService: ICodeRuntimeService,
35-
runtimeIntlService: IRuntimeIntlService,
36-
runtimeUtilService: IRuntimeUtilService,
31+
@ILifeCycleService lifeCycleService: ILifeCycleService,
32+
@IPackageManagementService packageManagementService: IPackageManagementService,
33+
@ISchemaService schemaService: ISchemaService,
34+
@ICodeRuntimeService codeRuntimeService: ICodeRuntimeService,
35+
@IRuntimeIntlService runtimeIntlService: IRuntimeIntlService,
36+
@IRuntimeUtilService runtimeUtilService: IRuntimeUtilService,
3737
) {
3838
super();
3939

@@ -103,7 +103,7 @@ export class ExtensionHostService extends Disposable implements IExtensionHostSe
103103

104104
await plugin.setup(this._pluginSetupContext);
105105
this._activePlugins.add(plugin.name);
106-
this.addDispose(plugin);
106+
this._addDispose(plugin);
107107
}
108108

109109
getPlugin(name: string): Plugin | undefined {

packages/renderer-core/src/services/extension/plugin.ts renamed to packages/renderer-core/src/extension/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type StringDictionary, type IDisposable } from '@alilc/lowcode-shared';
22
import { type IBoosts } from './boosts';
3-
import { ILifeCycleService } from '../lifeCycleService';
3+
import { ILifeCycleService } from '../life-cycle/lifeCycleService';
44
import { type ISchemaService } from '../schema';
55
import { type IPackageManagementService } from '../package';
66
import { type IStore } from '../../utils/store';

packages/renderer-core/src/index.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
/* --------------- api -------------------- */
2-
export { createRenderer } from './createRenderer';
3-
export { IExtensionHostService } from './services/extension';
4-
export { definePackageLoader, IPackageManagementService } from './services/package';
5-
export { LifecyclePhase, ILifeCycleService } from './services/lifeCycleService';
6-
export { IComponentTreeModelService } from './services/model';
7-
export { ICodeRuntimeService } from './services/code-runtime';
8-
export { IRuntimeIntlService } from './services/runtimeIntlService';
9-
export { IRuntimeUtilService } from './services/runtimeUtilService';
10-
export { ISchemaService } from './services/schema';
2+
export * from './createRenderer';
3+
export { IExtensionHostService } from './extension';
4+
export { definePackageLoader, IPackageManagementService } from './package';
5+
export { LifecyclePhase, ILifeCycleService } from './life-cycle';
6+
export { IComponentTreeModelService } from './model';
7+
export { ICodeRuntimeService, mapValue, someValue } from './code-runtime';
8+
export { IRuntimeIntlService } from './intl';
9+
export { IRuntimeUtilService } from './util';
10+
export { ISchemaService } from './schema';
1111
export { Widget } from './widget';
1212

1313
/* --------------- types ---------------- */
14-
export type * from './types';
15-
export type * from './services/extension';
16-
export type * from './services/code-runtime';
17-
export type * from './services/model';
18-
export type * from './services/package';
19-
export type * from './services/schema';
20-
export type * from './services/extension';
14+
export type * from './extension';
15+
export type * from './code-runtime';
16+
export type * from './model';
17+
export type * from './package';
18+
export type * from './schema';
19+
export type * from './extension';
2120
export type * from './widget';
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './intlService';

packages/renderer-core/src/services/runtimeIntlService.ts renamed to packages/renderer-core/src/intl/intlService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
type LocaleTranslationsMap,
88
Disposable,
99
} from '@alilc/lowcode-shared';
10-
import { ICodeRuntimeService } from './code-runtime';
10+
import { ICodeRuntimeService } from '../code-runtime';
1111

1212
export interface MessageDescriptor {
1313
key: string;
@@ -37,7 +37,7 @@ export class RuntimeIntlService extends Disposable implements IRuntimeIntlServic
3737
) {
3838
super();
3939

40-
this._intl = this.addDispose(new Intl(defaultLocale));
40+
this._intl = this._addDispose(new Intl(defaultLocale));
4141
for (const key of Object.keys(i18nTranslations)) {
4242
this._intl.addTranslations(key, i18nTranslations[key]);
4343
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lifeCycleService';

packages/renderer-core/src/services/model/componentTreeModel.ts renamed to packages/renderer-core/src/model/componentTreeModel.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
Disposable,
1616
} from '@alilc/lowcode-shared';
1717
import { type ICodeRuntime } from '../code-runtime';
18-
import { IWidget, Widget } from '../../widget';
18+
import { IWidget, Widget } from '../widget';
1919

2020
export interface NormalizedComponentNode extends ComponentNode {
2121
loopArgs: [string, string];
@@ -25,7 +25,7 @@ export interface NormalizedComponentNode extends ComponentNode {
2525
/**
2626
* 根据低代码搭建协议的容器组件描述生成的容器模型
2727
*/
28-
export interface IComponentTreeModel<Component, ComponentInstance = unknown> {
28+
export interface IComponentTreeModel<Component, ComponentInstance = unknown> extends IDisposable {
2929
readonly id: string;
3030

3131
readonly codeRuntime: ICodeRuntime;
@@ -61,7 +61,7 @@ export type ModelDataSourceCreator = (
6161
codeRuntime: ICodeRuntime<InstanceApi>,
6262
) => InstanceDataSourceApi;
6363

64-
export interface ComponentTreeModelOptions extends IDisposable {
64+
export interface ComponentTreeModelOptions {
6565
id?: string;
6666
metadata?: StringDictionary;
6767

@@ -91,7 +91,7 @@ export class ComponentTreeModel<Component, ComponentInstance = unknown>
9191
this._id = options?.id ?? `model_${uniqueId()}`;
9292
this._metadata = options?.metadata ?? {};
9393
this.initialize(options);
94-
this.addDispose(_codeRuntime);
94+
this._addDispose(_codeRuntime);
9595
}
9696

9797
get id() {

packages/renderer-core/src/services/model/componentTreeModelService.ts renamed to packages/renderer-core/src/model/componentTreeModelService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class ComponentTreeModelService extends Disposable implements IComponentT
4848
): IComponentTreeModel<Component> {
4949
this._throwIfDisposed(`ComponentTreeModelService has been disposed.`);
5050

51-
return this.addDispose(
51+
return this._addDispose(
5252
new ComponentTreeModel(
5353
componentsTree,
5454
this.codeRuntimeService.createCodeRuntime({

packages/renderer-core/src/services/package/managementService.ts renamed to packages/renderer-core/src/package/managementService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class PackageManagementService extends Disposable implements IPackageMana
6363
constructor(@ISchemaService private schemaService: ISchemaService) {
6464
super();
6565

66-
this.addDispose(
66+
this._addDispose(
6767
this.schemaService.onSchemaUpdate(({ key, previous, data }) => {
6868
if (key === 'componentsMap') {
6969
// todo: add remove ...

packages/renderer-core/src/services/schema/schemaService.ts renamed to packages/renderer-core/src/schema/schemaService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const ISchemaService = createDecorator<ISchemaService>('schemaService');
2222
export class SchemaService extends Disposable implements ISchemaService {
2323
private store: NormalizedSchema;
2424

25-
private _observer = this.addDispose(new Events.Emitter<SchemaUpdateEvent>());
25+
private _observer = this._addDispose(new Events.Emitter<SchemaUpdateEvent>());
2626

2727
readonly onSchemaUpdate = this._observer.event;
2828

0 commit comments

Comments
 (0)