feat(data-source,utils,runtime): 数据源setData支持指定路径
This commit is contained in:
@@ -24,7 +24,7 @@ import type { AppCore, DataSourceSchema, Id, MNode } from '@tmagic/schema';
|
||||
import { compiledCond, compiledNode, DATA_SOURCE_FIELDS_SELECT_VALUE_PREFIX, isObject } from '@tmagic/utils';
|
||||
|
||||
import { DataSource, HttpDataSource } from './data-sources';
|
||||
import type { DataSourceManagerData, DataSourceManagerOptions } from './types';
|
||||
import type { ChangeEvent, DataSourceManagerData, DataSourceManagerOptions } from './types';
|
||||
|
||||
class DataSourceManager extends EventEmitter {
|
||||
private static dataSourceClassMap = new Map<string, typeof DataSource>();
|
||||
@@ -123,14 +123,14 @@ class DataSourceManager extends EventEmitter {
|
||||
|
||||
this.data[ds.id] = ds.data;
|
||||
|
||||
ds.on('change', () => {
|
||||
this.setData(ds);
|
||||
ds.on('change', (changeEvent: ChangeEvent) => {
|
||||
this.setData(ds, changeEvent);
|
||||
});
|
||||
}
|
||||
|
||||
public setData(ds: DataSource) {
|
||||
Object.assign(this.data[ds.id], ds.data);
|
||||
this.emit('change', ds.id);
|
||||
public setData(ds: DataSource, changeEvent: ChangeEvent) {
|
||||
this.data[ds.id] = ds.data;
|
||||
this.emit('change', ds.id, changeEvent);
|
||||
}
|
||||
|
||||
public removeDataSource(id: string) {
|
||||
|
||||
@@ -21,7 +21,7 @@ import type { AppCore } from '@tmagic/schema';
|
||||
import { getDepNodeIds, getNodes, replaceChildNode } from '@tmagic/utils';
|
||||
|
||||
import DataSourceManager from './DataSourceManager';
|
||||
import { DataSourceManagerData } from './types';
|
||||
import type { ChangeEvent, DataSourceManagerData } from './types';
|
||||
|
||||
/**
|
||||
* 创建数据源管理器
|
||||
@@ -52,7 +52,7 @@ export const createDataSourceManager = (app: AppCore, useMock?: boolean, initial
|
||||
// ssr环境下,数据应该是提前准备好的(放到initialData中),不应该发生变化,无需监听
|
||||
// 有initialData不一定是在ssr环境下
|
||||
if (app.jsEngine !== 'nodejs') {
|
||||
dataSourceManager.on('change', (sourceId: string) => {
|
||||
dataSourceManager.on('change', (sourceId: string, changeEvent: ChangeEvent) => {
|
||||
const dep = dsl.dataSourceDeps?.[sourceId] || {};
|
||||
const condDep = dsl.dataSourceCondDeps?.[sourceId] || {};
|
||||
|
||||
@@ -66,6 +66,7 @@ export const createDataSourceManager = (app: AppCore, useMock?: boolean, initial
|
||||
return dataSourceManager.compiledNode(newNode);
|
||||
}),
|
||||
sourceId,
|
||||
changeEvent,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
import EventEmitter from 'events';
|
||||
|
||||
import type { AppCore, CodeBlockContent, DataSchema, DataSourceSchema } from '@tmagic/schema';
|
||||
import { getDefaultValueFromFields } from '@tmagic/utils';
|
||||
import { getDefaultValueFromFields, setValueByKeyPath } from '@tmagic/utils';
|
||||
|
||||
import type { DataSourceOptions } from '@data-source/types';
|
||||
import type { ChangeEvent, DataSourceOptions } from '@data-source/types';
|
||||
|
||||
/**
|
||||
* 静态数据源
|
||||
@@ -101,10 +101,20 @@ export default class DataSource<T extends DataSourceSchema = DataSourceSchema> e
|
||||
this.#methods = methods;
|
||||
}
|
||||
|
||||
public setData(data: Record<string, any>) {
|
||||
// todo: 校验数据,看是否符合 schema
|
||||
this.data = data;
|
||||
this.emit('change');
|
||||
public setData(data: any, path?: string) {
|
||||
if (path) {
|
||||
setValueByKeyPath(path, data, this.data);
|
||||
} else {
|
||||
// todo: 校验数据,看是否符合 schema
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
const changeEvent: ChangeEvent = {
|
||||
updateData: data,
|
||||
path,
|
||||
};
|
||||
|
||||
this.emit('change', changeEvent);
|
||||
}
|
||||
|
||||
public getDefaultData() {
|
||||
|
||||
@@ -37,3 +37,8 @@ export interface DataSourceManagerOptions {
|
||||
export interface DataSourceManagerData {
|
||||
[key: string]: Record<string, any>;
|
||||
}
|
||||
|
||||
export interface ChangeEvent {
|
||||
path?: string;
|
||||
updateData: any;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user