fix: application bug (#2958)

* fix: application bug
This commit is contained in:
jack zhang 2023-11-05 19:22:41 +08:00 committed by GitHub
parent bce02ad1d7
commit 9249dcb896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 14 additions and 9 deletions

View File

@ -8,6 +8,7 @@ export const app = new Application({
}, },
plugins: [NocoBaseClientPresetPlugin], plugins: [NocoBaseClientPresetPlugin],
ws: true, ws: true,
loadRemotePlugins: true,
devDynamicImport, devDynamicImport,
}); });

View File

@ -38,6 +38,7 @@ export interface ApplicationOptions {
scopes?: Record<string, any>; scopes?: Record<string, any>;
router?: RouterOptions; router?: RouterOptions;
devDynamicImport?: DevDynamicImport; devDynamicImport?: DevDynamicImport;
loadRemotePlugins?: boolean;
} }
export class Application { export class Application {
@ -75,7 +76,7 @@ export class Application {
...options.router, ...options.router,
renderComponent: this.renderComponent.bind(this), renderComponent: this.renderComponent.bind(this),
}); });
this.pm = new PluginManager(options.plugins, this); this.pm = new PluginManager(options.plugins, options.loadRemotePlugins, this);
this.addDefaultProviders(); this.addDefaultProviders();
this.addReactRouterComponents(); this.addReactRouterComponents();
this.addProviders(options.providers || []); this.addProviders(options.providers || []);

View File

@ -19,6 +19,7 @@ export class PluginManager {
constructor( constructor(
protected _plugins: PluginType[], protected _plugins: PluginType[],
protected loadRemotePlugins: boolean,
protected app: Application, protected app: Application,
) { ) {
this.app = app; this.app = app;
@ -27,7 +28,9 @@ export class PluginManager {
async init(_plugins: PluginType[]) { async init(_plugins: PluginType[]) {
await this.initStaticPlugins(_plugins); await this.initStaticPlugins(_plugins);
await this.initRemotePlugins(); if (this.loadRemotePlugins) {
await this.initRemotePlugins();
}
} }
private async initStaticPlugins(_plugins: PluginType[] = []) { private async initStaticPlugins(_plugins: PluginType[] = []) {

View File

@ -331,8 +331,8 @@ describe('Application', () => {
router, router,
}); });
const ErrorFallback = () => { const AppError = () => {
return <div>ErrorFallback</div>; return <div>AppError</div>;
}; };
const Foo = () => { const Foo = () => {
throw new Error('error'); throw new Error('error');
@ -340,7 +340,7 @@ describe('Application', () => {
}; };
app.use(Foo); app.use(Foo);
app.addComponents({ app.addComponents({
ErrorFallback, AppError,
}); });
const originalConsoleWarn = console.error; const originalConsoleWarn = console.error;
@ -352,8 +352,7 @@ describe('Application', () => {
await sleep(10); await sleep(10);
expect(fn).toBeCalled(); expect(fn).toBeCalled();
expect(screen.getByText('ErrorFallback')).toBeInTheDocument(); expect(screen.getByText('AppError')).toBeInTheDocument();
screen.debug();
console.error = originalConsoleWarn; console.error = originalConsoleWarn;
}); });

View File

@ -23,7 +23,7 @@ export const AppComponent: FC<AppComponentProps> = observer((props) => {
if (app.error?.code === 'LOAD_ERROR') return app.renderComponent('AppError', { app }); if (app.error?.code === 'LOAD_ERROR') return app.renderComponent('AppError', { app });
return ( return (
<ErrorBoundary FallbackComponent={app.getComponent<FallbackProps>('ErrorFallback')} onError={handleErrors}> <ErrorBoundary FallbackComponent={app.getComponent<FallbackProps>('AppError')} onError={handleErrors}>
<ApplicationContext.Provider value={app}> <ApplicationContext.Provider value={app}>
{app.maintained && app.maintaining && app.renderComponent('AppMaintainingDialog', { app })} {app.maintained && app.maintaining && app.renderComponent('AppMaintainingDialog', { app })}
{app.renderComponent('AppMain')} {app.renderComponent('AppMain')}

View File

@ -28,7 +28,7 @@ export const compose = (...components: [ComponentType, any][]) => {
<Child {...childProps}>{children}</Child> <Child {...childProps}>{children}</Child>
</Parent> </Parent>
); );
ComposeComponent.displayName = Child.displayName; ComposeComponent.displayName = Child.displayName || Child.name;
return ComposeComponent; return ComposeComponent;
}, BlankComponent); }, BlankComponent);

View File

@ -74,3 +74,4 @@ export const SchemaComponentProvider: React.FC<ISchemaComponentProvider> = (prop
</SchemaComponentContext.Provider> </SchemaComponentContext.Provider>
); );
}; };
SchemaComponentProvider.displayName = 'SchemaComponentProvider';