Skip to content

Commit

Permalink
feat(cli,data-source,runtime): 数据源支持动态按需加载
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Apr 12, 2024
1 parent 872c978 commit 0061331
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 34 deletions.
9 changes: 8 additions & 1 deletion packages/cli/src/utils/prepareEntryFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export const prepareEntryFile = async (app: App) => {
'value-entry': generateContent(useTs, EntryType.VALUE, moduleMainFilePath.valueMap),
'event-entry': generateContent(useTs, EntryType.EVENT, moduleMainFilePath.eventMap),
'datasource-entry': generateContent(useTs, EntryType.DATASOURCE, moduleMainFilePath.datasourceMap),
'async-datasource-entry': generateContent(
useTs,
EntryType.DATASOURCE,
moduleMainFilePath.datasourceMap,
'',
dynamicImport,
),
'ds-config-entry': generateContent(useTs, EntryType.DS_CONFIG, moduleMainFilePath.dsConfigMap),
'ds-value-entry': generateContent(useTs, EntryType.DS_VALUE, moduleMainFilePath.dsValueMap),
'ds-event-entry': generateContent(useTs, EntryType.DS_EVENT, moduleMainFilePath.dsEventMap),
Expand All @@ -43,7 +50,7 @@ export const prepareEntryFile = async (app: App) => {
});
};

const generateContent = (
export const generateContent = (
useTs: boolean,
type: EntryType,
map: Record<string, string> = {},
Expand Down
7 changes: 6 additions & 1 deletion packages/data-source/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AppCore, DataSourceSchema, HttpOptions, RequestFunction } from '@tmagic/schema';

import HttpDataSource from './data-sources/Http';
import type DataSource from './data-sources/Base';
import type HttpDataSource from './data-sources/Http';

export interface DataSourceOptions<T extends DataSourceSchema = DataSourceSchema> {
schema: T;
Expand Down Expand Up @@ -42,3 +43,7 @@ export interface ChangeEvent {
path?: string;
updateData: any;
}

export type AsyncDataSourceResolveResult<T = typeof DataSource> = {
default: T;
};
33 changes: 32 additions & 1 deletion packages/data-source/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
replaceChildNode,
} from '@tmagic/utils';

import type { DataSourceManagerData } from './types';
import type { AsyncDataSourceResolveResult, DataSourceManagerData } from './types';

export const compliedConditions = (node: MNode, data: DataSourceManagerData) => {
if (!node.displayConds || !Array.isArray(node.displayConds) || !node.displayConds.length) return true;
Expand Down Expand Up @@ -151,3 +151,34 @@ export const compliedIteratorItems = (itemData: any, items: MNode[], dsId: strin
);
});
};

export const registerDataSourceOnDemand = async (
dsl: MApp,
dataSourceModules: Record<string, () => Promise<AsyncDataSourceResolveResult>>,
) => {
const { dataSourceCondDeps = {}, dataSourceDeps = {}, dataSources = [] } = dsl;

const dsModuleMap: Record<string, () => Promise<AsyncDataSourceResolveResult>> = {};

dataSources.forEach((ds) => {
let dep = dataSourceCondDeps[ds.id] || {};

if (!Object.keys(dep).length) {
dep = dataSourceDeps[ds.id] || {};
}

if (Object.keys(dep).length && dataSourceModules[ds.type]) {
dsModuleMap[ds.type] = dataSourceModules[ds.type];
}
});

const modules = await Promise.all(Object.values(dsModuleMap).map((asyncModule) => asyncModule()));

const moduleMap: Record<string, any> = {};
modules.forEach((module, index) => {
const type = Object.keys(dsModuleMap)[index];
moduleMap[type] = module.default;
});

return moduleMap;
};
4 changes: 2 additions & 2 deletions runtime/react/page/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { AppContent } from '@tmagic/ui-react';
import { getUrlParam } from '@tmagic/utils';

import components from '../.tmagic/comp-entry';
import datasources from '../.tmagic/datasource-entry';
import dataSources from '../.tmagic/datasource-entry';
import plugins from '../.tmagic/plugin-entry';

import App from './App';
Expand Down Expand Up @@ -53,7 +53,7 @@ const getLocalConfig = (): MApp[] => {

window.magicDSL = [];

Object.entries(datasources).forEach(([type, ds]: [string, any]) => {
Object.entries(dataSources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.register(type, ds);
});

Expand Down
6 changes: 3 additions & 3 deletions runtime/react/playground/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { AppContent } from '@tmagic/ui-react';
import { replaceChildNode } from '@tmagic/utils';

import components from '../.tmagic/comp-entry';
import datasources from '../.tmagic/datasource-entry';
import dataSources from '../.tmagic/datasource-entry';
import plugins from '../.tmagic/plugin-entry';

import App from './App';
Expand All @@ -41,8 +41,8 @@ declare global {
}
}

Object.entries(datasources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.registe(type, ds);
Object.entries(dataSources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.register(type, ds);
});

const app = new Core({
Expand Down
4 changes: 2 additions & 2 deletions runtime/vue2/page/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { DataSourceManager } from '@tmagic/data-source';
import { getUrlParam } from '@tmagic/utils';

import components from '../.tmagic/comp-entry';
import datasources from '../.tmagic/datasource-entry';
import dataSources from '../.tmagic/datasource-entry';
import plugins from '../.tmagic/plugin-entry';

import request from './utils/request';
Expand All @@ -38,7 +38,7 @@ Object.keys(components).forEach((type: string) => {
Vue.component(`magic-ui-${type}`, components[type]);
});

Object.entries(datasources).forEach(([type, ds]: [string, any]) => {
Object.entries(dataSources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.register(type, ds);
});

Expand Down
6 changes: 3 additions & 3 deletions runtime/vue2/playground/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ Promise.all([
import('../.tmagic/comp-entry'),
import('../.tmagic/plugin-entry'),
import('../.tmagic/datasource-entry'),
]).then(([components, plugins, datasources]) => {
]).then(([components, plugins, dataSources]) => {
Object.entries(components.default).forEach(([type, component]: [string, any]) => {
Vue.component(`magic-ui-${type}`, component);
});

Object.entries(datasources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.registe(type, ds);
Object.entries(dataSources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.register(type, ds);
});

Object.values(plugins.default).forEach((plugin: any) => {
Expand Down
2 changes: 1 addition & 1 deletion runtime/vue3/page/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
display: none;
}
</style>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.4.21/vue.global.prod.min.js"></script>
</head>
<body style="font-size: 14px">
<div id="app"></div>
Expand Down
36 changes: 20 additions & 16 deletions runtime/vue3/page/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import { createApp, defineAsyncComponent } from 'vue';

import Core from '@tmagic/core';
import { DataSourceManager } from '@tmagic/data-source';
import { DataSourceManager, registerDataSourceOnDemand } from '@tmagic/data-source';
import { getUrlParam } from '@tmagic/utils';

import components from '../.tmagic/async-comp-entry';
import datasources from '../.tmagic/datasource-entry';
import asyncDataSources from '../.tmagic/async-datasource-entry';
import plugins from '../.tmagic/plugin-entry';

import request from './utils/request';
Expand All @@ -40,24 +40,28 @@ Object.entries(components).forEach(([type, component]: [string, any]) => {
magicApp.component(`magic-ui-${type}`, defineAsyncComponent(component));
});

Object.entries(datasources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.register(type, ds);
});

Object.values(plugins).forEach((plugin: any) => {
magicApp.use(plugin);
});

const app = new Core({
ua: window.navigator.userAgent,
config: ((getUrlParam('localPreview') ? getLocalConfig() : window.magicDSL) || [])[0] || {},
curPage: getUrlParam('page'),
useMock: Boolean(getUrlParam('useMock')),
});
const dsl = ((getUrlParam('localPreview') ? getLocalConfig() : window.magicDSL) || [])[0] || {};

app.setDesignWidth(app.env.isWeb ? window.document.documentElement.getBoundingClientRect().width : 375);
registerDataSourceOnDemand(dsl, asyncDataSources).then((dataSources) => {
Object.entries(dataSources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.register(type, ds);
});

magicApp.config.globalProperties.app = app;
magicApp.provide('app', app);
const app = new Core({
ua: window.navigator.userAgent,
config: dsl,
curPage: getUrlParam('page'),
useMock: Boolean(getUrlParam('useMock')),
});

magicApp.mount('#app');
app.setDesignWidth(app.env.isWeb ? window.document.documentElement.getBoundingClientRect().width : 375);

magicApp.config.globalProperties.app = app;
magicApp.provide('app', app);

magicApp.mount('#app');
});
2 changes: 1 addition & 1 deletion runtime/vue3/playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

<div id="app" class="in-editor"></div>

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.4.21/vue.global.prod.min.js"></script>

<script type="module" src="./main.ts"></script>
</body>
Expand Down
6 changes: 3 additions & 3 deletions runtime/vue3/playground/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ Promise.all([
import('../.tmagic/comp-entry'),
import('../.tmagic/plugin-entry'),
import('../.tmagic/datasource-entry'),
]).then(([components, plugins, datasources]) => {
]).then(([components, plugins, dataSources]) => {
const magicApp = createApp(App);

Object.entries(components.default).forEach(([type, component]: [string, any]) => {
magicApp.component(`magic-ui-${type}`, component);
});

Object.entries(datasources).forEach(([type, ds]: [string, any]) => {
DataSourceManager.registe(type, ds);
Object.entries(dataSources.default).forEach(([type, ds]: [string, any]) => {
DataSourceManager.register(type, ds);
});

Object.values(plugins.default).forEach((plugin: any) => {
Expand Down

0 comments on commit 0061331

Please sign in to comment.