mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-06-14 17:06:03 +08:00
fix(data-source): 数据源初始化
This commit is contained in:
parent
7f9c4f3855
commit
78762b920e
@ -29,7 +29,7 @@ import type { DataSourceManagerData, DataSourceManagerOptions } from './types';
|
|||||||
class DataSourceManager extends EventEmitter {
|
class DataSourceManager extends EventEmitter {
|
||||||
private static dataSourceClassMap = new Map<string, typeof DataSource>();
|
private static dataSourceClassMap = new Map<string, typeof DataSource>();
|
||||||
|
|
||||||
public static registe(type: string, dataSource: typeof DataSource) {
|
public static registe<T extends typeof DataSource = typeof DataSource>(type: string, dataSource: T) {
|
||||||
DataSourceManager.dataSourceClassMap.set(type, dataSource);
|
DataSourceManager.dataSourceClassMap.set(type, dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,39 +57,37 @@ class DataSourceManager extends EventEmitter {
|
|||||||
app.dsl?.dataSources?.forEach((config) => {
|
app.dsl?.dataSources?.forEach((config) => {
|
||||||
this.addDataSource(config);
|
this.addDataSource(config);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Promise.all(Array.from(this.dataSourceMap).map(async ([, ds]) => this.init(ds)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async init() {
|
public async init(ds: DataSource) {
|
||||||
await Promise.all(
|
if (ds.isInit) {
|
||||||
Array.from(this.dataSourceMap).map(async ([, ds]) => {
|
return;
|
||||||
if (ds.isInit) {
|
}
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const beforeInit: ((...args: any[]) => any)[] = [];
|
const beforeInit: ((...args: any[]) => any)[] = [];
|
||||||
const afterInit: ((...args: any[]) => any)[] = [];
|
const afterInit: ((...args: any[]) => any)[] = [];
|
||||||
|
|
||||||
ds.methods.forEach((method) => {
|
ds.methods.forEach((method) => {
|
||||||
if (typeof method.content !== 'function') return;
|
if (typeof method.content !== 'function') return;
|
||||||
if (method.timing === 'beforeInit') {
|
if (method.timing === 'beforeInit') {
|
||||||
beforeInit.push(method.content);
|
beforeInit.push(method.content);
|
||||||
}
|
}
|
||||||
if (method.timing === 'afterInit') {
|
if (method.timing === 'afterInit') {
|
||||||
afterInit.push(method.content);
|
afterInit.push(method.content);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const method of beforeInit) {
|
for (const method of beforeInit) {
|
||||||
await method({ params: {}, dataSource: ds, app: this.app });
|
await method({ params: {}, dataSource: ds, app: this.app });
|
||||||
}
|
}
|
||||||
|
|
||||||
await ds.init();
|
await ds.init();
|
||||||
|
|
||||||
for (const method of afterInit) {
|
for (const method of afterInit) {
|
||||||
await method({ params: {}, dataSource: ds, app: this.app });
|
await method({ params: {}, dataSource: ds, app: this.app });
|
||||||
}
|
}
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public get(id: string) {
|
public get(id: string) {
|
||||||
@ -117,8 +115,6 @@ class DataSourceManager extends EventEmitter {
|
|||||||
ds.on('change', () => {
|
ds.on('change', () => {
|
||||||
this.setData(ds);
|
this.setData(ds);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public setData(ds: DataSource) {
|
public setData(ds: DataSource) {
|
||||||
@ -134,7 +130,7 @@ class DataSourceManager extends EventEmitter {
|
|||||||
|
|
||||||
public updateSchema(schemas: DataSourceSchema[]) {
|
public updateSchema(schemas: DataSourceSchema[]) {
|
||||||
schemas.forEach((schema) => {
|
schemas.forEach((schema) => {
|
||||||
const ds = this.dataSourceMap.get(schema.id);
|
const ds = this.get(schema.id);
|
||||||
if (!ds) {
|
if (!ds) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -142,6 +138,10 @@ class DataSourceManager extends EventEmitter {
|
|||||||
this.removeDataSource(schema.id);
|
this.removeDataSource(schema.id);
|
||||||
|
|
||||||
this.addDataSource(schema);
|
this.addDataSource(schema);
|
||||||
|
const newDs = this.get(schema.id);
|
||||||
|
if (newDs) {
|
||||||
|
this.init(newDs);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,6 +239,6 @@ class DataSourceManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DataSourceManager.registe('http', HttpDataSource);
|
DataSourceManager.registe('http', HttpDataSource as any);
|
||||||
|
|
||||||
export default DataSourceManager;
|
export default DataSourceManager;
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
|
|
||||||
import type { AppCore, CodeBlockContent, DataSchema } from '@tmagic/schema';
|
import type { AppCore, CodeBlockContent, DataSchema, DataSourceSchema } from '@tmagic/schema';
|
||||||
import { getDefaultValueFromFields } from '@tmagic/utils';
|
import { getDefaultValueFromFields } from '@tmagic/utils';
|
||||||
|
|
||||||
import type { DataSourceOptions } from '@data-source/types';
|
import type { DataSourceOptions } from '@data-source/types';
|
||||||
@ -25,7 +25,7 @@ import type { DataSourceOptions } from '@data-source/types';
|
|||||||
/**
|
/**
|
||||||
* 静态数据源
|
* 静态数据源
|
||||||
*/
|
*/
|
||||||
export default class DataSource extends EventEmitter {
|
export default class DataSource<T extends DataSourceSchema = DataSourceSchema> extends EventEmitter {
|
||||||
public isInit = false;
|
public isInit = false;
|
||||||
|
|
||||||
public data: Record<string, any> = {};
|
public data: Record<string, any> = {};
|
||||||
@ -37,17 +37,21 @@ export default class DataSource extends EventEmitter {
|
|||||||
|
|
||||||
#type = 'base';
|
#type = 'base';
|
||||||
#id: string;
|
#id: string;
|
||||||
|
#schema: T;
|
||||||
|
|
||||||
/** 数据源自定义字段配置 */
|
/** 数据源自定义字段配置 */
|
||||||
#fields: DataSchema[] = [];
|
#fields: DataSchema[] = [];
|
||||||
/** 数据源自定义方法配置 */
|
/** 数据源自定义方法配置 */
|
||||||
#methods: CodeBlockContent[] = [];
|
#methods: CodeBlockContent[] = [];
|
||||||
|
|
||||||
constructor(options: DataSourceOptions) {
|
constructor(options: DataSourceOptions<T>) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.app = options.app;
|
|
||||||
this.#id = options.schema.id;
|
this.#id = options.schema.id;
|
||||||
|
this.#schema = options.schema;
|
||||||
|
|
||||||
|
this.app = options.app;
|
||||||
|
|
||||||
this.setFields(options.schema.fields);
|
this.setFields(options.schema.fields);
|
||||||
this.setMethods(options.schema.methods || []);
|
this.setMethods(options.schema.methods || []);
|
||||||
|
|
||||||
@ -77,6 +81,10 @@ export default class DataSource extends EventEmitter {
|
|||||||
return this.#type;
|
return this.#type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get schema() {
|
||||||
|
return this.#schema;
|
||||||
|
}
|
||||||
|
|
||||||
public get fields() {
|
public get fields() {
|
||||||
return this.#fields;
|
return this.#fields;
|
||||||
}
|
}
|
||||||
|
@ -66,14 +66,13 @@ const webRequest = async (options: HttpOptions) => {
|
|||||||
* Http 数据源
|
* Http 数据源
|
||||||
* @description 通过 http 请求获取数据
|
* @description 通过 http 请求获取数据
|
||||||
*/
|
*/
|
||||||
export default class HttpDataSource extends DataSource {
|
export default class HttpDataSource extends DataSource<HttpDataSourceSchema> {
|
||||||
/** 是否正在发起请求 */
|
/** 是否正在发起请求 */
|
||||||
public isLoading = false;
|
public isLoading = false;
|
||||||
public error?: {
|
public error?: {
|
||||||
msg?: string;
|
msg?: string;
|
||||||
code?: string | number;
|
code?: string | number;
|
||||||
};
|
};
|
||||||
public schema: HttpDataSourceSchema;
|
|
||||||
/** 请求配置 */
|
/** 请求配置 */
|
||||||
public httpOptions: HttpOptions;
|
public httpOptions: HttpOptions;
|
||||||
|
|
||||||
@ -91,7 +90,6 @@ export default class HttpDataSource extends DataSource {
|
|||||||
|
|
||||||
super(options);
|
super(options);
|
||||||
|
|
||||||
this.schema = options.schema;
|
|
||||||
this.httpOptions = httpOptions;
|
this.httpOptions = httpOptions;
|
||||||
|
|
||||||
if (typeof options.request === 'function') {
|
if (typeof options.request === 'function') {
|
||||||
|
@ -2,7 +2,7 @@ import type { AppCore, DataSourceSchema, HttpOptions, RequestFunction } from '@t
|
|||||||
|
|
||||||
import HttpDataSource from './data-sources/Http';
|
import HttpDataSource from './data-sources/Http';
|
||||||
|
|
||||||
export interface DataSourceOptions<T = DataSourceSchema> {
|
export interface DataSourceOptions<T extends DataSourceSchema = DataSourceSchema> {
|
||||||
schema: T;
|
schema: T;
|
||||||
app: AppCore;
|
app: AppCore;
|
||||||
initialData?: Record<string, any>;
|
initialData?: Record<string, any>;
|
||||||
@ -12,7 +12,6 @@ export interface DataSourceOptions<T = DataSourceSchema> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface HttpDataSourceSchema extends DataSourceSchema {
|
export interface HttpDataSourceSchema extends DataSourceSchema {
|
||||||
type: 'http';
|
|
||||||
options: HttpOptions;
|
options: HttpOptions;
|
||||||
responseOptions?: {
|
responseOptions?: {
|
||||||
dataPath?: string;
|
dataPath?: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user