Skip to content

Commit

Permalink
Further fixing: Avoid circular dependency with contribution provider
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-fleck-at committed Mar 11, 2024
1 parent f361c07 commit bf32b58
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 16 deletions.
43 changes: 41 additions & 2 deletions packages/client/src/base/action-handler-registry.ts
Expand Up @@ -14,17 +14,56 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { injectable } from 'inversify';
import { ActionHandlerRegistry } from '@eclipse-glsp/sprotty';
import { ContributionProvider } from '@eclipse-glsp/protocol/lib/utils/contribution-provider';
import { ActionHandlerRegistration, ActionHandlerRegistry, IActionHandler, IActionHandlerInitializer, TYPES } from '@eclipse-glsp/sprotty';
import { inject, injectable, named } from 'inversify';

@injectable()
export class GLSPActionHandlerRegistry extends ActionHandlerRegistry {
@inject(ContributionProvider)
@named(TYPES.ActionHandlerRegistration)
protected readonly registrations: ContributionProvider<ActionHandlerRegistration>;

@inject(ContributionProvider)
@named(TYPES.IActionHandlerInitializer)
protected readonly initializers: ContributionProvider<IActionHandlerInitializer>;

protected initialized = false;

constructor() {
super([], []);
}

protected init(): void {
if (!this.initialized) {
this.initialized = true;
this.registrations.getContributions().forEach(registration => this.register(registration.actionKind, registration.factory()));
this.initializers.getContributions().forEach(initializer => this.initializeActionHandler(initializer));
}
}

override register(key: string, instance: IActionHandler): void {
this.init();
super.register(key, instance);
}

override get(key: string): IActionHandler[] {
this.init();
return super.get(key);
}

override initializeActionHandler(initializer: IActionHandlerInitializer): void {
this.init();
super.initializeActionHandler(initializer);
}

/**
* Retrieve a set of all action kinds for which (at least) one
* handler is registered
* @returns the set of handled action kinds
*/
getHandledActionKinds(): string[] {
this.init();
return Array.from(this.elements.keys());
}
}
4 changes: 4 additions & 0 deletions packages/client/src/base/default.module.ts
Expand Up @@ -13,6 +13,7 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { bindContributionProvider } from '@eclipse-glsp/protocol/lib/utils/contribution-provider';
import {
ActionHandlerRegistry,
FeatureModule,
Expand Down Expand Up @@ -87,6 +88,9 @@ export const defaultModule = new FeatureModule((bind, unbind, isBound, rebind, .
bindOrRebind(context, TYPES.ICommandStack).to(GLSPCommandStack).inSingletonScope();
bind(GLSPActionDispatcher).toSelf().inSingletonScope();
bindOrRebind(context, TYPES.IActionDispatcher).toService(GLSPActionDispatcher);

bindContributionProvider(bind, TYPES.ActionHandlerRegistration);
bindContributionProvider(bind, TYPES.IActionHandlerInitializer);
bind(GLSPActionHandlerRegistry).toSelf().inSingletonScope();
bindOrRebind(context, ActionHandlerRegistry).toService(GLSPActionHandlerRegistry);

Expand Down
Expand Up @@ -89,11 +89,7 @@ export class FeedbackActionDispatcher implements IFeedbackActionDispatcher {
@inject(TYPES.IActionDispatcherProvider) protected actionDispatcher: () => Promise<IActionDispatcher>;
@inject(TYPES.ILogger) protected logger: ILogger;

protected actionHandlerRegistry?: ActionHandlerRegistry;

constructor(@inject(TYPES.ActionHandlerRegistryProvider) actionHandlerRegistryProvider: () => Promise<ActionHandlerRegistry>) {
actionHandlerRegistryProvider().then(registry => (this.actionHandlerRegistry = registry));
}
@inject(ActionHandlerRegistry) protected actionHandlerRegistry: ActionHandlerRegistry;

registerFeedback(feedbackEmitter: IFeedbackEmitter, feedbackActions: Action[], cleanupActions?: Action[] | undefined): Disposable {
if (feedbackActions.length > 0) {
Expand Down
10 changes: 1 addition & 9 deletions packages/client/src/base/feedback/update-model-command.ts
Expand Up @@ -14,7 +14,6 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import {
ActionHandlerRegistry,
Animation,
CommandExecutionContext,
CommandReturn,
Expand Down Expand Up @@ -43,15 +42,8 @@ export class FeedbackAwareUpdateModelCommand extends UpdateModelCommand {
@optional()
protected feedbackActionDispatcher: IFeedbackActionDispatcher;

protected actionHandlerRegistry?: ActionHandlerRegistry;

constructor(
@inject(TYPES.Action) action: UpdateModelAction,
@inject(TYPES.ActionHandlerRegistryProvider)
actionHandlerRegistryProvider: () => Promise<ActionHandlerRegistry>
) {
constructor(@inject(TYPES.Action) action: UpdateModelAction) {
super({ animate: true, ...action });
actionHandlerRegistryProvider().then(registry => (this.actionHandlerRegistry = registry));
}

protected override performUpdate(oldRoot: GModelRoot, newRoot: GModelRoot, context: CommandExecutionContext): CommandReturn {
Expand Down
78 changes: 78 additions & 0 deletions packages/protocol/src/utils/contribution-provider.ts
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (C) 2017 TypeFox 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
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
*******************************************************************************/
// from https://github.com/eclipse-theia/theia/blob/9ff0cedff1d591b0eb4be97a05f6d992789d0a24/packages/core/src/common/contribution-provider.ts

Check warning on line 16 in packages/protocol/src/utils/contribution-provider.ts

View check run for this annotation

Jenkins - GLSP / ESLint

max-len

NORMAL: This line has a length of 142. Maximum allowed is 140. (max-len)

import { interfaces } from 'inversify';

export const ContributionProvider = Symbol('ContributionProvider');

export interface ContributionProvider<T extends object> {
/**
* @param recursive `true` if the contributions should be collected from the parent containers as well. Otherwise, `false`.
* It is `false` by default.
*/
getContributions(recursive?: boolean): T[];
}

class ContainerBasedContributionProvider<T extends object> implements ContributionProvider<T> {
protected services: T[] | undefined;

constructor(
protected readonly serviceIdentifier: interfaces.ServiceIdentifier<T>,
protected readonly container: interfaces.Container
) {}

getContributions(recursive?: boolean): T[] {
if (this.services === undefined) {
const currentServices: T[] = [];
let currentContainer: interfaces.Container | null = this.container;
// eslint-disable-next-line no-null/no-null
while (currentContainer !== null) {
if (currentContainer.isBound(this.serviceIdentifier)) {
try {
currentServices.push(...currentContainer.getAll(this.serviceIdentifier));
} catch (error) {
console.error(error);
}
}
// eslint-disable-next-line no-null/no-null
currentContainer = recursive === true ? currentContainer.parent : null;
}
this.services = currentServices;
}
return this.services;
}
}

export type Bindable = interfaces.Bind | interfaces.Container;
export namespace Bindable {
export function isContainer(arg: Bindable): arg is interfaces.Container {
return (
typeof arg !== 'function' &&
// https://github.com/eclipse-theia/theia/issues/3204#issue-371029654
// In InversifyJS `4.14.0` containers no longer have a property `guid`.
('guid' in arg || 'parent' in arg)
);
}
}

export function bindContributionProvider(bindable: Bindable, id: symbol): void {
const bindingToSyntax = Bindable.isContainer(bindable) ? bindable.bind(ContributionProvider) : bindable(ContributionProvider);
bindingToSyntax
.toDynamicValue(ctx => new ContainerBasedContributionProvider(id, ctx.container))
.inSingletonScope()
.whenTargetNamed(id);
}

0 comments on commit bf32b58

Please sign in to comment.