feat: 🎸 Rax 出码中添加数据源的 dataHandler 并与数据源引擎的对齐参数

This commit is contained in:
牧毅 2020-08-17 10:46:44 +08:00
parent 8114c6f050
commit 42b9db3f32
4 changed files with 91 additions and 31 deletions

View File

@ -13,6 +13,7 @@ import {
} from '../../../types'; } from '../../../types';
import { generateUnknownType } from '../../../utils/compositeType'; import { generateUnknownType } from '../../../utils/compositeType';
import { parseExpressionConvertThis2Context } from '../../../utils/expressionParser';
import { isContainerSchema } from '../../../utils/schema'; import { isContainerSchema } from '../../../utils/schema';
import { RAX_CHUNK_NAME } from './const'; import { RAX_CHUNK_NAME } from './const';
@ -46,8 +47,8 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
fileType: cfg.fileType, fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsVar, name: CLASS_DEFINE_CHUNK_NAME.InsVar,
content: ` content: `
_dataSourceList = this._defineDataSourceList(); _dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceList, this._context);`, _dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, { runtimeConfig: true });`,
linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start], linkAfter: [CLASS_DEFINE_CHUNK_NAME.Start],
}); });
@ -61,25 +62,32 @@ const pluginFactory: BuilderComponentPluginFactory<PluginConfig> = (config?) =>
linkAfter: [RAX_CHUNK_NAME.ClassDidMountBegin], linkAfter: [RAX_CHUNK_NAME.ClassDidMountBegin],
}); });
const dataSource = isContainerSchema(pre.ir) ? pre.ir.dataSource : null; const dataSourceConfig = isContainerSchema(pre.ir) ? pre.ir.dataSource : null;
const dataSourceItems: DataSourceConfig[] = (dataSource && dataSource.list) || []; const dataSourceItems: DataSourceConfig[] = (dataSourceConfig && dataSourceConfig.list) || [];
next.chunks.push({ next.chunks.push({
type: ChunkType.STRING, type: ChunkType.STRING,
fileType: cfg.fileType, fileType: cfg.fileType,
name: CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod, name: CLASS_DEFINE_CHUNK_NAME.InsPrivateMethod,
// TODO: 下面的定义应该需要调用 @ali/lowcode-datasource-engine 的方法来搞:
content: ` content: `
_defineDataSourceList() { _defineDataSourceConfig() {
return (function(){ const __$$context = this._context;
return (${generateUnknownType([ return (${generateUnknownType(
{
...dataSourceConfig,
list: [
...dataSourceItems.map((item) => ({ ...dataSourceItems.map((item) => ({
...item, ...item,
isInit: wrapAsFunction(item.isInit), isInit: wrapAsFunction(item.isInit),
options: wrapAsFunction(item.options), options: wrapAsFunction(item.options),
})), })),
])}); ],
}).call(this._context); },
{
function: (jsFunc) => parseExpressionConvertThis2Context(jsFunc.value, '__$$context'),
expression: (jsExpr) => parseExpressionConvertThis2Context(jsExpr.value, '__$$context'),
},
)});
}`, }`,
linkAfter: [RAX_CHUNK_NAME.ClassRenderEnd], linkAfter: [RAX_CHUNK_NAME.ClassRenderEnd],
}); });
@ -95,12 +103,12 @@ function wrapAsFunction(value: CompositeValue): CompositeValue {
if (isJSExpression(value) || isJSFunction(value)) { if (isJSExpression(value) || isJSFunction(value)) {
return { return {
type: 'JSExpression', type: 'JSExpression',
value: `() => (${value.value})`, value: `function(){ return ((${value.value}))}`,
}; };
} }
return { return {
type: 'JSExpression', type: 'JSExpression',
value: `() => (${generateUnknownType(value)})`, value: `function(){return((${generateUnknownType(value)}))}`,
}; };
} }

View File

@ -19,8 +19,8 @@ class Home$$Page extends Component {
_context = this._createContext(); _context = this._createContext();
_dataSourceList = this._defineDataSourceList(); _dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceList, this._context); _dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, { runtimeConfig: true });
_utils = this._defineUtils(); _utils = this._defineUtils();
@ -74,10 +74,9 @@ class Home$$Page extends Component {
return context; return context;
} }
_defineDataSourceList() { _defineDataSourceConfig() {
return function () { const __$$context = this._context;
return []; return { list: [] };
}.call(this._context);
} }
_defineUtils() { _defineUtils() {

View File

@ -38,8 +38,8 @@ class Home$$Page extends Component {
_context = this._createContext(); _context = this._createContext();
_dataSourceList = this._defineDataSourceList(); _dataSourceConfig = this._defineDataSourceConfig();
_dataSourceEngine = __$$createDataSourceEngine(this._dataSourceList, this._context); _dataSourceEngine = __$$createDataSourceEngine(this._dataSourceConfig, this._context, { runtimeConfig: true });
_utils = this._defineUtils(); _utils = this._defineUtils();
@ -161,24 +161,65 @@ class Home$$Page extends Component {
return context; return context;
} }
_defineDataSourceList() { _defineDataSourceConfig() {
return function () { const __$$context = this._context;
return [ return {
{ id: 'urlParams', type: 'urlParams', isInit: () => undefined, options: () => undefined }, list: [
{
id: 'urlParams',
type: 'urlParams',
isInit: function () {
return undefined;
},
options: function () {
return undefined;
},
},
{ {
id: 'user', id: 'user',
type: 'fetch', type: 'fetch',
options: () => ({ method: 'GET', uri: 'https://shs.alibaba-inc.com/mock/1458/demo/user' }), options: function () {
isInit: () => undefined, return {
method: 'GET',
uri: 'https://shs.alibaba-inc.com/mock/1458/demo/user',
};
},
dataHandler: function (response) {
if (!response.success) {
throw new Error(response.message);
}
return response.data;
},
isInit: function () {
return undefined;
},
}, },
{ {
id: 'orders', id: 'orders',
type: 'fetch', type: 'fetch',
options: () => ({ method: 'GET', uri: this.state.user.ordersApiUri }), options: function () {
isInit: () => undefined, return {
method: 'GET',
uri: __$$context.state.user.ordersApiUri,
};
}, },
]; dataHandler: function (response) {
}.call(this._context); if (!response.success) {
throw new Error(response.message);
}
return response.data.result;
},
isInit: function () {
return undefined;
},
},
],
dataHander: function (dataMap) {
console.info('All datasources loaded:', dataMap);
},
};
} }
_defineUtils() { _defineUtils() {

View File

@ -82,6 +82,10 @@
method: 'GET', method: 'GET',
uri: 'https://shs.alibaba-inc.com/mock/1458/demo/user', uri: 'https://shs.alibaba-inc.com/mock/1458/demo/user',
}, },
dataHandler: {
type: 'JSFunction',
value: 'function (response) {\nif (!response.success){\n throw new Error(response.message);\n }\n return response.data;\n}',
},
}, },
// 示例数据源https://shs.alibaba-inc.com/mock/1458/demo/orders // 示例数据源https://shs.alibaba-inc.com/mock/1458/demo/orders
{ {
@ -94,8 +98,16 @@
value: 'this.state.user.ordersApiUri', value: 'this.state.user.ordersApiUri',
}, },
}, },
dataHandler: {
type: 'JSFunction',
value: 'function (response) {\nif (!response.success){\n throw new Error(response.message);\n }\n return response.data.result;\n}',
},
}, },
], ],
dataHander: {
type: 'JSFunction',
value: 'function (dataMap) {\n console.info("All datasources loaded:", dataMap);\n}',
},
}, },
children: [ children: [
{ {