Skip to content

Commit 035bde4

Browse files
JackLianliujuping
authored andcommitted
refactor: remove useless logic, and some minor refactors
1 parent 81a7304 commit 035bde4

1 file changed

Lines changed: 32 additions & 32 deletions

File tree

  • packages/renderer-core/src/renderer

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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export default function baseRendererFactory(): IBaseRenderComponent {
7777
__compScopes: Record<string, any> = {};
7878
__instanceMap: Record<string, any> = {};
7979
__dataHelper: any;
80-
__showPlaceholder: boolean = false;
8180
__customMethodsList: any[] = [];
8281
dataSourceMap: Record<string, any> = {};
8382
__ref: any;
@@ -106,7 +105,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
106105
this.__compScopes = {};
107106
this.__instanceMap = {};
108107
this.__bindCustomMethods(props);
109-
this.__initI18nAPIs();
108+
this.__initI18nAPIs(props);
110109
}
111110

112111
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -159,23 +158,17 @@ export default function baseRendererFactory(): IBaseRenderComponent {
159158
reloadDataSource = () => new Promise((resolve, reject) => {
160159
this.__debug('reload data source');
161160
if (!this.__dataHelper) {
162-
this.__showPlaceholder = false;
163161
return resolve({});
164162
}
165163
this.__dataHelper.getInitData()
166164
.then((res: any) => {
167-
this.__showPlaceholder = false;
168165
if (isEmpty(res)) {
169166
this.forceUpdate();
170167
return resolve({});
171168
}
172169
this.setState(res, resolve as () => void);
173170
})
174171
.catch((err: Error) => {
175-
if (this.__showPlaceholder) {
176-
this.__showPlaceholder = false;
177-
this.forceUpdate();
178-
}
179172
reject(err);
180173
});
181174
});
@@ -193,12 +186,15 @@ export default function baseRendererFactory(): IBaseRenderComponent {
193186
super.forceUpdate();
194187
}
195188
}
196-
189+
/**
190+
* execute method in schema.lifeCycles
191+
* @PRIVATE
192+
*/
197193
__excuteLifeCycleMethod = (method: string, args?: any) => {
198194
const lifeCycleMethods = getValue(this.props.__schema, 'lifeCycles', {});
199195
let fn = lifeCycleMethods[method];
200196
if (fn) {
201-
// TODO, cache
197+
// TODO: cache
202198
if (isJSExpression(fn) || isJSFunction(fn)) {
203199
fn = this.parseExpression(fn, this);
204200
}
@@ -214,6 +210,10 @@ export default function baseRendererFactory(): IBaseRenderComponent {
214210
}
215211
};
216212

213+
/**
214+
* this method is for legacy purpose only, which used _ prefix instead of __ as private for some historical reasons
215+
* @LEGACY
216+
*/
217217
_getComponentView = (componentName: string) => {
218218
const { __components } = this.props;
219219
if (!__components) {
@@ -262,14 +262,18 @@ export default function baseRendererFactory(): IBaseRenderComponent {
262262
return parseData(data, ctx || __ctx || this, { thisRequiredInJSE });
263263
};
264264

265-
__initDataSource = (props = this.props) => {
265+
__initDataSource = (props: IBaseRendererProps) => {
266+
if (!props) {
267+
return;
268+
}
266269
const schema = props.__schema || {};
267270
const defaultDataSource: DataSource = {
268271
list: [],
269272
};
270-
const dataSource = (schema && schema?.dataSource) || defaultDataSource;
273+
const dataSource = schema.dataSource || defaultDataSource;
271274
// requestHandlersMap 存在才走数据源引擎方案
272-
if (props?.__appHelper?.requestHandlersMap) {
275+
const useDataSourceEngine = !!(props.__appHelper?.requestHandlersMap);
276+
if (useDataSourceEngine) {
273277
this.__dataHelper = {
274278
updateConfig: (updateDataSource: any) => {
275279
const { dataSourceMap, reloadDataSource } = createDataSourceEngine(
@@ -280,11 +284,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
280284

281285
this.reloadDataSource = () => new Promise((resolve) => {
282286
this.__debug('reload data source');
283-
// this.__showPlaceholder = true;
284287
reloadDataSource().then(() => {
285-
// this.__showPlaceholder = false;
286-
// @TODO 是否需要 forceUpate
287-
// this.forceUpdate();
288288
resolve({});
289289
});
290290
});
@@ -299,41 +299,41 @@ export default function baseRendererFactory(): IBaseRenderComponent {
299299
this.reloadDataSource = () => new Promise((resolve, reject) => {
300300
this.__debug('reload data source');
301301
if (!this.__dataHelper) {
302-
// this.__showPlaceholder = false;
303302
return resolve({});
304303
}
305304
this.__dataHelper.getInitData()
306305
.then((res: any) => {
307-
// this.__showPlaceholder = false;
308306
if (isEmpty(res)) {
309307
this.forceUpdate();
310308
return resolve({});
311309
}
312310
this.setState(res, resolve as () => void);
313311
})
314312
.catch((err: Error) => {
315-
if (this.__showPlaceholder) {
316-
this.__showPlaceholder = false;
317-
this.forceUpdate();
318-
}
319313
reject(err);
320314
});
321315
});
322316
}
323-
// 设置容器组件占位,若设置占位则在初始异步请求完成之前用loading占位且不渲染容器组件内部内容
324-
// @TODO __showPlaceholder 的逻辑一旦开启就关不掉,先注释掉了
325-
/* this.__showPlaceholder = this.__parseData(schema.props && schema.props.autoLoading) && (dataSource.list || []).some(
326-
(item) => !!this.__parseData(item.isInit),
327-
); */
328317
};
329318

330-
__initI18nAPIs = () => {
319+
/**
320+
* init i18n apis
321+
* @PRIVATE
322+
*/
323+
__initI18nAPIs = (props: IBaseRendererProps) => {
331324
this.i18n = (key: string, values = {}) => {
332-
const { locale, messages } = this.props;
325+
const { locale, messages } = props;
333326
return getI18n(key, values, locale, messages);
334327
};
335-
this.getLocale = () => this.props.locale;
336-
this.setLocale = (loc: string) => this.appHelper?.utils?.i18n?.setLocale && this.appHelper?.utils?.i18n?.setLocale(loc);
328+
this.getLocale = () => props.locale;
329+
this.setLocale = (loc: string) => {
330+
const setLocaleFn = this.appHelper?.utils?.i18n?.setLocale;
331+
if (!setLocaleFn || typeof setLocaleFn !== 'function') {
332+
console.warn('initI18nAPIs Failed, i18n only works when appHelper.utils.i18n.setLocale() exists');
333+
return undefined;
334+
}
335+
return setLocaleFn(loc);
336+
};
337337
};
338338

339339
__writeCss = () => {

0 commit comments

Comments
 (0)