GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor: abstract excuteLifeCycleMethod and some minor refactors · xzlinux/lowcode-engine@706dad4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/renderer-core
    • jest.config.js
    • src/renderer
      • base.tsx
      • page.tsx
    • tests/renderer
      • base.test.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/renderer-core/jest.config.js‎

Copy file name to clipboardExpand all lines: packages/renderer-core/jest.config.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const jestConfig = {
1010
// // '^.+\\.(js|jsx)$': 'babel-jest',
1111
// },
1212
// testMatch: ['(/tests?/.*(test))\\.[jt]s$'],
13-
// testMatch: ['**/*/common.test.ts'],
13+
// testMatch: ['**/*/base.test.tsx'],
1414
transformIgnorePatterns: [
1515
`/node_modules/(?!${esModules})/`,
1616
],
Collapse file

‎packages/renderer-core/src/renderer/base.tsx‎

Copy file name to clipboardExpand all lines: packages/renderer-core/src/renderer/base.tsx
+54-51Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,38 @@ import { IComponentConstruct, IComponentHoc, leafWrapper } from '../hoc/leaf';
3535
import logger from '../utils/logger';
3636
import isUseLoop from '../utils/is-use-loop';
3737

38+
/**
39+
* execute method in schema.lifeCycles with context
40+
* @PRIVATE
41+
*/
42+
export function excuteLifeCycleMethod(context: any, schema: NodeSchema, method: string, args: any, thisRequiredInJSE: boolean | undefined): any {
43+
if (!context || !isSchema(schema) || !method) {
44+
return;
45+
}
46+
const lifeCycleMethods = getValue(schema, 'lifeCycles', {});
47+
let fn = lifeCycleMethods[method];
48+
49+
if (!fn) {
50+
return;
51+
}
52+
53+
// TODO: cache
54+
if (isJSExpression(fn) || isJSFunction(fn)) {
55+
fn = thisRequiredInJSE ? parseThisRequiredExpression(fn, context) : parseExpression(fn, context);
56+
}
57+
58+
if (typeof fn !== 'function') {
59+
console.error(`生命周期${method}类型不符`, fn);
60+
return;
61+
}
62+
63+
try {
64+
return fn.apply(context, args);
65+
} catch (e) {
66+
console.error(`[${schema.componentName}]生命周期${method}出错`, e);
67+
}
68+
}
69+
3870
export default function baseRendererFactory(): IBaseRenderComponent {
3971
const { BaseRenderer: customBaseRenderer } = adapter.getRenderers();
4072

@@ -72,32 +104,33 @@ export default function baseRendererFactory(): IBaseRenderComponent {
72104

73105
static contextType = AppContext;
74106

75-
__namespace = 'base';
76-
77107
appHelper?: IRendererAppHelper;
108+
i18n: any;
109+
getLocale: any;
110+
setLocale: any;
111+
dataSourceMap: Record<string, any> = {};
112+
113+
__namespace = 'base';
78114
__compScopes: Record<string, any> = {};
79115
__instanceMap: Record<string, any> = {};
80116
__dataHelper: any;
81117
__customMethodsList: any[] = [];
82-
dataSourceMap: Record<string, any> = {};
118+
__parseExpression: any;
83119
__ref: any;
84-
i18n: any;
85-
getLocale: any;
86-
setLocale: any;
87120

88121
/**
89122
* reference of style element contains schema.css
90123
*
91124
* @type {any}
92125
*/
93-
styleElement: any;
94-
parseExpression: any;
126+
__styleElement: any;
127+
95128
[key: string]: any;
96129

97130
constructor(props: IBaseRendererProps, context: IBaseRendererContext) {
98131
super(props, context);
99132
this.context = context;
100-
this.parseExpression = props?.thisRequiredInJSE ? parseThisRequiredExpression : parseExpression;
133+
this.__parseExpression = props?.thisRequiredInJSE ? parseThisRequiredExpression : parseExpression;
101134
this.__beforeInit(props);
102135
this.__init(props);
103136
this.__afterInit(props);
@@ -119,21 +152,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
119152
__afterInit(_props: IBaseRendererProps) { }
120153

121154
static getDerivedStateFromProps(props: IBaseRendererProps, state: any) {
122-
logger.log('getDerivedStateFromProps');
123-
const func = props?.__schema?.lifeCycles?.getDerivedStateFromProps;
124-
125-
if (func) {
126-
if (isJSExpression(func) || isJSFunction(func)) {
127-
const fn = props.thisRequiredInJSE ? parseThisRequiredExpression(func, this) : parseExpression(func, this);
128-
return fn?.(props, state);
129-
}
130-
131-
if (typeof func === 'function') {
132-
// eslint-disable-next-line @typescript-eslint/ban-types
133-
return (func as Function)(props, state);
134-
}
135-
}
136-
return null;
155+
return excuteLifeCycleMethod(this, props?.__schema, 'getDerivedStateFromProps', [props, state], props.thisRequiredInJSE);
137156
}
138157

139158
async getSnapshotBeforeUpdate(...args: any[]) {
@@ -198,23 +217,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
198217
* @PRIVATE
199218
*/
200219
__excuteLifeCycleMethod = (method: string, args?: any) => {
201-
const lifeCycleMethods = getValue(this.props.__schema, 'lifeCycles', {});
202-
let fn = lifeCycleMethods[method];
203-
if (fn) {
204-
// TODO: cache
205-
if (isJSExpression(fn) || isJSFunction(fn)) {
206-
fn = this.parseExpression(fn, this);
207-
}
208-
if (typeof fn !== 'function') {
209-
console.error(`生命周期${method}类型不符`, fn);
210-
return;
211-
}
212-
try {
213-
return fn.apply(this, args);
214-
} catch (e) {
215-
console.error(`[${this.props.__schema.componentName}]生命周期${method}出错`, e);
216-
}
217-
}
220+
excuteLifeCycleMethod(this, this.props.__schema, method, args, this.props.thisRequiredInJSE);
218221
};
219222

220223
/**
@@ -242,7 +245,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
242245
forEach(__schema.methods, (val: any, key: string) => {
243246
let value = val;
244247
if (isJSExpression(value) || isJSFunction(value)) {
245-
value = this.parseExpression(value, this);
248+
value = this.__parseExpression(value, this);
246249
}
247250
if (typeof value !== 'function') {
248251
console.error(`自定义函数${key}类型不符`, value);
@@ -351,16 +354,16 @@ export default function baseRendererFactory(): IBaseRenderComponent {
351354
__writeCss = (props: IBaseRendererProps) => {
352355
const css = getValue(props.__schema, 'css', '');
353356
this.__debug('create this.styleElement with css', css);
354-
let style = this.styleElement;
355-
if (!this.styleElement) {
357+
let style = this.__styleElement;
358+
if (!this.__styleElement) {
356359
style = document.createElement('style');
357360
style.type = 'text/css';
358361
style.setAttribute('from', 'style-sheet');
359362

360363
const head = document.head || document.getElementsByTagName('head')[0];
361364
head.appendChild(style);
362-
this.styleElement = style;
363-
this.__debug('this.styleElement is created', this.styleElement);
365+
this.__styleElement = style;
366+
this.__debug('this.styleElement is created', this.__styleElement);
364367
}
365368

366369
if (style.innerHTML === css) {
@@ -459,7 +462,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
459462
const { __appHelper: appHelper, __components: components = {} } = this.props || {};
460463

461464
if (isJSExpression(schema)) {
462-
return this.parseExpression(schema, scope);
465+
return this.__parseExpression(schema, scope);
463466
}
464467
if (isI18nData(schema)) {
465468
return parseI18n(schema, scope);
@@ -486,7 +489,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
486489
const _children = this.__getSchemaChildren(schema);
487490
// 解析占位组件
488491
if (schema.componentName === 'Fragment' && _children) {
489-
const tarChildren = isJSExpression(_children) ? this.parseExpression(_children, scope) : _children;
492+
const tarChildren = isJSExpression(_children) ? this.__parseExpression(_children, scope) : _children;
490493
return this.__createVirtualDom(tarChildren, scope, parentInfo);
491494
}
492495

@@ -553,7 +556,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
553556
let scopeKey = '';
554557
// 判断组件是否需要生成scope,且只生成一次,挂在this.__compScopes上
555558
if (Comp.generateScope) {
556-
const key = this.parseExpression(schema.props?.key, scope);
559+
const key = this.__parseExpression(schema.props?.key, scope);
557560
if (key) {
558561
// 如果组件自己设置key则使用组件自己的key
559562
scopeKey = key;
@@ -704,7 +707,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
704707

705708
children.forEach((child: any) => {
706709
const childVirtualDom = this.__createVirtualDom(
707-
isJSExpression(child) ? this.parseExpression(child, scope) : child,
710+
isJSExpression(child) ? this.__parseExpression(child, scope) : child,
708711
scope,
709712
{
710713
schema,
@@ -806,7 +809,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
806809
};
807810

808811
if (isJSExpression(props)) {
809-
props = this.parseExpression(props, scope);
812+
props = this.__parseExpression(props, scope);
810813
// 只有当变量解析出来为模型结构的时候才会继续解析
811814
if (!isSchema(props) && !isJSSlot(props)) {
812815
return checkProps(props);
Collapse file

‎packages/renderer-core/src/renderer/page.tsx‎

Copy file name to clipboardExpand all lines: packages/renderer-core/src/renderer/page.tsx
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ export default function pageRendererFactory(): IBaseRenderComponent {
4040
this.__bindCustomMethods(this.props);
4141
this.__initDataSource(this.props);
4242

43-
// this.__excuteLifeCycleMethod('constructor', arguments);
44-
4543
this.__generateCtx({
4644
page: this,
4745
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.