feat(data-source): 支持在指定js环境下不执行init的配置

This commit is contained in:
roymondchen 2023-12-18 17:27:37 +08:00
parent 2a5b9ec6bd
commit 86bdb9f099
3 changed files with 20 additions and 4 deletions

View File

@ -31,6 +31,7 @@ import {
type DeprecatedEventConfig,
type EventConfig,
type Id,
type JsEngine,
type MApp,
type RequestFunction,
} from '@tmagic/schema';
@ -45,7 +46,7 @@ interface AppOptionsConfig {
ua?: string;
config?: MApp;
platform?: 'editor' | 'mobile' | 'tv' | 'pc';
jsEngine?: 'browser' | 'hippy' | 'nodejs';
jsEngine?: JsEngine;
designWidth?: number;
curPage?: Id;
useMock?: boolean;

View File

@ -29,10 +29,17 @@ import type { DataSourceManagerData, DataSourceManagerOptions } from './types';
class DataSourceManager extends EventEmitter {
private static dataSourceClassMap = new Map<string, typeof DataSource>();
public static registe<T extends typeof DataSource = typeof DataSource>(type: string, dataSource: T) {
public static register<T extends typeof DataSource = typeof DataSource>(type: string, dataSource: T) {
DataSourceManager.dataSourceClassMap.set(type, dataSource);
}
/**
* @deprecated
*/
public static registe<T extends typeof DataSource = typeof DataSource>(type: string, dataSource: T) {
DataSourceManager.register(type, dataSource);
}
public static getDataSourceClass(type: string) {
return DataSourceManager.dataSourceClassMap.get(type);
}
@ -66,6 +73,10 @@ class DataSourceManager extends EventEmitter {
return;
}
if (this.app.jsEngine && ds.schema.disabledInitInJsEngine?.includes(this.app.jsEngine)) {
return;
}
const beforeInit: ((...args: any[]) => any)[] = [];
const afterInit: ((...args: any[]) => any)[] = [];
@ -239,6 +250,6 @@ class DataSourceManager extends EventEmitter {
}
}
DataSourceManager.registe('http', HttpDataSource as any);
DataSourceManager.register('http', HttpDataSource as any);
export default DataSourceManager;

View File

@ -33,13 +33,15 @@ export interface HttpOptions {
export type RequestFunction = <T = any>(options: HttpOptions) => Promise<T>;
export type JsEngine = 'browser' | 'hippy' | 'nodejs';
export interface AppCore {
/** 页面配置描述 */
dsl?: MApp;
/** 允许平台editor: 编辑器中mobile: 手机端tv: 电视端, pc: 电脑端 */
platform?: 'editor' | 'mobile' | 'tv' | 'pc' | string;
/** 代码运行环境 */
jsEngine?: 'browser' | 'hippy' | 'nodejs' | string;
jsEngine?: JsEngine | string;
/** 网络请求函数 */
request?: RequestFunction;
[key: string]: any;
@ -237,6 +239,8 @@ export interface DataSourceSchema {
methods: CodeBlockContent[];
/** mock数据 */
mocks?: MockSchema[];
/** 不执行init的环境 */
disabledInitInJsEngine?: (JsEngine | string)[];
/** 扩展字段 */
[key: string]: any;
}