Skip to content

Commit

Permalink
fix(data-source): 数据源初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Dec 1, 2023
1 parent 7f9c4f3 commit 78762b9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
62 changes: 31 additions & 31 deletions packages/data-source/src/DataSourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -117,8 +115,6 @@ class DataSourceManager extends EventEmitter {
ds.on('change', () => {
this.setData(ds);
});

this.init();
}

public setData(ds: DataSource) {
Expand All @@ -134,14 +130,18 @@ 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;
}

this.removeDataSource(schema.id);

this.addDataSource(schema);
const newDs = this.get(schema.id);
if (newDs) {
this.init(newDs);
}
});
}

Expand Down Expand Up @@ -239,6 +239,6 @@ class DataSourceManager extends EventEmitter {
}
}

DataSourceManager.registe('http', HttpDataSource);
DataSourceManager.registe('http', HttpDataSource as any);

export default DataSourceManager;
16 changes: 12 additions & 4 deletions packages/data-source/src/data-sources/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
*/
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';

/**
* 静态数据源
*/
export default class DataSource extends EventEmitter {
export default class DataSource<T extends DataSourceSchema = DataSourceSchema> extends EventEmitter {
public isInit = false;

public data: Record<string, any> = {};
Expand All @@ -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 || []);

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/data-source/src/data-sources/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -91,7 +90,6 @@ export default class HttpDataSource extends DataSource {

super(options);

this.schema = options.schema;
this.httpOptions = httpOptions;

if (typeof options.request === 'function') {
Expand Down
3 changes: 1 addition & 2 deletions packages/data-source/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand All @@ -12,7 +12,6 @@ export interface DataSourceOptions<T = DataSourceSchema> {
}

export interface HttpDataSourceSchema extends DataSourceSchema {
type: 'http';
options: HttpOptions;
responseOptions?: {
dataPath?: string;
Expand Down

0 comments on commit 78762b9

Please sign in to comment.