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