refactor(registry): register support override

This commit is contained in:
Aaron 2024-07-12 17:01:43 +08:00
parent dcd7d36c76
commit c0e832e5dc
2 changed files with 14 additions and 1 deletions

View File

@ -80,4 +80,12 @@ describe('registry', () => {
'rect-node': RectNode,
});
});
it('override', () => {
class CircleNode {}
class RectNode {}
register('node', 'circle-node', CircleNode as any);
register('node', 'circle-node', RectNode as any, true);
expect(getExtension('node', 'circle-node')).toEqual(RectNode);
});
});

View File

@ -38,6 +38,10 @@ const EXTENSION_REGISTRY: ExtensionRegistry = {
* <zh/> 使
*
* <en/> The extension class to be registered. An instance will be created upon use
* @param override
* <zh/>
*
* <en/> Whether to override the registered extension
* @remarks
* <zh/> `register` 使
*
@ -56,9 +60,10 @@ export function register<T extends ExtensionCategory>(
category: Loosen<T>,
type: string,
Ctor: ExtensionRegistry[T][string],
override = false,
) {
const ext = EXTENSION_REGISTRY[category][type];
if (ext) {
if (!override && ext) {
if (ext !== Ctor) print.warn(`The extension ${type} of ${category} has been registered before.`);
} else Object.assign(EXTENSION_REGISTRY[category]!, { [type]: Ctor });
}