Skip to content

Commit

Permalink
GLSP-1254 Fix client-server action forwarding
Browse files Browse the repository at this point in the history
- Deprecate the concept of marking actions as locally dispatched by appending the `__localDispatch` property. This concept is a remainder of the 1.x API implementation and no longer necessary in 2.x. In addition, it conflicted with the default behavior of marking actions received from the server.

- Remove `ExtensionAction` bindings of `RequestClipboardDataAction` and `SetClipboardDataAction` in `vscodeCopyPasteModule`. These bindings where added on accident. As a consequence, related actions dispatched in the webview client where also forwarded back to the host extension where they were not handled.

Fixes eclipse-glsp/glsp#1254
  • Loading branch information
tortmayr committed Feb 19, 2024
1 parent 71b430d commit 2be4852
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import {
FeatureModule,
ICopyPasteHandler,
RequestClipboardDataAction,
SetClipboardDataAction,
TYPES,
bindAsService,
copyPasteModule
} from '@eclipse-glsp/client';
import { ExtensionActionKind } from '../default/extension-action-handler';
import { FeatureModule, ICopyPasteHandler, TYPES, bindAsService, copyPasteModule } from '@eclipse-glsp/client';
import { CopyPasteHandlerProvider, CopyPasteStartup } from './copy-paste-startup';

export const vscodeCopyPasteModule = new FeatureModule(
Expand All @@ -32,8 +23,6 @@ export const vscodeCopyPasteModule = new FeatureModule(
ctx => () => new Promise<ICopyPasteHandler>(resolve => resolve(ctx.container.get<ICopyPasteHandler>(TYPES.ICopyPasteHandler)))
);
bindAsService(bind, TYPES.IDiagramStartup, CopyPasteStartup);
bind(ExtensionActionKind).toConstantValue(RequestClipboardDataAction.KIND);
bind(ExtensionActionKind).toConstantValue(SetClipboardDataAction.KIND);
},
{ requires: copyPasteModule }
);
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { FeatureModule, GLSPModelSource, SelectAction, TYPES, bindAsService, defaultModule } from '@eclipse-glsp/client';
import { FeatureModule, SelectAction, TYPES, bindAsService, defaultModule } from '@eclipse-glsp/client';
import { GLSPDiagramWidget, GLSPDiagramWidgetFactory } from '../../glsp-diagram-widget';
import { ExtensionActionKind, HostExtensionActionHandler } from './extension-action-handler';
import { VsCodeGLSPModelSource } from './vscode-glsp-modelsource';

export const vscodeDefaultModule = new FeatureModule(
(bind, _unbind, _isBound, rebind) => {
bind => {
bind(GLSPDiagramWidget).toSelf().inSingletonScope();
bind(GLSPDiagramWidgetFactory).toFactory(context => () => context.container.get<GLSPDiagramWidget>(GLSPDiagramWidget));
bind(VsCodeGLSPModelSource).toSelf().inSingletonScope();
rebind(GLSPModelSource).toService(VsCodeGLSPModelSource);
bindAsService(bind, TYPES.IActionHandlerInitializer, HostExtensionActionHandler);
bind(ExtensionActionKind).toConstantValue(SelectAction.KIND);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ export class HostExtensionActionHandler implements IActionHandler, IActionHandle
if (this.actionKinds.includes(action.kind)) {
const message = {
clientId: this.diagramOptions.clientId,
action,
__localDispatch: true
action
};
this.glspClient?.sendActionMessage(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,33 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { Action, ActionMessage, GLSPModelSource, ServerAction } from '@eclipse-glsp/client';
/* eslint-disable deprecation/deprecation */
import { Action, GLSPModelSource } from '@eclipse-glsp/client';
import { injectable } from 'inversify';

/**
* A helper interface that allows the webview to mark actions that have been received directly from the vscode extension
* (i.e. they are not forwarded to the GLSP Server).
*
* @deprecated The concept of marking actions as locally dispatched `ExtensionAction`s is no longer necessary and usage is discouraged.
*/
export interface ExtensionAction extends Action {
__localDispatch: true;
}

export namespace ExtensionAction {
/**
* @deprecated The concept of marking actions as locally dispatched `ExtensionAction`s is no longer necessary and usage is discouraged.
* */
export function is(object: unknown): object is ExtensionAction {
return Action.is(object) && '__localDispatch' in object && object.__localDispatch === true;
}

/**
* Mark the given action as {@link ServerAction} by attaching the "_receivedFromServer" property
* @param action The action that should be marked as server action
*
* @deprecated The concept of marking actions as locally dispatched `ExtensionAction`s is no longer necessary and usage is discouraged.
*/
export function mark(action: Action): void {
(action as ExtensionAction).__localDispatch = true;
Expand All @@ -41,23 +49,8 @@ export namespace ExtensionAction {
/**
* Customization of the default {@link GLSPModelSource} for the vscode integration.
* Also takes locally dispatched actions (i.e. actions that are originating from or intended for the host extension) into consideration.
*
* @deprecated A customized model source is no longer necessary. Use the default {@link GLSPModelSource} instead.
*/
@injectable()
export class VsCodeGLSPModelSource extends GLSPModelSource {
protected override messageReceived(message: ActionMessage): void {
if (this.clientId !== message.clientId) {
return;
}
this.checkMessageOrigin(message);
const action = message.action;
this.logger.log(this, 'receiving', action);
this.actionDispatcher.dispatch(action);
}

protected checkMessageOrigin(message: ActionMessage): void {
const isLocalDispatch = (message as any)['__localDispatch'] ?? false;
if (!isLocalDispatch) {
ServerAction.mark(message.action);
}
}
}
export class VsCodeGLSPModelSource extends GLSPModelSource {}
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ export class WebviewEndpoint implements Disposable {
}

sendMessage(actionMessage: ActionMessage): void {
// Attach a property to mark the actionMessage as locally dispatched (i.e. not received from the serer)
(actionMessage as any).__localDispatch = true;
this.messenger.sendNotification(ActionMessageNotification, this.messageParticipant, actionMessage);
}

Expand Down

0 comments on commit 2be4852

Please sign in to comment.