fix(core): deep-state-observer update 的data与初始化时相同,导致get data为空

This commit is contained in:
roymondchen 2025-01-20 14:58:20 +08:00
parent e8461f91e6
commit 701a9d6f30
3 changed files with 8 additions and 5 deletions

View File

@ -1,8 +1,9 @@
import { describe, expect, test } from 'vitest';
import { MApp, NodeType, TMagicIteratorContainer } from '@tmagic/schema';
import { MApp, NodeType } from '@tmagic/schema';
import App from '../src/App';
import TMagicIteratorContainer from '../src/IteratorContainer';
const createAppDsl = (pageLength: number, nodeLength = 0) => {
const dsl: MApp = {

View File

@ -177,7 +177,7 @@ class DataSourceManager extends EventEmitter {
this.removeDataSource(schema.id);
this.addDataSource(schema);
this.addDataSource(cloneDeep(schema));
const newDs = this.get(schema.id);
if (newDs) {
this.init(newDs);

View File

@ -62,13 +62,15 @@ export default class DataSource<T extends DataSourceSchema = DataSourceSchema> e
// eslint-disable-next-line @typescript-eslint/naming-convention
const ObservedDataClass = options.ObservedDataClass || SimpleObservedData;
if (this.app.platform === 'editor') {
const mocks = cloneDeep(options.schema.mocks || []);
// 编辑器中有mock使用mock没有使用默认值
this.mockData = options.schema.mocks?.find((mock) => mock.useInEditor)?.data || this.getDefaultData();
this.mockData = mocks.find((mock) => mock.useInEditor)?.data || this.getDefaultData();
data = cloneDeep(this.mockData);
} else if (typeof options.useMock === 'boolean' && options.useMock) {
const mocks = cloneDeep(options.schema.mocks || []);
// 设置了使用mock就使用mock数据
this.mockData = options.schema.mocks?.find((mock) => mock.enable)?.data;
data = this.mockData || this.getDefaultData();
this.mockData = mocks.find((mock) => mock.enable)?.data;
data = cloneDeep(this.mockData) || this.getDefaultData();
} else if (!options.initialData) {
data = this.getDefaultData();
} else {