Skip to content

Commit

Permalink
refactor: adjust dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
1ncounter committed Aug 1, 2024
1 parent d4bdf14 commit ddc2473
Show file tree
Hide file tree
Showing 32 changed files with 118 additions and 123 deletions.
1 change: 0 additions & 1 deletion packages/react-renderer/src/runtime/createComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export function createComponent(

useEffect(() => {
// trigger lifeCycles
// componentDidMount?.();
model.triggerLifeCycle('componentDidMount');

// 当 state 改变之后调用
Expand Down
3 changes: 1 addition & 2 deletions packages/react-renderer/src/runtime/elements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ export function WidgetComponent(props: WidgetRendererProps) {
},
);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
super();

if (options.evalCodeFunction) this._evalCodeFunction = options.evalCodeFunction;
this._codeScope = this.addDispose(
this._codeScope = this._addDispose(
options.parentScope
? options.parentScope.createChild<T>(options.initScopeValue ?? {})
: new CodeScope(options.initScopeValue ?? {}),
Expand Down Expand Up @@ -122,7 +122,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
onResolve(handler: NodeResolverHandler): IDisposable {
this._resolveHandlers.push(handler);

return this.addDispose(
return this._addDispose(
toDisposable(() => {
this._resolveHandlers = this._resolveHandlers.filter((h) => h !== handler);
}),
Expand All @@ -132,7 +132,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
createChild<V extends StringDictionary = StringDictionary>(
options?: Omit<CodeRuntimeOptions<V>, 'parentScope'>,
): ICodeRuntime<V> {
return this.addDispose(
return this._addDispose(
new CodeRuntime({
initScopeValue: options?.initScopeValue,
parentScope: this._codeScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class CodeRuntimeService extends Disposable implements ICodeRuntimeServic
) {
super();

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

return this.addDispose(
return this._addDispose(
options.parentScope ? new CodeRuntime(options) : this.rootRuntime.createChild<T>(options),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class CodeScope<T extends StringDictionary = StringDictionary>
}

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

return childScope;
Expand Down
109 changes: 68 additions & 41 deletions packages/renderer-core/src/createRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,98 @@
import { invariant, InstantiationService } from '@alilc/lowcode-shared';
import type { AppOptions, RendererApplication } from './types';
import { CodeRuntimeService, ICodeRuntimeService } from './services/code-runtime';
import {
invariant,
InstantiationService,
BeanContainer,
CtorDescriptor,
type Project,
type Package,
} from '@alilc/lowcode-shared';
import { CodeRuntimeService, ICodeRuntimeService, type CodeRuntimeOptions } from './code-runtime';
import {
IExtensionHostService,
type RenderAdapter,
type IRenderObject,
ExtensionHostService,
} from './services/extension';
import { IPackageManagementService, PackageManagementService } from './services/package';
import { ISchemaService, SchemaService } from './services/schema';
import { ILifeCycleService, LifecyclePhase, LifeCycleService } from './services/lifeCycleService';
import { IRuntimeIntlService, RuntimeIntlService } from './services/runtimeIntlService';
import { IRuntimeUtilService, RuntimeUtilService } from './services/runtimeUtilService';
type Plugin,
} from './extension';
import { IPackageManagementService, PackageManagementService } from './package';
import { ISchemaService, SchemaService } from './schema';
import { ILifeCycleService, LifecyclePhase, LifeCycleService } from './life-cycle';
import { IRuntimeIntlService, RuntimeIntlService } from './intl';
import { IRuntimeUtilService, RuntimeUtilService } from './util';
import { type ModelDataSourceCreator } from './model';

export interface AppOptions {
schema: Project;
packages?: Package[];
plugins?: Plugin[];
/**
* code runtime 设置选项
*/
codeRuntime?: CodeRuntimeOptions;
/**
* 数据源创建工厂函数
*/
dataSourceCreator?: ModelDataSourceCreator;
}

export type RendererApplication<Render = unknown> = {
readonly mode: 'development' | 'production';

readonly schema: Omit<ISchemaService, 'initialize'>;

readonly packageManager: IPackageManagementService;

use(plugin: Plugin): Promise<void>;

destroy(): void;
} & Render;

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

const instantiationService = new InstantiationService();

// create services
const lifeCycleService = new LifeCycleService();
instantiationService.container.set(ILifeCycleService, lifeCycleService);

return async (options) => {
// create services
const container = new BeanContainer();
const lifeCycleService = new LifeCycleService();
container.set(ILifeCycleService, lifeCycleService);

const schemaService = new SchemaService(options.schema);
instantiationService.container.set(ISchemaService, schemaService);
container.set(ISchemaService, schemaService);

const codeRuntimeService = instantiationService.createInstance(
CodeRuntimeService,
options.codeRuntime,
container.set(
ICodeRuntimeService,
new CtorDescriptor(CodeRuntimeService, [options.codeRuntime]),
);
instantiationService.container.set(ICodeRuntimeService, codeRuntimeService);

const packageManagementService = instantiationService.createInstance(PackageManagementService);
instantiationService.container.set(IPackageManagementService, packageManagementService);
container.set(IPackageManagementService, new CtorDescriptor(PackageManagementService));

const utils = schemaService.get('utils');
const runtimeUtilService = instantiationService.createInstance(RuntimeUtilService, utils);
instantiationService.container.set(IRuntimeUtilService, runtimeUtilService);
container.set(IRuntimeUtilService, new CtorDescriptor(RuntimeUtilService, [utils]));

const defaultLocale = schemaService.get('config.defaultLocale');
const i18ns = schemaService.get('i18n', {});
const runtimeIntlService = instantiationService.createInstance(
RuntimeIntlService,
defaultLocale,
i18ns,
);
instantiationService.container.set(IRuntimeIntlService, runtimeIntlService);

const extensionHostService = new ExtensionHostService(
lifeCycleService,
packageManagementService,
schemaService,
codeRuntimeService,
runtimeIntlService,
runtimeUtilService,
container.set(
IRuntimeIntlService,
new CtorDescriptor(RuntimeIntlService, [defaultLocale, i18ns]),
);
instantiationService.container.set(IExtensionHostService, extensionHostService);

container.set(IExtensionHostService, new CtorDescriptor(ExtensionHostService));

const instantiationService = new InstantiationService(container);

lifeCycleService.setPhase(LifecyclePhase.OptionsResolved);

const [extensionHostService, packageManagementService] = instantiationService.invokeFunction(
(accessor) => {
return [accessor.get(IExtensionHostService), accessor.get(IPackageManagementService)];
},
);

const renderObject = await renderAdapter(instantiationService);

await extensionHostService.registerPlugin(options.plugins ?? []);
// 先加载插件提供 package loader

await packageManagementService.loadPackages(options.packages ?? []);

lifeCycleService.setPhase(LifecyclePhase.Ready);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { type StringDictionary } from '@alilc/lowcode-shared';
import { isObject } from 'lodash-es';
import { ICodeRuntime, ICodeRuntimeService } from '../code-runtime';
import { IRuntimeUtilService } from '../runtimeUtilService';
import { IRuntimeIntlService } from '../runtimeIntlService';
import { IRuntimeUtilService } from '../util/utilService';
import { IRuntimeIntlService } from '../intlService';

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { type Plugin, type PluginContext } from './plugin';
import { BoostsManager } from './boosts';
import { IPackageManagementService } from '../package';
import { ISchemaService } from '../schema';
import { ILifeCycleService } from '../lifeCycleService';
import { ILifeCycleService } from '../life-cycle/lifeCycleService';
import { ICodeRuntimeService } from '../code-runtime';
import { IRuntimeIntlService } from '../runtimeIntlService';
import { IRuntimeUtilService } from '../runtimeUtilService';
import { IRuntimeIntlService } from '../intl';
import { IRuntimeUtilService } from '../util';

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

constructor(
lifeCycleService: ILifeCycleService,
packageManagementService: IPackageManagementService,
schemaService: ISchemaService,
codeRuntimeService: ICodeRuntimeService,
runtimeIntlService: IRuntimeIntlService,
runtimeUtilService: IRuntimeUtilService,
@ILifeCycleService lifeCycleService: ILifeCycleService,
@IPackageManagementService packageManagementService: IPackageManagementService,
@ISchemaService schemaService: ISchemaService,
@ICodeRuntimeService codeRuntimeService: ICodeRuntimeService,
@IRuntimeIntlService runtimeIntlService: IRuntimeIntlService,
@IRuntimeUtilService runtimeUtilService: IRuntimeUtilService,
) {
super();

Expand Down Expand Up @@ -103,7 +103,7 @@ export class ExtensionHostService extends Disposable implements IExtensionHostSe

await plugin.setup(this._pluginSetupContext);
this._activePlugins.add(plugin.name);
this.addDispose(plugin);
this._addDispose(plugin);
}

getPlugin(name: string): Plugin | undefined {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type StringDictionary, type IDisposable } from '@alilc/lowcode-shared';
import { type IBoosts } from './boosts';
import { ILifeCycleService } from '../lifeCycleService';
import { ILifeCycleService } from '../life-cycle/lifeCycleService';
import { type ISchemaService } from '../schema';
import { type IPackageManagementService } from '../package';
import { type IStore } from '../../utils/store';
Expand Down
31 changes: 15 additions & 16 deletions packages/renderer-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/* --------------- api -------------------- */
export { createRenderer } from './createRenderer';
export { IExtensionHostService } from './services/extension';
export { definePackageLoader, IPackageManagementService } from './services/package';
export { LifecyclePhase, ILifeCycleService } from './services/lifeCycleService';
export { IComponentTreeModelService } from './services/model';
export { ICodeRuntimeService } from './services/code-runtime';
export { IRuntimeIntlService } from './services/runtimeIntlService';
export { IRuntimeUtilService } from './services/runtimeUtilService';
export { ISchemaService } from './services/schema';
export * from './createRenderer';
export { IExtensionHostService } from './extension';
export { definePackageLoader, IPackageManagementService } from './package';
export { LifecyclePhase, ILifeCycleService } from './life-cycle';
export { IComponentTreeModelService } from './model';
export { ICodeRuntimeService, mapValue, someValue } from './code-runtime';
export { IRuntimeIntlService } from './intl';
export { IRuntimeUtilService } from './util';
export { ISchemaService } from './schema';
export { Widget } from './widget';

/* --------------- types ---------------- */
export type * from './types';
export type * from './services/extension';
export type * from './services/code-runtime';
export type * from './services/model';
export type * from './services/package';
export type * from './services/schema';
export type * from './services/extension';
export type * from './extension';
export type * from './code-runtime';
export type * from './model';
export type * from './package';
export type * from './schema';
export type * from './extension';
export type * from './widget';
1 change: 1 addition & 0 deletions packages/renderer-core/src/intl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './intlService';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
type LocaleTranslationsMap,
Disposable,
} from '@alilc/lowcode-shared';
import { ICodeRuntimeService } from './code-runtime';
import { ICodeRuntimeService } from '../code-runtime';

export interface MessageDescriptor {
key: string;
Expand Down Expand Up @@ -37,7 +37,7 @@ export class RuntimeIntlService extends Disposable implements IRuntimeIntlServic
) {
super();

this._intl = this.addDispose(new Intl(defaultLocale));
this._intl = this._addDispose(new Intl(defaultLocale));
for (const key of Object.keys(i18nTranslations)) {
this._intl.addTranslations(key, i18nTranslations[key]);
}
Expand Down
1 change: 1 addition & 0 deletions packages/renderer-core/src/life-cycle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lifeCycleService';
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Disposable,
} from '@alilc/lowcode-shared';
import { type ICodeRuntime } from '../code-runtime';
import { IWidget, Widget } from '../../widget';
import { IWidget, Widget } from '../widget';

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

readonly codeRuntime: ICodeRuntime;
Expand Down Expand Up @@ -61,7 +61,7 @@ export type ModelDataSourceCreator = (
codeRuntime: ICodeRuntime<InstanceApi>,
) => InstanceDataSourceApi;

export interface ComponentTreeModelOptions extends IDisposable {
export interface ComponentTreeModelOptions {
id?: string;
metadata?: StringDictionary;

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

get id() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ComponentTreeModelService extends Disposable implements IComponentT
): IComponentTreeModel<Component> {
this._throwIfDisposed(`ComponentTreeModelService has been disposed.`);

return this.addDispose(
return this._addDispose(
new ComponentTreeModel(
componentsTree,
this.codeRuntimeService.createCodeRuntime({
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class PackageManagementService extends Disposable implements IPackageMana
constructor(@ISchemaService private schemaService: ISchemaService) {
super();

this.addDispose(
this._addDispose(
this.schemaService.onSchemaUpdate(({ key, previous, data }) => {
if (key === 'componentsMap') {
// todo: add remove ...
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const ISchemaService = createDecorator<ISchemaService>('schemaService');
export class SchemaService extends Disposable implements ISchemaService {
private store: NormalizedSchema;

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

readonly onSchemaUpdate = this._observer.event;

Expand Down
Loading

0 comments on commit ddc2473

Please sign in to comment.