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