Skip to content

Commit

Permalink
Add ExtensionRegistryMergeStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
wpiers committed Jun 4, 2024
1 parent 741eb6e commit 9def101
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
DataExtension,
DataExtensionPoint,
} from './ExtensionRegistry.types';
import { ExtensionRegistryMergeStrategy } from './ExtensionRegistryMergeStrategy';

export class ExtensionRegistry {
private components: Record<string, ComponentExtension<any>[]> = {};
Expand Down Expand Up @@ -53,12 +54,37 @@ export class ExtensionRegistry {
return this.data[extensionPoint.identifier] ?? null;
}

public addAll(extensionRegistry: ExtensionRegistry): void {
public addAll(
extensionRegistry: ExtensionRegistry,
extensionRegistryMergeStrategy: ExtensionRegistryMergeStrategy
): void {
Object.entries(extensionRegistry.components).forEach((entry) => {
this.components[entry[0]] = entry[1];
let extensionPointIdentifier = entry[0];
let existingValues = this.components[extensionPointIdentifier];
let newValues = entry[1];
if (existingValues) {
this.components[extensionPointIdentifier] = extensionRegistryMergeStrategy.mergeComponentExtensions(
extensionPointIdentifier,
existingValues,
newValues
);
} else {
this.components[extensionPointIdentifier] = newValues;
}
});
Object.entries(extensionRegistry.data).forEach((entry) => {
this.data[entry[0]] = entry[1];
let extensionPointIdentifier = entry[0];
let existingValues = this.data[extensionPointIdentifier];
let newValues = entry[1];
if (existingValues) {
this.data[extensionPointIdentifier] = extensionRegistryMergeStrategy.mergeDataExtensions(
extensionPointIdentifier,
existingValues,
newValues
);
} else {
this.data[extensionPointIdentifier] = newValues;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ export interface DataExtensionPoint<P> {
}

export interface ComponentExtension<P> {
identifier: string;
Component: React.ComponentType<P>;
}

export interface DataExtension<P> {
identifier: string;
data: P;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/

import { ComponentExtension, DataExtension } from './ExtensionRegistry.types';

export interface ExtensionRegistryMergeStrategy {
mergeComponentExtensions(
identifier: string,
existingValues: ComponentExtension<any>[],
newValues: ComponentExtension<any>[]
): ComponentExtension<any>[];
mergeDataExtensions(
identifier: string,
existingValue: DataExtension<any>,
newValue: DataExtension<any>
): DataExtension<any>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function useComponent<P>(extensionPoint: ComponentExtensionPoint<P>): Com

if (!componentExtension) {
const componentExtension: ComponentExtension<P> = {
identifier: 'fallback_' + extensionPoint.identifier,
Component: extensionPoint.FallbackComponent,
};
return componentExtension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ export function useData<P>(extensionPoint: DataExtensionPoint<P>): DataExtension
if (dataExtension) {
return dataExtension as DataExtension<P>;
}
return { data: extensionPoint.fallback };
return {
identifier: 'fallback_' + extensionPoint.identifier,
data: extensionPoint.fallback,
};
}
1 change: 1 addition & 0 deletions packages/core/frontend/sirius-components-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
type DataExtension,
type DataExtensionPoint,
} from './extension/ExtensionRegistry.types';
export { type ExtensionRegistryMergeStrategy } from './extension/ExtensionRegistryMergeStrategy';
export { useComponent } from './extension/useComponent';
export { useComponents } from './extension/useComponents';
export { useData } from './extension/useData';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
* Obeo - initial API and implementation
*******************************************************************************/
import {
ComponentExtension,
ConfirmationDialogContextProvider,
DataExtension,
ExtensionProvider,
ExtensionRegistry,
ExtensionRegistryMergeStrategy,
RepresentationPathContext,
ServerContext,
WorkbenchViewContribution,
Expand Down Expand Up @@ -59,10 +62,24 @@ const style = {
minHeight: '100vh',
};

class DefaultExtensionRegistryMergeStrategy implements ExtensionRegistryMergeStrategy {
public mergeComponentExtensions(
_: string,
existingValues: ComponentExtension<any>[],
newValues: ComponentExtension<any>[]
): ComponentExtension<any>[] {
return [...existingValues, ...newValues];
}
public mergeDataExtensions(_1: string, _2: DataExtension<any>, newValue: DataExtension<any>): DataExtension<any> {
return newValue;
}
}

export const SiriusWebApplication = ({
httpOrigin,
wsOrigin,
extensionRegistry,
extensionRegistryMergeStrategy,
theme,
children,
}: SiriusWebApplicationProps) => {
Expand Down Expand Up @@ -117,10 +134,19 @@ export const SiriusWebApplication = ({
];

const internalExtensionRegistry = new ExtensionRegistry();
internalExtensionRegistry.addComponent(workbenchMainAreaExtensionPoint, { Component: OnboardArea });
internalExtensionRegistry.putData(workbenchViewContributionExtensionPoint, { data: workbenchViewContributions });
internalExtensionRegistry.addComponent(workbenchMainAreaExtensionPoint, {
identifier: 'sw_onboard',
Component: OnboardArea,
});
internalExtensionRegistry.putData(workbenchViewContributionExtensionPoint, {
identifier: 'sw_workbenchView',
data: workbenchViewContributions,
});
if (extensionRegistry) {
internalExtensionRegistry.addAll(extensionRegistry);
internalExtensionRegistry.addAll(
extensionRegistry,
extensionRegistryMergeStrategy ? extensionRegistryMergeStrategy : new DefaultExtensionRegistryMergeStrategy()
);
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
import { ExtensionRegistry } from '@eclipse-sirius/sirius-components-core';
import { ExtensionRegistry, ExtensionRegistryMergeStrategy } from '@eclipse-sirius/sirius-components-core';
import { Theme } from '@material-ui/core/styles';
import { ReactNode } from 'react';

export interface SiriusWebApplicationProps {
httpOrigin: string;
wsOrigin: string;
extensionRegistry?: ExtensionRegistry;
extensionRegistryMergeStrategy?: ExtensionRegistryMergeStrategy;
theme?: Theme;
children?: ReactNode;
}

0 comments on commit 9def101

Please sign in to comment.