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(() => { useEffect(() => {
// trigger lifeCycles // trigger lifeCycles
// componentDidMount?.();
model.triggerLifeCycle('componentDidMount'); model.triggerLifeCycle('componentDidMount');
// 当 state 改变之后调用 // 当 state 改变之后调用

View File

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

View File

@ -53,7 +53,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
super(); super();
if (options.evalCodeFunction) this._evalCodeFunction = options.evalCodeFunction; if (options.evalCodeFunction) this._evalCodeFunction = options.evalCodeFunction;
this._codeScope = this.addDispose( this._codeScope = this._addDispose(
options.parentScope options.parentScope
? options.parentScope.createChild<T>(options.initScopeValue ?? {}) ? options.parentScope.createChild<T>(options.initScopeValue ?? {})
: new CodeScope(options.initScopeValue ?? {}), : new CodeScope(options.initScopeValue ?? {}),
@ -122,7 +122,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
onResolve(handler: NodeResolverHandler): IDisposable { onResolve(handler: NodeResolverHandler): IDisposable {
this._resolveHandlers.push(handler); this._resolveHandlers.push(handler);
return this.addDispose( return this._addDispose(
toDisposable(() => { toDisposable(() => {
this._resolveHandlers = this._resolveHandlers.filter((h) => h !== handler); this._resolveHandlers = this._resolveHandlers.filter((h) => h !== handler);
}), }),
@ -132,7 +132,7 @@ export class CodeRuntime<T extends StringDictionary = StringDictionary>
createChild<V extends StringDictionary = StringDictionary>( createChild<V extends StringDictionary = StringDictionary>(
options?: Omit<CodeRuntimeOptions<V>, 'parentScope'>, options?: Omit<CodeRuntimeOptions<V>, 'parentScope'>,
): ICodeRuntime<V> { ): ICodeRuntime<V> {
return this.addDispose( return this._addDispose(
new CodeRuntime({ new CodeRuntime({
initScopeValue: options?.initScopeValue, initScopeValue: options?.initScopeValue,
parentScope: this._codeScope, parentScope: this._codeScope,

View File

@ -29,8 +29,8 @@ export class CodeRuntimeService extends Disposable implements ICodeRuntimeServic
) { ) {
super(); super();
this._rootRuntime = this.addDispose(new CodeRuntime(options)); this._rootRuntime = this._addDispose(new CodeRuntime(options));
this.addDispose( this._addDispose(
this.schemaService.onSchemaUpdate(({ key, data }) => { this.schemaService.onSchemaUpdate(({ key, data }) => {
if (key === 'constants') { if (key === 'constants') {
this.rootRuntime.getScope().set('constants', data); this.rootRuntime.getScope().set('constants', data);
@ -44,7 +44,7 @@ export class CodeRuntimeService extends Disposable implements ICodeRuntimeServic
): ICodeRuntime<T> { ): ICodeRuntime<T> {
this._throwIfDisposed(); this._throwIfDisposed();
return this.addDispose( return this._addDispose(
options.parentScope ? new CodeRuntime(options) : this.rootRuntime.createChild<T>(options), 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> { 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; childScope.node.prev = this.node;
return childScope; return childScope;

View File

@ -1,71 +1,98 @@
import { invariant, InstantiationService } from '@alilc/lowcode-shared'; import {
import type { AppOptions, RendererApplication } from './types'; invariant,
import { CodeRuntimeService, ICodeRuntimeService } from './services/code-runtime'; InstantiationService,
BeanContainer,
CtorDescriptor,
type Project,
type Package,
} from '@alilc/lowcode-shared';
import { CodeRuntimeService, ICodeRuntimeService, type CodeRuntimeOptions } from './code-runtime';
import { import {
IExtensionHostService, IExtensionHostService,
type RenderAdapter, type RenderAdapter,
type IRenderObject, type IRenderObject,
ExtensionHostService, ExtensionHostService,
} from './services/extension'; type Plugin,
import { IPackageManagementService, PackageManagementService } from './services/package'; } from './extension';
import { ISchemaService, SchemaService } from './services/schema'; import { IPackageManagementService, PackageManagementService } from './package';
import { ILifeCycleService, LifecyclePhase, LifeCycleService } from './services/lifeCycleService'; import { ISchemaService, SchemaService } from './schema';
import { IRuntimeIntlService, RuntimeIntlService } from './services/runtimeIntlService'; import { ILifeCycleService, LifecyclePhase, LifeCycleService } from './life-cycle';
import { IRuntimeUtilService, RuntimeUtilService } from './services/runtimeUtilService'; 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>( export function createRenderer<RenderObject = IRenderObject>(
renderAdapter: RenderAdapter<RenderObject>, renderAdapter: RenderAdapter<RenderObject>,
): (options: AppOptions) => Promise<RendererApplication<RenderObject>> { ): (options: AppOptions) => Promise<RendererApplication<RenderObject>> {
invariant(typeof renderAdapter === 'function', 'The first parameter must be a function.'); 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) => { return async (options) => {
// create services
const container = new BeanContainer();
const lifeCycleService = new LifeCycleService();
container.set(ILifeCycleService, lifeCycleService);
const schemaService = new SchemaService(options.schema); const schemaService = new SchemaService(options.schema);
instantiationService.container.set(ISchemaService, schemaService); container.set(ISchemaService, schemaService);
const codeRuntimeService = instantiationService.createInstance( container.set(
CodeRuntimeService, ICodeRuntimeService,
options.codeRuntime, new CtorDescriptor(CodeRuntimeService, [options.codeRuntime]),
); );
instantiationService.container.set(ICodeRuntimeService, codeRuntimeService); container.set(IPackageManagementService, new CtorDescriptor(PackageManagementService));
const packageManagementService = instantiationService.createInstance(PackageManagementService);
instantiationService.container.set(IPackageManagementService, packageManagementService);
const utils = schemaService.get('utils'); const utils = schemaService.get('utils');
const runtimeUtilService = instantiationService.createInstance(RuntimeUtilService, utils); container.set(IRuntimeUtilService, new CtorDescriptor(RuntimeUtilService, [utils]));
instantiationService.container.set(IRuntimeUtilService, runtimeUtilService);
const defaultLocale = schemaService.get('config.defaultLocale'); const defaultLocale = schemaService.get('config.defaultLocale');
const i18ns = schemaService.get('i18n', {}); const i18ns = schemaService.get('i18n', {});
const runtimeIntlService = instantiationService.createInstance( container.set(
RuntimeIntlService, IRuntimeIntlService,
defaultLocale, new CtorDescriptor(RuntimeIntlService, [defaultLocale, i18ns]),
i18ns,
); );
instantiationService.container.set(IRuntimeIntlService, runtimeIntlService);
const extensionHostService = new ExtensionHostService( container.set(IExtensionHostService, new CtorDescriptor(ExtensionHostService));
lifeCycleService,
packageManagementService, const instantiationService = new InstantiationService(container);
schemaService,
codeRuntimeService,
runtimeIntlService,
runtimeUtilService,
);
instantiationService.container.set(IExtensionHostService, extensionHostService);
lifeCycleService.setPhase(LifecyclePhase.OptionsResolved); lifeCycleService.setPhase(LifecyclePhase.OptionsResolved);
const [extensionHostService, packageManagementService] = instantiationService.invokeFunction(
(accessor) => {
return [accessor.get(IExtensionHostService), accessor.get(IPackageManagementService)];
},
);
const renderObject = await renderAdapter(instantiationService); const renderObject = await renderAdapter(instantiationService);
await extensionHostService.registerPlugin(options.plugins ?? []); await extensionHostService.registerPlugin(options.plugins ?? []);
// 先加载插件提供 package loader
await packageManagementService.loadPackages(options.packages ?? []); await packageManagementService.loadPackages(options.packages ?? []);
lifeCycleService.setPhase(LifecyclePhase.Ready); lifeCycleService.setPhase(LifecyclePhase.Ready);

View File

@ -1,8 +1,8 @@
import { type StringDictionary } from '@alilc/lowcode-shared'; import { type StringDictionary } from '@alilc/lowcode-shared';
import { isObject } from 'lodash-es'; import { isObject } from 'lodash-es';
import { ICodeRuntime, ICodeRuntimeService } from '../code-runtime'; import { ICodeRuntime, ICodeRuntimeService } from '../code-runtime';
import { IRuntimeUtilService } from '../runtimeUtilService'; import { IRuntimeUtilService } from '../util/utilService';
import { IRuntimeIntlService } from '../runtimeIntlService'; import { IRuntimeIntlService } from '../intlService';
export type IBoosts<Extends> = IBoostsApi & Extends & { [key: string]: any }; 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 { BoostsManager } from './boosts';
import { IPackageManagementService } from '../package'; import { IPackageManagementService } from '../package';
import { ISchemaService } from '../schema'; import { ISchemaService } from '../schema';
import { ILifeCycleService } from '../lifeCycleService'; import { ILifeCycleService } from '../life-cycle/lifeCycleService';
import { ICodeRuntimeService } from '../code-runtime'; import { ICodeRuntimeService } from '../code-runtime';
import { IRuntimeIntlService } from '../runtimeIntlService'; import { IRuntimeIntlService } from '../intl';
import { IRuntimeUtilService } from '../runtimeUtilService'; import { IRuntimeUtilService } from '../util';
export interface IExtensionHostService { export interface IExtensionHostService {
readonly boostsManager: BoostsManager; readonly boostsManager: BoostsManager;
@ -28,12 +28,12 @@ export class ExtensionHostService extends Disposable implements IExtensionHostSe
private _pluginSetupContext: PluginContext; private _pluginSetupContext: PluginContext;
constructor( constructor(
lifeCycleService: ILifeCycleService, @ILifeCycleService lifeCycleService: ILifeCycleService,
packageManagementService: IPackageManagementService, @IPackageManagementService packageManagementService: IPackageManagementService,
schemaService: ISchemaService, @ISchemaService schemaService: ISchemaService,
codeRuntimeService: ICodeRuntimeService, @ICodeRuntimeService codeRuntimeService: ICodeRuntimeService,
runtimeIntlService: IRuntimeIntlService, @IRuntimeIntlService runtimeIntlService: IRuntimeIntlService,
runtimeUtilService: IRuntimeUtilService, @IRuntimeUtilService runtimeUtilService: IRuntimeUtilService,
) { ) {
super(); super();
@ -103,7 +103,7 @@ export class ExtensionHostService extends Disposable implements IExtensionHostSe
await plugin.setup(this._pluginSetupContext); await plugin.setup(this._pluginSetupContext);
this._activePlugins.add(plugin.name); this._activePlugins.add(plugin.name);
this.addDispose(plugin); this._addDispose(plugin);
} }
getPlugin(name: string): Plugin | undefined { getPlugin(name: string): Plugin | undefined {

View File

@ -1,6 +1,6 @@
import { type StringDictionary, type IDisposable } from '@alilc/lowcode-shared'; import { type StringDictionary, type IDisposable } from '@alilc/lowcode-shared';
import { type IBoosts } from './boosts'; import { type IBoosts } from './boosts';
import { ILifeCycleService } from '../lifeCycleService'; import { ILifeCycleService } from '../life-cycle/lifeCycleService';
import { type ISchemaService } from '../schema'; import { type ISchemaService } from '../schema';
import { type IPackageManagementService } from '../package'; import { type IPackageManagementService } from '../package';
import { type IStore } from '../../utils/store'; import { type IStore } from '../../utils/store';

View File

@ -1,21 +1,20 @@
/* --------------- api -------------------- */ /* --------------- api -------------------- */
export { createRenderer } from './createRenderer'; export * from './createRenderer';
export { IExtensionHostService } from './services/extension'; export { IExtensionHostService } from './extension';
export { definePackageLoader, IPackageManagementService } from './services/package'; export { definePackageLoader, IPackageManagementService } from './package';
export { LifecyclePhase, ILifeCycleService } from './services/lifeCycleService'; export { LifecyclePhase, ILifeCycleService } from './life-cycle';
export { IComponentTreeModelService } from './services/model'; export { IComponentTreeModelService } from './model';
export { ICodeRuntimeService } from './services/code-runtime'; export { ICodeRuntimeService, mapValue, someValue } from './code-runtime';
export { IRuntimeIntlService } from './services/runtimeIntlService'; export { IRuntimeIntlService } from './intl';
export { IRuntimeUtilService } from './services/runtimeUtilService'; export { IRuntimeUtilService } from './util';
export { ISchemaService } from './services/schema'; export { ISchemaService } from './schema';
export { Widget } from './widget'; export { Widget } from './widget';
/* --------------- types ---------------- */ /* --------------- types ---------------- */
export type * from './types'; export type * from './extension';
export type * from './services/extension'; export type * from './code-runtime';
export type * from './services/code-runtime'; export type * from './model';
export type * from './services/model'; export type * from './package';
export type * from './services/package'; export type * from './schema';
export type * from './services/schema'; export type * from './extension';
export type * from './services/extension';
export type * from './widget'; export type * from './widget';

View File

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

View File

@ -7,7 +7,7 @@ import {
type LocaleTranslationsMap, type LocaleTranslationsMap,
Disposable, Disposable,
} from '@alilc/lowcode-shared'; } from '@alilc/lowcode-shared';
import { ICodeRuntimeService } from './code-runtime'; import { ICodeRuntimeService } from '../code-runtime';
export interface MessageDescriptor { export interface MessageDescriptor {
key: string; key: string;
@ -37,7 +37,7 @@ export class RuntimeIntlService extends Disposable implements IRuntimeIntlServic
) { ) {
super(); super();
this._intl = this.addDispose(new Intl(defaultLocale)); this._intl = this._addDispose(new Intl(defaultLocale));
for (const key of Object.keys(i18nTranslations)) { for (const key of Object.keys(i18nTranslations)) {
this._intl.addTranslations(key, i18nTranslations[key]); this._intl.addTranslations(key, i18nTranslations[key]);
} }

View File

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

View File

@ -15,7 +15,7 @@ import {
Disposable, Disposable,
} from '@alilc/lowcode-shared'; } from '@alilc/lowcode-shared';
import { type ICodeRuntime } from '../code-runtime'; import { type ICodeRuntime } from '../code-runtime';
import { IWidget, Widget } from '../../widget'; import { IWidget, Widget } from '../widget';
export interface NormalizedComponentNode extends ComponentNode { export interface NormalizedComponentNode extends ComponentNode {
loopArgs: [string, string]; 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 id: string;
readonly codeRuntime: ICodeRuntime; readonly codeRuntime: ICodeRuntime;
@ -61,7 +61,7 @@ export type ModelDataSourceCreator = (
codeRuntime: ICodeRuntime<InstanceApi>, codeRuntime: ICodeRuntime<InstanceApi>,
) => InstanceDataSourceApi; ) => InstanceDataSourceApi;
export interface ComponentTreeModelOptions extends IDisposable { export interface ComponentTreeModelOptions {
id?: string; id?: string;
metadata?: StringDictionary; metadata?: StringDictionary;
@ -91,7 +91,7 @@ export class ComponentTreeModel<Component, ComponentInstance = unknown>
this._id = options?.id ?? `model_${uniqueId()}`; this._id = options?.id ?? `model_${uniqueId()}`;
this._metadata = options?.metadata ?? {}; this._metadata = options?.metadata ?? {};
this.initialize(options); this.initialize(options);
this.addDispose(_codeRuntime); this._addDispose(_codeRuntime);
} }
get id() { get id() {

View File

@ -48,7 +48,7 @@ export class ComponentTreeModelService extends Disposable implements IComponentT
): IComponentTreeModel<Component> { ): IComponentTreeModel<Component> {
this._throwIfDisposed(`ComponentTreeModelService has been disposed.`); this._throwIfDisposed(`ComponentTreeModelService has been disposed.`);
return this.addDispose( return this._addDispose(
new ComponentTreeModel( new ComponentTreeModel(
componentsTree, componentsTree,
this.codeRuntimeService.createCodeRuntime({ this.codeRuntimeService.createCodeRuntime({

View File

@ -63,7 +63,7 @@ export class PackageManagementService extends Disposable implements IPackageMana
constructor(@ISchemaService private schemaService: ISchemaService) { constructor(@ISchemaService private schemaService: ISchemaService) {
super(); super();
this.addDispose( this._addDispose(
this.schemaService.onSchemaUpdate(({ key, previous, data }) => { this.schemaService.onSchemaUpdate(({ key, previous, data }) => {
if (key === 'componentsMap') { if (key === 'componentsMap') {
// todo: add remove ... // todo: add remove ...

View File

@ -22,7 +22,7 @@ export const ISchemaService = createDecorator<ISchemaService>('schemaService');
export class SchemaService extends Disposable implements ISchemaService { export class SchemaService extends Disposable implements ISchemaService {
private store: NormalizedSchema; private store: NormalizedSchema;
private _observer = this.addDispose(new Events.Emitter<SchemaUpdateEvent>()); private _observer = this._addDispose(new Events.Emitter<SchemaUpdateEvent>());
readonly onSchemaUpdate = this._observer.event; 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, type StringDictionary,
} from '@alilc/lowcode-shared'; } from '@alilc/lowcode-shared';
import { isPlainObject } from 'lodash-es'; import { isPlainObject } from 'lodash-es';
import { IPackageManagementService } from './package'; import { IPackageManagementService } from '../package';
import { ICodeRuntimeService } from './code-runtime'; import { ICodeRuntimeService } from '../code-runtime';
export interface IRuntimeUtilService { export interface IRuntimeUtilService {
add(utilItem: UtilDescription, force?: boolean): void; add(utilItem: UtilDescription, force?: boolean): void;