@@ -35,6 +35,38 @@ import { IComponentConstruct, IComponentHoc, leafWrapper } from '../hoc/leaf'; |
35 | 35 | import logger from '../utils/logger'; |
36 | 36 | import isUseLoop from '../utils/is-use-loop'; |
37 | 37 |
|
| 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 | + |
38 | 70 | export default function baseRendererFactory(): IBaseRenderComponent { |
39 | 71 | const { BaseRenderer: customBaseRenderer } = adapter.getRenderers(); |
40 | 72 |
|
@@ -72,32 +104,33 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
72 | 104 |
|
73 | 105 | static contextType = AppContext; |
74 | 106 |
|
75 | | - __namespace = 'base'; |
76 | | - |
77 | 107 | appHelper?: IRendererAppHelper; |
| 108 | + i18n: any; |
| 109 | + getLocale: any; |
| 110 | + setLocale: any; |
| 111 | + dataSourceMap: Record<string, any> = {}; |
| 112 | + |
| 113 | + __namespace = 'base'; |
78 | 114 | __compScopes: Record<string, any> = {}; |
79 | 115 | __instanceMap: Record<string, any> = {}; |
80 | 116 | __dataHelper: any; |
81 | 117 | __customMethodsList: any[] = []; |
82 | | - dataSourceMap: Record<string, any> = {}; |
| 118 | + __parseExpression: any; |
83 | 119 | __ref: any; |
84 | | - i18n: any; |
85 | | - getLocale: any; |
86 | | - setLocale: any; |
87 | 120 |
|
88 | 121 | /** |
89 | 122 | * reference of style element contains schema.css |
90 | 123 | * |
91 | 124 | * @type {any} |
92 | 125 | */ |
93 | | - styleElement: any; |
94 | | - parseExpression: any; |
| 126 | + __styleElement: any; |
| 127 | + |
95 | 128 | [key: string]: any; |
96 | 129 |
|
97 | 130 | constructor(props: IBaseRendererProps, context: IBaseRendererContext) { |
98 | 131 | super(props, context); |
99 | 132 | this.context = context; |
100 | | - this.parseExpression = props?.thisRequiredInJSE ? parseThisRequiredExpression : parseExpression; |
| 133 | + this.__parseExpression = props?.thisRequiredInJSE ? parseThisRequiredExpression : parseExpression; |
101 | 134 | this.__beforeInit(props); |
102 | 135 | this.__init(props); |
103 | 136 | this.__afterInit(props); |
@@ -119,21 +152,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
119 | 152 | __afterInit(_props: IBaseRendererProps) { } |
120 | 153 |
|
121 | 154 | 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); |
137 | 156 | } |
138 | 157 |
|
139 | 158 | async getSnapshotBeforeUpdate(...args: any[]) { |
@@ -198,23 +217,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
198 | 217 | * @PRIVATE |
199 | 218 | */ |
200 | 219 | __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); |
218 | 221 | }; |
219 | 222 |
|
220 | 223 | /** |
@@ -242,7 +245,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
242 | 245 | forEach(__schema.methods, (val: any, key: string) => { |
243 | 246 | let value = val; |
244 | 247 | if (isJSExpression(value) || isJSFunction(value)) { |
245 | | - value = this.parseExpression(value, this); |
| 248 | + value = this.__parseExpression(value, this); |
246 | 249 | } |
247 | 250 | if (typeof value !== 'function') { |
248 | 251 | console.error(`自定义函数${key}类型不符`, value); |
@@ -351,16 +354,16 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
351 | 354 | __writeCss = (props: IBaseRendererProps) => { |
352 | 355 | const css = getValue(props.__schema, 'css', ''); |
353 | 356 | 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) { |
356 | 359 | style = document.createElement('style'); |
357 | 360 | style.type = 'text/css'; |
358 | 361 | style.setAttribute('from', 'style-sheet'); |
359 | 362 |
|
360 | 363 | const head = document.head || document.getElementsByTagName('head')[0]; |
361 | 364 | 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); |
364 | 367 | } |
365 | 368 |
|
366 | 369 | if (style.innerHTML === css) { |
@@ -459,7 +462,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
459 | 462 | const { __appHelper: appHelper, __components: components = {} } = this.props || {}; |
460 | 463 |
|
461 | 464 | if (isJSExpression(schema)) { |
462 | | - return this.parseExpression(schema, scope); |
| 465 | + return this.__parseExpression(schema, scope); |
463 | 466 | } |
464 | 467 | if (isI18nData(schema)) { |
465 | 468 | return parseI18n(schema, scope); |
@@ -486,7 +489,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
486 | 489 | const _children = this.__getSchemaChildren(schema); |
487 | 490 | // 解析占位组件 |
488 | 491 | 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; |
490 | 493 | return this.__createVirtualDom(tarChildren, scope, parentInfo); |
491 | 494 | } |
492 | 495 |
|
@@ -553,7 +556,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
553 | 556 | let scopeKey = ''; |
554 | 557 | // 判断组件是否需要生成scope,且只生成一次,挂在this.__compScopes上 |
555 | 558 | if (Comp.generateScope) { |
556 | | - const key = this.parseExpression(schema.props?.key, scope); |
| 559 | + const key = this.__parseExpression(schema.props?.key, scope); |
557 | 560 | if (key) { |
558 | 561 | // 如果组件自己设置key则使用组件自己的key |
559 | 562 | scopeKey = key; |
@@ -704,7 +707,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
704 | 707 |
|
705 | 708 | children.forEach((child: any) => { |
706 | 709 | const childVirtualDom = this.__createVirtualDom( |
707 | | - isJSExpression(child) ? this.parseExpression(child, scope) : child, |
| 710 | + isJSExpression(child) ? this.__parseExpression(child, scope) : child, |
708 | 711 | scope, |
709 | 712 | { |
710 | 713 | schema, |
@@ -806,7 +809,7 @@ export default function baseRendererFactory(): IBaseRenderComponent { |
806 | 809 | }; |
807 | 810 |
|
808 | 811 | if (isJSExpression(props)) { |
809 | | - props = this.parseExpression(props, scope); |
| 812 | + props = this.__parseExpression(props, scope); |
810 | 813 | // 只有当变量解析出来为模型结构的时候才会继续解析 |
811 | 814 | if (!isSchema(props) && !isJSSlot(props)) { |
812 | 815 | return checkProps(props); |
|