fix(data-source): 编辑器的runtime不能对数据源的mock配置有影响

This commit is contained in:
roymondchen 2024-08-06 14:33:21 +08:00
parent 69f3cb2f12
commit fa2d00ba36
2 changed files with 11 additions and 16 deletions

View File

@ -111,28 +111,21 @@ class DataSourceManager extends EventEmitter {
return;
}
const beforeInit: ((...args: any[]) => any)[] = [];
const afterInit: ((...args: any[]) => any)[] = [];
ds.methods.forEach((method) => {
for (const method of ds.methods) {
if (typeof method.content !== 'function') return;
if (method.timing === 'beforeInit') {
beforeInit.push(method.content);
method.content({ params: {}, dataSource: ds, app: this.app });
}
if (method.timing === 'afterInit') {
afterInit.push(method.content);
}
});
for (const method of beforeInit) {
await method({ params: {}, dataSource: ds, app: this.app });
}
await ds.init();
for (const method of afterInit) {
await method({ params: {}, dataSource: ds, app: this.app });
}
ds.methods.forEach((method) => {
if (typeof method.content !== 'function') return;
if (method.timing === 'afterInit') {
method.content({ params: {}, dataSource: ds, app: this.app });
}
});
}
public get(id: string) {

View File

@ -17,6 +17,8 @@
*/
import EventEmitter from 'events';
import { cloneDeep } from 'lodash-es';
import type { AppCore, CodeBlockContent, DataSchema, DataSourceSchema } from '@tmagic/schema';
import { getDefaultValueFromFields } from '@tmagic/utils';
@ -62,7 +64,7 @@ export default class DataSource<T extends DataSourceSchema = DataSourceSchema> e
if (this.app.platform === 'editor') {
// 编辑器中有mock使用mock没有使用默认值
this.mockData = options.schema.mocks?.find((mock) => mock.useInEditor)?.data || this.getDefaultData();
data = this.mockData;
data = cloneDeep(this.mockData);
} else if (typeof options.useMock === 'boolean' && options.useMock) {
// 设置了使用mock就使用mock数据
this.mockData = options.schema.mocks?.find((mock) => mock.enable)?.data || this.getDefaultData();