refactor(register): adjust getPlugins returns type (#5363)

This commit is contained in:
Aaron 2024-01-23 14:40:43 +08:00 committed by GitHub
parent 9f09fa4eef
commit a4aa09bc41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 12 additions and 16 deletions

View File

@ -135,7 +135,7 @@ export default abstract class Item implements IItem {
lodLevels: modelLodLevels,
enableBalanceShape,
} = this.displayModel.data;
const RenderExtension = renderExtensions.find((ext) => ext.type === type);
const RenderExtension = renderExtensions[type];
this.themeStyles = theme.styles;
this.lodLevels = modelLodLevels ? formatLodLevels(modelLodLevels) : theme.lodLevels;
if (!RenderExtension) {
@ -270,7 +270,7 @@ export default abstract class Item implements IItem {
Object.values(this.shapeMap).forEach((child) => child.destroy());
this.shapeMap = { keyShape: undefined };
const { type = this.type === 'node' ? 'circle-node' : 'line-edge' } = displayModel.data;
const RenderExtension = this.renderExtensions.find((ext) => ext.type === type);
const RenderExtension = this.renderExtensions[type];
this.renderExt = new RenderExtension({
themeStyles: this.themeStyles.default,
lodLevels: this.lodLevels,

View File

@ -65,7 +65,6 @@ function register<T extends PluginCategory>(category: T, type: string, pluginCla
);
}
pluginClass.type = type;
pluginRegistry[category].set(type, pluginClass);
}
@ -83,15 +82,15 @@ function getPlugin<T extends PluginCategory>(category: T, type: string): PluginR
}
/**
* <zh/>
* <zh/>
*
* <en/> Retrieves a list of all plugin classes for a given category.
* <en/> Retrieves all plugin classes for a given category.
* @param category - <zh/> | <en/> Plugin category to retrieve
* @returns <zh/> | <en/> Returns an array of all plugin classes for the specified category
* @returns <zh/> | <en/> Returns all plugin classes for the specified category
* @internal
*/
function getPlugins<T extends PluginCategory>(category: T): PluginRegistry[T][string][] {
return Array.from(pluginRegistry[category]?.values() || []);
function getPlugins<T extends PluginCategory>(category: T) {
return Object.fromEntries(pluginRegistry[category]) as PluginRegistry[T];
}
export { getPlugin, getPlugins, register };

View File

@ -87,9 +87,9 @@ const getWarnMsg = {
*/
export class ItemController {
public graph: Graph;
public nodeExtensions: NodeRegistry[string][] = [];
public edgeExtensions: EdgeRegistry[string][] = [];
public comboExtensions: NodeRegistry[string][] = [];
public nodeExtensions: NodeRegistry = {};
public edgeExtensions: EdgeRegistry = {};
public comboExtensions: NodeRegistry = {};
public zoom: number;

View File

@ -42,10 +42,7 @@ export class ThemeController {
}
private getThemes(): ThemeRegistry {
return getPlugins('theme').reduce((res, acc) => {
res[acc.type] = acc;
return res;
}, {}) as ThemeRegistry;
return getPlugins('theme');
}
/**

View File

@ -47,7 +47,7 @@ describe('Plugin Registration', () => {
it('should retrieve all plugins for a given category', () => {
register(category, type, CustomBehavior);
const plugins = getPlugins(category);
expect(plugins).toContain(CustomBehavior);
expect(plugins[type]).toBe(CustomBehavior);
});
});
});