From f224abffffcbdf53f8d6568e46da8f4582ad7e1b Mon Sep 17 00:00:00 2001 From: "wuyue.xht" Date: Tue, 1 Sep 2020 19:15:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=8F=E5=87=BA=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=BE=B9=E7=95=8C=E6=8D=95=E6=8D=89=E4=B8=8D=E5=88=B0=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/react-provider/package.json | 2 +- .../src/components/LazyComponent/index.tsx | 9 +- packages/runtime/package.json | 2 +- packages/runtime/src/core/index.ts | 386 ++---------------- packages/runtime/src/core/runApp.ts | 7 +- 5 files changed, 44 insertions(+), 362 deletions(-) diff --git a/packages/react-provider/package.json b/packages/react-provider/package.json index 090339a48..e81069d28 100644 --- a/packages/react-provider/package.json +++ b/packages/react-provider/package.json @@ -1,6 +1,6 @@ { "name": "@ali/lowcode-react-provider", - "version": "1.0.6-0", + "version": "1.0.6-1", "description": "React Provider for Runtime", "files": [ "es", diff --git a/packages/react-provider/src/components/LazyComponent/index.tsx b/packages/react-provider/src/components/LazyComponent/index.tsx index ec2b1c0e9..4cf20a341 100644 --- a/packages/react-provider/src/components/LazyComponent/index.tsx +++ b/packages/react-provider/src/components/LazyComponent/index.tsx @@ -32,19 +32,24 @@ export default class LazyComponent extends Component { const schema = await getPageData(); this.setState({ schema }); context.emitPageReady(); - } catch (error) { + } catch (err) { this.setState({ hasError: true }); + this.exeAfterCatch(err.message, err.stack); } } } - componentDidCatch(error: any, errorInfo: any) { + exeAfterCatch(error: any, errorInfo?: any) { const { afterCatch } = app.getErrorBoundary() || {}; if (typeof afterCatch === 'function') { afterCatch.call(this, error, errorInfo); } } + componentDidCatch(error: any, errorInfo: any) { + this.exeAfterCatch(error, errorInfo); + } + render() { const { schema, hasError } = this.state; if (hasError) { diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 98d5ec817..4e433ffcf 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -1,6 +1,6 @@ { "name": "@ali/lowcode-runtime", - "version": "1.0.6-0", + "version": "1.0.6-1", "description": "Runtime for Ali lowCode engine", "files": [ "es", diff --git a/packages/runtime/src/core/index.ts b/packages/runtime/src/core/index.ts index 5268797ab..720e9e471 100644 --- a/packages/runtime/src/core/index.ts +++ b/packages/runtime/src/core/index.ts @@ -1,383 +1,57 @@ -import { IAppConfig, IUtils, IComponents, HistoryMode } from './runApp'; -import EventEmitter from '@ali/offline-events'; +import Container, { ILayoutOptions, IErrorBoundaryConfig } from './container'; +import Provider from './provider'; +import runApp from './runApp'; -interface IConstants { - [key: string]: any; -} - -interface IComponentMap { - componentName: string; - package?: string; - version?: string; - destructuring?: boolean; - exportName?: string; - subName?: string; -} - -interface ILayoutConfig { - componentName: string; - props: any; -} - -interface IRouterConfig { - [key: string]: string; -} - -interface IHistoryConfig { - mode: HistoryMode; - basement?: string; -} - -export interface IAppData { - history?: HistoryMode; - layout?: ILayoutConfig; - routes?: IRouterConfig; - containerId?: string; - components?: IComponents; - componentsMap?: IComponentMap[]; - utils?: IUtils; - constants?: IConstants; - i18n?: I18n; -} - -export interface ComponentProps { - [key: string]: any; -} - -export interface JSExpression { - type: string; - value: string; - [key: string]: string; -} - -export interface DataSourceItem { - id: string; - isInit: boolean; - type: string; - options: { - uri: string; - params: object; - method: string; - shouldFetch?: string; - willFetch?: string; - fit?: string; - didFetch?: string; - }; - dataHandler: JSExpression; -} - -export interface DataSource { - list: DataSourceItem[]; - dataHandler: JSExpression; -} - -export interface LifeCycles { - [key: string]: JSExpression; -} - -export interface Methods { - [key: string]: JSExpression; -} - -export interface ComponentModel { - id?: string; - componentName: string; - fileName?: string; - props?: ComponentProps; - css?: string; - dataSource?: DataSource; - lifeCycles?: LifeCycles; - methods?: Methods; - children?: ComponentModel[] | string[]; - condition?: JSExpression | boolean; - loop?: string[]; - loopArgs?: string[]; -} - -export interface I18n { - 'zh-CN': { [key: string]: string }; - 'en-US': { [key: string]: string }; -} - -type Locale = 'zh-CN' | 'en-US'; - -export default class Provider { - emitter: EventEmitter = new EventEmitter(); - components: IComponents = {}; - utils: IUtils = {}; - constants: IConstants = {}; - routes: IRouterConfig | null = null; - layout: ILayoutConfig | null = null; - componentsMap: IComponentMap[] = []; - history: HistoryMode = 'hash'; - containerId = ''; - i18n: I18n | null = null; - homePage = ''; - lazyElementsMap: { [key: string]: any } = {}; - isSectionalRender = false; +class App { + private container: Container; constructor() { - this.init(); + this.container = new Container(); } - async(): Promise { - return new Promise(async (resolve, reject) => { - try { - const appData: IAppData = await this.getAppData(); - if (!appData) { - return; - } - const { history, layout, routes, containerId, components, componentsMap, utils, constants, i18n } = appData; - this.setHistory(history); - this.setLayoutConfig(layout); - this.setRouterConfig(routes); - this.setContainerId(containerId); - this.setI18n(i18n); - this.registerComponents(components); - this.registerComponentsMap(componentsMap); - this.registerUtils(utils); - this.registerContants(constants); - resolve({ - history: this.getHistory(), - components: this.getComponents(), - utils: this.getUtils(), - containerId: this.getContainerId(), - }); - } catch (err) { - reject(err); - } - }); + run() { + runApp(); } - init() { - // 默认 ready,当重载了init时需手动触发 this.ready() - this.ready(); + registerRenderer(renderer: any): any { + this.container.registerRenderer(renderer); } - ready(params?: any) { - if (params && typeof params === 'function') { - params = params(); - } - this.emitter.emit('ready', params || ''); + registerLayout(Layout: any, options: ILayoutOptions): any { + this.container.registerLayout(Layout, options); } - onReady(cb: (params?: any) => void) { - if (!cb || typeof cb !== 'function') { - return; - } - this.emitter.on('ready', cb); + registerLoading(component: any) { + this.container.registerLoading(component); } - emitPageReady() { - this.emitter.emit('pageReady'); + registerProvider(CustomProvider: any) { + this.container.registerProvider(CustomProvider); } - emitPageEnter() { - this.emitter.emit('pageEnter'); + registerErrorBoundary(config: IErrorBoundaryConfig) { + this.container.registerErrorBoundary(config); } - emitPageUpdate() { - this.emitter.emit('pageUpdate'); + getLayout(componentName: string) { + return this.container.getLayout(componentName); } - emitPageLeave() { - this.emitter.emit('pageLeave'); + getRenderer(): any { + return this.container.getRenderer(); } - onPageReady(cb: (params?: any) => void) { - this.emitter.on('pageReady', cb); - return () => { - this.emitter.removeListener('pageReady', cb); - }; + getLoading(): any { + return this.container.getLoading(); } - onPageEnter(cb: (params?: any) => void) { - this.emitter.on('pageEnter', cb); - return () => { - this.emitter.removeListener('pageEnter', cb); - }; + getErrorBoundary(): IErrorBoundaryConfig { + return this.container.getErrorBoundary(); } - onPageUpdate(cb: (params?: any) => void) { - this.emitter.on('pageUpdate', cb); - return () => { - this.emitter.removeListener('pageUpdate', cb); - }; - } - - onPageLeave(cb: (params?: any) => void) { - this.emitter.on('pageLeave', cb); - return () => { - this.emitter.removeListener('pageLeave', cb); - }; - } - - getAppData(): any { - throw new Error('Method called "getAppData" not implemented.'); - } - - getPageData(pageId?: string): any { - throw new Error('Method called "getPageData" not implemented.'); - } - - getLazyComponent(pageId: string, props: any): any { - throw new Error('Method called "getLazyComponent" not implemented.'); - } - - // 定制构造根组件的逻辑,如切换路由机制 - createApp() { - throw new Error('Method called "createApp" not implemented.'); - } - - runApp(App: any, config: IAppConfig) { - throw new Error('Method called "runApp" not implemented.'); - } - - registerComponents(components: IComponents | undefined) { - if (!components) { - return; - } - this.components = components; - } - - registerComponentsMap(componentsMap: IComponentMap[] | undefined) { - if (!componentsMap) { - return; - } - this.componentsMap = componentsMap; - } - - registerUtils(utils: IUtils | undefined) { - if (!utils) { - return; - } - this.utils = utils; - } - - registerContants(constants: IConstants | undefined) { - if (!constants) { - return; - } - this.constants = constants; - } - - setLayoutConfig(config: ILayoutConfig | undefined) { - if (!config) { - return; - } - this.layout = config; - } - - setRouterConfig(config: IRouterConfig | undefined) { - if (!config) { - return; - } - this.routes = config; - } - - setHistory(config: HistoryMode | undefined): any { - if (!config) { - return; - } - this.history = config; - } - - setContainerId(id: string | undefined) { - if (!id) { - return; - } - this.containerId = id; - } - - setI18n(i18n: I18n | undefined) { - if (!i18n) { - return; - } - this.i18n = i18n; - } - - setLazyElement(pageId: string, cache: any) { - if (!pageId || !cache) { - return; - } - this.lazyElementsMap[pageId] = cache; - } - - setHomePage(pageId: string) { - if (pageId) { - this.homePage = pageId; - } - } - - getComponents() { - return this.components; - } - - getComponent(name: string) { - if (!name) { - return; - } - return this.components[name]; - } - - getUtils() { - return this.utils; - } - - getConstants() { - return this.constants; - } - - getComponentsMap() { - return this.componentsMap; - } - - getComponentsMapObj() { - const compMapArr = this.getComponentsMap(); - if (!compMapArr || !Array.isArray(compMapArr)) { - return; - } - const compMapObj: any = {}; - compMapArr.forEach((item: IComponentMap) => { - if (!item || !item.componentName) { - return; - } - compMapObj[item.componentName] = item; - }); - return compMapObj; - } - - getLayoutConfig() { - return this.layout; - } - - getRouterConfig() { - return this.routes; - } - - getHistory() { - return this.history; - } - - getContainerId() { - return this.containerId || 'App'; - } - - getI18n(locale?: Locale) { - if (!this.i18n) { - return; - } - return locale ? this.i18n[locale] : this.i18n; - } - - getHomePage() { - return this.homePage; - } - - getLazyElement(pageId: string) { - if (!pageId) { - return; - } - return this.lazyElementsMap[pageId]; + getProvider(id?: string): Provider | undefined { + return this.container.getProvider(id); } } + +export default new App(); diff --git a/packages/runtime/src/core/runApp.ts b/packages/runtime/src/core/runApp.ts index 998d005a8..6c6cc2318 100644 --- a/packages/runtime/src/core/runApp.ts +++ b/packages/runtime/src/core/runApp.ts @@ -36,9 +36,12 @@ export default function runApp() { } const App = provider.createApp(); provider.runApp(App, config); - }).catch((err) => { + }).catch((err: Error) => { console.error(err.message); - const { fallbackUI } = app.getErrorBoundary() || {}; + const { fallbackUI, afterCatch } = app.getErrorBoundary() || {}; + if (typeof afterCatch === 'function') { + afterCatch(err.message, err.stack); + } if (!fallbackUI) { return; }