mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2025-06-01 21:29:20 +08:00
chore(core,data-source): 将app传入datasourceManager
This commit is contained in:
parent
8c93d9a5be
commit
7f48b4d9f5
@ -67,6 +67,7 @@ class App extends EventEmitter {
|
||||
public platform = 'mobile';
|
||||
public jsEngine = 'browser';
|
||||
public designWidth = 375;
|
||||
public request?: RequestFunction;
|
||||
|
||||
public components = new Map();
|
||||
|
||||
@ -91,8 +92,12 @@ class App extends EventEmitter {
|
||||
this.transformStyle = options.transformStyle;
|
||||
}
|
||||
|
||||
if (options.request) {
|
||||
this.request = options.request;
|
||||
}
|
||||
|
||||
if (options.config) {
|
||||
this.setConfig(options.config, options.curPage, options.request);
|
||||
this.setConfig(options.config, options.curPage);
|
||||
}
|
||||
|
||||
bindCommonEventListener(this);
|
||||
@ -156,7 +161,7 @@ class App extends EventEmitter {
|
||||
* @param config dsl跟节点
|
||||
* @param curPage 当前页面id
|
||||
*/
|
||||
public setConfig(config: MApp, curPage?: Id, request?: RequestFunction) {
|
||||
public setConfig(config: MApp, curPage?: Id) {
|
||||
this.dsl = config;
|
||||
|
||||
if (!curPage && config.items.length) {
|
||||
@ -167,9 +172,7 @@ class App extends EventEmitter {
|
||||
this.dataSourceManager.destroy();
|
||||
}
|
||||
|
||||
this.dataSourceManager = createDataSourceManager(config, this.platform, {
|
||||
request,
|
||||
});
|
||||
this.dataSourceManager = createDataSourceManager(this);
|
||||
|
||||
this.codeDsl = config.codeBlocks;
|
||||
this.setPage(curPage || this.page?.data?.id);
|
||||
|
@ -20,11 +20,11 @@ import EventEmitter from 'events';
|
||||
|
||||
import { cloneDeep, template } from 'lodash-es';
|
||||
|
||||
import { DataSourceDeps, DataSourceSchema, Id, MNode } from '@tmagic/schema';
|
||||
import type { DataSourceSchema, Id, MNode } from '@tmagic/schema';
|
||||
import { compiledCond, compiledNode } from '@tmagic/utils';
|
||||
|
||||
import { DataSource, HttpDataSource } from './data-sources';
|
||||
import type { DataSourceManagerData, DataSourceManagerOptions, HttpDataSourceSchema, RequestFunction } from './types';
|
||||
import type { DataSourceManagerData, DataSourceManagerOptions, HttpDataSourceSchema } from './types';
|
||||
|
||||
class DataSourceManager extends EventEmitter {
|
||||
private static dataSourceClassMap = new Map<string, typeof DataSource>();
|
||||
@ -33,25 +33,22 @@ class DataSourceManager extends EventEmitter {
|
||||
DataSourceManager.dataSourceClassMap.set(type, dataSource);
|
||||
}
|
||||
|
||||
public static getDataSourceClass(type: string) {
|
||||
return DataSourceManager.dataSourceClassMap.get(type);
|
||||
}
|
||||
|
||||
public app: any;
|
||||
|
||||
public dataSourceMap = new Map<string, DataSource>();
|
||||
|
||||
public data: DataSourceManagerData = {};
|
||||
public dataSourceDeps: DataSourceDeps = {};
|
||||
public dataSourceCondDeps: DataSourceDeps = {};
|
||||
|
||||
private request?: RequestFunction;
|
||||
|
||||
constructor(options: DataSourceManagerOptions) {
|
||||
constructor({ app }: DataSourceManagerOptions) {
|
||||
super();
|
||||
|
||||
this.dataSourceDeps = options.dataSourceDeps || {};
|
||||
this.dataSourceCondDeps = options.dataSourceCondDeps || {};
|
||||
this.app = app;
|
||||
|
||||
if (options.httpDataSourceOptions?.request) {
|
||||
this.request = options.httpDataSourceOptions.request;
|
||||
}
|
||||
|
||||
options.dataSourceConfigs.forEach((config) => {
|
||||
app.dsl?.dataSources?.forEach((config: any) => {
|
||||
this.addDataSource(config);
|
||||
});
|
||||
}
|
||||
@ -60,14 +57,14 @@ class DataSourceManager extends EventEmitter {
|
||||
return this.dataSourceMap.get(id);
|
||||
}
|
||||
|
||||
public addDataSource(config?: DataSourceSchema) {
|
||||
public async addDataSource(config?: DataSourceSchema) {
|
||||
if (!config) return;
|
||||
|
||||
let ds: DataSource;
|
||||
if (config.type === 'http') {
|
||||
ds = new HttpDataSource({
|
||||
schema: config as HttpDataSourceSchema,
|
||||
request: this.request,
|
||||
request: this.app.request,
|
||||
});
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
@ -82,36 +79,34 @@ class DataSourceManager extends EventEmitter {
|
||||
|
||||
this.data[ds.id] = ds.data;
|
||||
|
||||
const beforeInit: ((...args: any[]) => any)[] = [];
|
||||
const afterInit: ((...args: any[]) => any)[] = [];
|
||||
|
||||
ds.getMethods().forEach((method) => {
|
||||
if (typeof method.content !== 'function') return;
|
||||
if (method.timing === 'beforeInit') {
|
||||
if (typeof method.content === 'function') {
|
||||
method.content({ params: {}, dataSource: ds });
|
||||
}
|
||||
beforeInit.push(method.content);
|
||||
}
|
||||
if (method.timing === 'afterInit') {
|
||||
afterInit.push(method.content);
|
||||
}
|
||||
});
|
||||
|
||||
ds.init().then(() => {
|
||||
this.data[ds.id] = ds.data;
|
||||
await Promise.all(beforeInit.map((method) => method({ params: {}, dataSource: ds, app: this.app })));
|
||||
|
||||
ds.getMethods().forEach((method) => {
|
||||
if (method.timing === 'afterInit') {
|
||||
if (typeof method.content === 'function') {
|
||||
method.content({ params: {}, dataSource: ds });
|
||||
}
|
||||
}
|
||||
});
|
||||
await ds.init();
|
||||
|
||||
this.change(ds);
|
||||
});
|
||||
await Promise.all(afterInit.map((method) => method({ params: {}, dataSource: ds, app: this.app })));
|
||||
|
||||
this.setData(ds);
|
||||
|
||||
ds.on('change', () => {
|
||||
this.change(ds);
|
||||
this.setData(ds);
|
||||
});
|
||||
}
|
||||
|
||||
public change(ds: DataSource) {
|
||||
public setData(ds: DataSource) {
|
||||
Object.assign(this.data[ds.id], ds.data);
|
||||
|
||||
this.emit('change', ds.id);
|
||||
}
|
||||
|
||||
@ -148,7 +143,7 @@ class DataSourceManager extends EventEmitter {
|
||||
return value;
|
||||
},
|
||||
cloneDeep(node),
|
||||
this.dataSourceDeps,
|
||||
this.app.dsl?.dataSourceDeps || {},
|
||||
sourceId,
|
||||
);
|
||||
}
|
||||
|
@ -17,11 +17,9 @@
|
||||
*/
|
||||
import { cloneDeep } from 'lodash-es';
|
||||
|
||||
import type { MApp } from '@tmagic/schema';
|
||||
import { getDepNodeIds, getNodes, replaceChildNode } from '@tmagic/utils';
|
||||
|
||||
import DataSourceManager from './DataSourceManager';
|
||||
import type { HttpDataSourceOptions } from './types';
|
||||
|
||||
/**
|
||||
* 创建数据源管理器
|
||||
@ -29,19 +27,11 @@ import type { HttpDataSourceOptions } from './types';
|
||||
* @param httpDataSourceOptions http 数据源配置
|
||||
* @returns DataSourceManager
|
||||
*/
|
||||
export const createDataSourceManager = (
|
||||
dsl: MApp,
|
||||
platform: string,
|
||||
httpDataSourceOptions?: Partial<HttpDataSourceOptions>,
|
||||
) => {
|
||||
export const createDataSourceManager = (app: any) => {
|
||||
const { dsl, platform } = app;
|
||||
if (!dsl?.dataSources) return;
|
||||
|
||||
const dataSourceManager = new DataSourceManager({
|
||||
dataSourceConfigs: dsl.dataSources,
|
||||
dataSourceDeps: dsl.dataSourceDeps,
|
||||
dataSourceCondDeps: dsl.dataSourceCondDeps,
|
||||
httpDataSourceOptions,
|
||||
});
|
||||
const dataSourceManager = new DataSourceManager({ app });
|
||||
|
||||
if (dsl.dataSources && dsl.dataSourceCondDeps && platform !== 'editor') {
|
||||
getNodes(getDepNodeIds(dsl.dataSourceCondDeps), dsl.items).forEach((node) => {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { DataSourceDeps, DataSourceSchema } from '@tmagic/schema';
|
||||
import type { DataSourceSchema } from '@tmagic/schema';
|
||||
|
||||
export interface DataSourceOptions {
|
||||
schema: DataSourceSchema;
|
||||
@ -31,10 +31,7 @@ export interface HttpDataSourceOptions {
|
||||
}
|
||||
|
||||
export interface DataSourceManagerOptions {
|
||||
dataSourceConfigs: DataSourceSchema[];
|
||||
dataSourceDeps?: DataSourceDeps;
|
||||
dataSourceCondDeps?: DataSourceDeps;
|
||||
httpDataSourceOptions?: Partial<HttpDataSourceOptions>;
|
||||
app: any;
|
||||
}
|
||||
|
||||
export interface DataSourceManagerData {
|
||||
|
@ -1,24 +1,35 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import Core from '@tmagic/core';
|
||||
import { NodeType } from '@tmagic/schema';
|
||||
|
||||
import { DataSource, DataSourceManager } from '@data-source/index';
|
||||
|
||||
describe('DataSourceManager', () => {
|
||||
const dsm = new DataSourceManager({
|
||||
dataSourceConfigs: [
|
||||
const app = new Core({
|
||||
config: {
|
||||
type: NodeType.ROOT,
|
||||
id: '1',
|
||||
items: [],
|
||||
dataSources: [
|
||||
{
|
||||
type: 'base',
|
||||
id: '1',
|
||||
fields: [{ name: 'name' }],
|
||||
methods: [],
|
||||
},
|
||||
{
|
||||
type: 'http',
|
||||
id: '2',
|
||||
fields: [{ name: 'name' }],
|
||||
methods: [],
|
||||
},
|
||||
],
|
||||
httpDataSourceOptions: {
|
||||
request: () => Promise.resolve(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
describe('DataSourceManager', () => {
|
||||
const dsm = new DataSourceManager({
|
||||
app,
|
||||
});
|
||||
|
||||
test('instance', () => {
|
||||
@ -31,7 +42,7 @@ describe('DataSourceManager', () => {
|
||||
class TestDataSource extends DataSource {}
|
||||
|
||||
DataSourceManager.registe('test', TestDataSource);
|
||||
expect(DataSourceManager.dataSourceClassMap.get('test')).toBe(TestDataSource);
|
||||
expect(DataSourceManager.getDataSourceClass('test')).toBe(TestDataSource);
|
||||
});
|
||||
|
||||
test('get', () => {
|
||||
@ -46,24 +57,14 @@ describe('DataSourceManager', () => {
|
||||
});
|
||||
|
||||
test('updateSchema', () => {
|
||||
const dsm = new DataSourceManager({
|
||||
dataSourceConfigs: [
|
||||
{
|
||||
type: 'base',
|
||||
id: '1',
|
||||
fields: [{ name: 'name' }],
|
||||
},
|
||||
],
|
||||
httpDataSourceOptions: {
|
||||
request: () => Promise.resolve(),
|
||||
},
|
||||
});
|
||||
const dsm = new DataSourceManager({ app });
|
||||
|
||||
dsm.updateSchema([
|
||||
{
|
||||
type: 'base',
|
||||
id: '1',
|
||||
fields: [{ name: 'name1' }],
|
||||
methods: [],
|
||||
},
|
||||
]);
|
||||
const ds = dsm.get('1');
|
||||
@ -75,7 +76,13 @@ describe('DataSourceManager', () => {
|
||||
expect(dsm.dataSourceMap.size).toBe(0);
|
||||
});
|
||||
|
||||
test('addDataSource error', () => {
|
||||
expect(dsm.addDataSource()).toBeUndefined();
|
||||
test('addDataSource error', async () => {
|
||||
await dsm.addDataSource({
|
||||
type: 'base',
|
||||
id: '1',
|
||||
fields: [{ name: 'name' }],
|
||||
methods: [],
|
||||
});
|
||||
expect(dsm.get('1')).toBeInstanceOf(DataSource);
|
||||
});
|
||||
});
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import { MApp, MNode, NodeType } from '@tmagic/schema';
|
||||
import Core from '@tmagic/core';
|
||||
import { MApp, NodeType } from '@tmagic/schema';
|
||||
|
||||
import { createDataSourceManager, DataSourceManager } from '@data-source/index';
|
||||
|
||||
@ -37,13 +38,14 @@ const dsl: MApp = {
|
||||
name: 'text',
|
||||
},
|
||||
],
|
||||
methods: [],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
describe('createDataSourceManager', () => {
|
||||
test('instance', () => {
|
||||
const manager = createDataSourceManager(dsl, (node: MNode) => node, {});
|
||||
const manager = createDataSourceManager(new Core({ config: dsl }));
|
||||
expect(manager).toBeInstanceOf(DataSourceManager);
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user