Skip to content

Commit

Permalink
Refactor createDiagramContainer function (#231)
Browse files Browse the repository at this point in the history
* Refactor `createDiagramContainer` function

Replace `ExcludeDescriptions` with `ModuleConfiguration` which can be used for both adding (one or more) additional modules and removing (one or more)  default modules.
Co-authored-by: Martin Fleck <[email protected]>
  • Loading branch information
tortmayr authored Mar 6, 2023
1 parent 6efef44 commit 5592553
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- [diagram] scope the styles to not break existing application layout [#209](https://github.com/eclipse-glsp/glsp-client/pull/209)
- [routing] Ensured that routes are properly re-calculated when moving a routing point [#198](https://github.com/eclipse-glsp/glsp-client/pull/198)
- [diagram] Fixed a bug in the `EditLabelUIExtension` where the diagram becomes dirt wihtout an acual change. [#766](https://github.com/eclipse-glsp/glsp/issues/766)
- [diagram] Extends `ComputedBoundsAction` definition with routing information. This enables proper forwarding of client-side computed routes to the server. [#201](https://github.com/eclipse-glsp/glsp-client/pull/201/)
- [diagram] Extends `ComputedBoundsAction` definition with routing information. This enables proper forwarding of cliend-side computed routes to the server. [#201](https://github.com/eclipse-glsp/glsp-client/pull/201/)
- [DI] The `createClientContainer` function is now deprecated. Please use `createDiagramContainer` instead. This new function also can be used with `ModuleConfigurations` which allow a more fine granular configuration by adding new modules and/or removing default modules. [#218](https://github.com/eclipse-glsp/glsp/issues/218) [#231](https://github.com/eclipse-glsp/glsp-client/pull/231)

### Breaking Changes

Expand Down
30 changes: 21 additions & 9 deletions packages/client/src/base/container-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { remove } from '@eclipse-glsp/protocol';
import { asArray, distinctAdd, MaybeArray, remove } from '@eclipse-glsp/protocol';
import { Container, ContainerModule } from 'inversify';
import {
buttonModule,
Expand Down Expand Up @@ -88,14 +88,19 @@ export const DEFAULT_MODULES = [

/**
* Creates a GLSP Diagram container with the GLSP default modules and the specified custom `modules`.
* Default modules can be excluded using {@link ExcludeDescription}s.
*
* Additional modules can be passed as direct arguments or as part of a {@link ModuleConfiguration}.
* ```typescript
* const container= createDiagramContainer(myModule1, myModule2)
* // or
* const container= createDiagramContainer({ add: [myModule1, myModule2]})
* ```
* Default modules can be excluded using {@link ModuleConfiguration}s.
* This means, you can still customize the default modules in two ways.
*
* First, you can exclude default modules and add a module with your custom code.
*
* ```typescript
* const container = createDiagramContainer(myModule1, {exclude: modelSourceWatcherModule},myModelSourceWatcherModule );
* const container = createDiagramContainer({ add:myModelSourceWatcherModule, remove: modelSourceWatcherModule} );
* ```
*
* Second, you can unbind or rebind implementations that are originally bound in one of the default modules.
Expand All @@ -108,26 +113,33 @@ export const DEFAULT_MODULES = [
* @param options Options to customize the module loading
* @returns The created container.
*/
export function createDiagramContainer(...customModules: Array<ContainerModule | ExcludeDescription>): Container {
export function createDiagramContainer(...customModules: Array<ContainerModule | ModuleConfiguration>): Container {
const container = new Container();

const modules = [...DEFAULT_MODULES];
customModules.forEach(customModule => {
if (customModule instanceof ContainerModule) {
modules.push(customModule);
} else {
remove(modules, customModule.exclude);
if (customModule.remove) {
remove(modules, ...asArray(customModule.remove));
}
if (customModule.add) {
distinctAdd(modules, ...asArray(customModule.add));
}
}
});
container.load(...modules);
return container;
}

/**
* Can be passed to the {@link createDiagramContainer} utility function to exclude (i.e. not load) a specific default module.
* Can be passed to the {@link createDiagramContainer} utility function to configure additional modules or
* remove (i.e. not load) default modules.
*/
export interface ExcludeDescription {
exclude: ContainerModule;
export interface ModuleConfiguration {
add?: MaybeArray<ContainerModule>;
remove?: MaybeArray<ContainerModule>;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion packages/protocol/src/utils/array-util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (c) 2019-2022 EclipseSource and others.
* Copyright (c) 2019-2023 EclipseSource and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -98,6 +98,17 @@ export function flatPush<T>(array: T[], toPush: MaybeArray<T>[]): void {
toPush.forEach(value => (Array.isArray(value) ? array.push(...value) : array.push(value)));
}

/**
* Helper function to convert a {@link MaybeArray} into an array.
* @param maybe The MaybeArray to convert
* @returns The corresponding array
*/
export function asArray<T>(maybe: MaybeArray<T>): T[] {
if (Array.isArray(maybe)) {
return maybe;
}
return [maybe];
}
/**
* Adds the given values to the given array. The add operation is executed distinct meaning
* a value will not be pushed to the array if its already present in the array.
Expand Down

0 comments on commit 5592553

Please sign in to comment.