refactor: adjust dicts

This commit is contained in:
1ncounter 2024-08-01 10:01:57 +08:00
parent d4bdf14a1d
commit ddc2473f98
32 changed files with 117 additions and 122 deletions

View File

@ -116,7 +116,6 @@ export function createComponent(
useEffect(() => {
// trigger lifeCycles
// componentDidMount?.();
model.triggerLifeCycle('componentDidMount');
// 当 state 改变之后调用

View File

@ -142,8 +142,7 @@ export function WidgetComponent(props: WidgetRendererProps) {
},
);
if (process.env.NODE_ENV === 'development') {
// development 模式下 把 widget 的内容作为 prop ,便于排查问题
if (__DEV__) {
processedProps.widget = widget;
}

View File

@ -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 ?? {}),
@ -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);
}),
@ -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,

View File

@ -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);
@ -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),
);
}

View File

@ -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;

View File

@ -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,
container.set(
IRuntimeIntlService,
new CtorDescriptor(RuntimeIntlService, [defaultLocale, i18ns]),
);
instantiationService.container.set(IRuntimeIntlService, runtimeIntlService);
const extensionHostService = new ExtensionHostService(
lifeCycleService,
packageManagementService,
schemaService,
codeRuntimeService,
runtimeIntlService,
runtimeUtilService,
);
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);

View File

@ -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 };

View File

@ -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;
@ -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();
@ -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 {

View File

@ -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';

View File

@ -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';

View File

@ -0,0 +1 @@
export * from './intlService';

View File

@ -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;
@ -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]);
}

View File

@ -0,0 +1 @@
export * from './lifeCycleService';

View File

@ -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];
@ -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;
@ -61,7 +61,7 @@ export type ModelDataSourceCreator = (
codeRuntime: ICodeRuntime<InstanceApi>,
) => InstanceDataSourceApi;
export interface ComponentTreeModelOptions extends IDisposable {
export interface ComponentTreeModelOptions {
id?: string;
metadata?: StringDictionary;
@ -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() {

View File

@ -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({

View File

@ -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 ...

View File

@ -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;

View File

@ -1,32 +0,0 @@
import { type Project, type Package } from '@alilc/lowcode-shared';
import { type Plugin } from './services/extension';
import { type ISchemaService } from './services/schema';
import { type IPackageManagementService } from './services/package';
import { type CodeRuntimeOptions } from './services/code-runtime';
import { type ModelDataSourceCreator } from './services/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;

View File

@ -0,0 +1 @@
export * from './utilService';

View File

@ -5,8 +5,8 @@ import {
type StringDictionary,
} from '@alilc/lowcode-shared';
import { isPlainObject } from 'lodash-es';
import { IPackageManagementService } from './package';
import { ICodeRuntimeService } from './code-runtime';
import { IPackageManagementService } from '../package';
import { ICodeRuntimeService } from '../code-runtime';
export interface IRuntimeUtilService {
add(utilItem: UtilDescription, force?: boolean): void;