Skip to content

Commit

Permalink
refactor: render-core
Browse files Browse the repository at this point in the history
  • Loading branch information
1ncounter committed May 28, 2024
1 parent 8510f99 commit d632e7f
Show file tree
Hide file tree
Showing 112 changed files with 2,864 additions and 2,546 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
git-checks=false
git-checks=false
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,27 @@
"prepare": "husky"
},
"devDependencies": {
"@alilc/build-plugin-lce": "^0.0.5",
"@alilc/lowcode-test-mate": "^1.0.1",
"@changesets/cli": "^2.27.1",
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.1.0",
"@eslint/js": "^8.57.0",
"@microsoft/api-extractor": "^7.43.0",
"@stylistic/eslint-plugin": "^1.7.0",
"@types/node": "^20.11.30",
"@types/react-router": "5.1.18",
"@vanilla-extract/vite-plugin": "^4.0.7",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.57.0",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"globals": "^15.0.0",
"husky": "^9.0.11",
"less": "^4.2.0",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"rimraf": "^5.0.2",
"typescript": "^5.4.2",
"typescript-eslint": "^7.5.0",
"vite": "^5.2.9",
"vitest": "^1.5.0"
"vitest": "^1.6.0"
},
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=21.1.0",
Expand Down
3 changes: 0 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@
"test:cov": ""
},
"dependencies": {
"@abraham/reflection": "^0.12.0",
"@alilc/lowcode-shared": "workspace:*",
"@alilc/lowcode-types": "workspace:*",
"@alilc/lowcode-utils": "workspace:*",
"@formatjs/intl": "^2.10.1",
"inversify": "^6.0.2",
"inversify-binding-decorators": "^4.0.0",
"lodash-es": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
import { get as lodashGet, isPlainObject } from 'lodash-es';
import { createLogger, type PlainObject, invariant } from '@alilc/lowcode-shared';

const logger = createLogger({ level: 'log', bizName: 'config' });

// this default behavior will be different later
const STRICT_PLUGIN_MODE_DEFAULT = true;

interface ConfigurationOptions<Config extends PlainObject, K extends keyof Config = keyof Config> {
strictMode?: boolean;
setterValidator?: (key: K, value: Config[K]) => boolean | string;
}
import { get as lodashGet, isPlainObject, cloneDeep } from 'lodash-es';
import { type PlainObject } from '@alilc/lowcode-shared/src/types';
import { invariant } from '@alilc/lowcode-shared/src/utils';

export class Configuration<Config extends PlainObject, K extends keyof Config = keyof Config> {
#strictMode = STRICT_PLUGIN_MODE_DEFAULT;
#setterValidator: (key: K, value: Config[K]) => boolean | string = () => true;
private config: Config;

#config: Config = {} as Config;
private setterValidator: ((key: K, value: Config[K]) => boolean | string) | undefined;

#waits = new Map<
private waits = new Map<
K,
{
once?: boolean;
resolve: (data: any) => void;
}[]
>();

constructor(config: Config, options?: ConfigurationOptions<Config>) {
constructor(config: Config, setterValidator?: (key: K, value: Config[K]) => boolean | string) {
invariant(config, 'config must exist', 'Configuration');

this.#config = config;
this.config = cloneDeep(config);

const { strictMode, setterValidator } = options ?? {};

if (strictMode === false) {
this.#strictMode = false;
}
if (setterValidator) {
invariant(
typeof setterValidator === 'function',
'setterValidator must be a function',
'Configuration',
);
this.#setterValidator = setterValidator;
this.setterValidator = setterValidator;
}
}

Expand All @@ -50,38 +35,35 @@ export class Configuration<Config extends PlainObject, K extends keyof Config =
* @param key
*/
has(key: K): boolean {
return this.#config[key] !== undefined;
return this.config[key] !== undefined;
}

/**
* 获取指定 key 的值
* @param key
* @param defaultValue
*/
get(key: K, defaultValue?: any): any {
return lodashGet(this.#config, key, defaultValue);
get<T = any>(key: K, defaultValue?: T): T | undefined {
return lodashGet(this.config, key, defaultValue);
}

/**
* 设置指定 key 的值
* @param key
* @param value
*/
set(key: K, value: any) {
if (this.#strictMode) {
const valid = this.#setterValidator(key, value);
if (valid === false || typeof valid === 'string') {
return logger.warn(
`failed to config ${key.toString()}, only predefined options can be set under strict mode, predefined options: `,
valid ? valid : '',
);
}
if (this.setterValidator) {
const valid = this.setterValidator(key, value);

invariant(
valid === false || typeof valid === 'string',
`failed to config ${key.toString()}, only predefined options can be set under strict mode, predefined options: ${valid ? valid : ''}`,
'Configuration',
);
}

this.#config[key] = value;
this.config[key] = value;
this.notifyGot(key);
}

/**
* 批量设值,set 的对象版本
* @param config
Expand All @@ -93,31 +75,29 @@ export class Configuration<Config extends PlainObject, K extends keyof Config =
});
}
}

/**
* 获取指定 key 的值,若此时还未赋值,则等待,若已有值,则直接返回值
* 注:此函数返回 Promise 实例,只会执行(fullfill)一次
* @param key
* @returns
*/
onceGot(key: K) {
const val = this.#config[key];
const val = this.get(key);
if (val !== undefined) {
return Promise.resolve(val);
}
return new Promise((resolve) => {
this.setWait(key, resolve, true);
});
}

/**
* 获取指定 key 的值,函数回调模式,若多次被赋值,回调会被多次调用
* @param key
* @param fn
* @returns
*/
onGot(key: K, fn: (data: Config[K]) => void): () => void {
const val = this.#config[key];
const val = this.config[key];
if (val !== undefined) {
fn(val);
}
Expand All @@ -127,8 +107,8 @@ export class Configuration<Config extends PlainObject, K extends keyof Config =
};
}

notifyGot(key: K): void {
let waits = this.#waits.get(key);
private notifyGot(key: K): void {
let waits = this.waits.get(key);
if (!waits) {
return;
}
Expand All @@ -141,23 +121,23 @@ export class Configuration<Config extends PlainObject, K extends keyof Config =
}
}
if (waits.length > 0) {
this.#waits.set(key, waits);
this.waits.set(key, waits);
} else {
this.#waits.delete(key);
this.waits.delete(key);
}
}

setWait(key: K, resolve: (data: any) => void, once?: boolean) {
const waits = this.#waits.get(key);
private setWait(key: K, resolve: (data: any) => void, once?: boolean) {
const waits = this.waits.get(key);
if (waits) {
waits.push({ resolve, once });
} else {
this.#waits.set(key, [{ resolve, once }]);
this.waits.set(key, [{ resolve, once }]);
}
}

delWait(key: K, fn: any) {
const waits = this.#waits.get(key);
private delWait(key: K, fn: any) {
const waits = this.waits.get(key);
if (!waits) {
return;
}
Expand All @@ -168,7 +148,7 @@ export class Configuration<Config extends PlainObject, K extends keyof Config =
}
}
if (waits.length < 1) {
this.#waits.delete(key);
this.waits.delete(key);
}
}
}
2 changes: 0 additions & 2 deletions packages/core/src/configuration/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './configuration';
export * from './preference';
export * from './hotkey';
export * from './intl';
export * from './instantiation';
43 changes: 0 additions & 43 deletions packages/core/src/instantiation/index.ts

This file was deleted.

24 changes: 4 additions & 20 deletions packages/core/src/intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
computed,
effect,
createLogger,
type Spec,
type Signal,
type I18nMap,
type ComputedSignal,
type PlainObject,
} from '@alilc/lowcode-shared';
Expand All @@ -21,8 +21,8 @@ const logger = createLogger({ level: 'warn', bizName: 'globalLocale' });
const STORED_LOCALE_KEY = 'ali-lowcode-config';

export type Locale = string;
export type IntlMessage = I18nMap[Locale];
export type IntlMessageRecord = I18nMap;
export type IntlMessage = Spec.I18nMap[Locale];
export type IntlMessageRecord = Spec.I18nMap;

export class Intl {
#locale: Signal<Locale>;
Expand All @@ -34,7 +34,7 @@ export class Intl {
if (defaultLocale) {
defaultLocale = nomarlizeLocale(defaultLocale);
} else {
defaultLocale = initializeLocale();
defaultLocale = 'zh-CN';
}

const messageStore = mapKeys(messages, (_, key) => {
Expand Down Expand Up @@ -65,22 +65,6 @@ export class Intl {

setLocale(locale: Locale) {
const nomarlizedLocale = nomarlizeLocale(locale);

try {
// store storage
let config = JSON.parse(localStorage.getItem(STORED_LOCALE_KEY) || '');

if (config && typeof config === 'object') {
config.locale = locale;
} else {
config = { locale };
}

localStorage.setItem(STORED_LOCALE_KEY, JSON.stringify(config));
} catch {
// ignore;
}

this.#locale.value = nomarlizedLocale;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,3 @@ export class Preference {
return !(result === undefined || result === null);
}
}

export const userPreference = new Preference();
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "dist"
},
"include": ["src", "__tests__"]
"include": ["src", "__tests__", "src/configuration.ts"]
}
4 changes: 2 additions & 2 deletions packages/designer/src/models/document/document-model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { signal, uniqueId, type ComponentTreeRootNode } from '@alilc/lowcode-shared';
import { signal, uniqueId, type Spec } from '@alilc/lowcode-shared';
import { type Project } from '../project';
import { History } from './history';

export interface DocumentSchema extends ComponentTreeRootNode {
export interface DocumentSchema extends Spec.ComponentTreeRoot {
id: string;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/designer/src/models/node/node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ComponentTreeNode } from '@alilc/lowcode-shared';
import { Spec } from '@alilc/lowcode-shared';
import { type ComponentMeta } from '../component-meta';
import { type Prop } from './prop';

export interface Node<Schema extends ComponentTreeNode = ComponentTreeNode> {
export interface Node<Schema extends Spec.ComponentNode = Spec.ComponentNode> {
/**
* 节点 id
* node id
Expand Down Expand Up @@ -353,6 +353,6 @@ export interface Node<Schema extends ComponentTreeNode = ComponentTreeNode> {
};
}

export function createNode<Schema extends ComponentTreeNode>(nodeSchema: Schema): Node<Schema> {
export function createNode<Schema extends Spec.ComponentNode>(nodeSchema: Schema): Node<Schema> {
return {};
}
Loading

0 comments on commit d632e7f

Please sign in to comment.