From a0bbb6883ea6db46a834f51705d7957d7c84a71e Mon Sep 17 00:00:00 2001 From: Jan Bicker Date: Tue, 20 Feb 2024 16:32:43 +0000 Subject: [PATCH] Improved stoppable command. Signed-off-by: Jan Bicker --- packages/sprotty/src/base/commands/command-stack.ts | 4 ++-- packages/sprotty/src/base/commands/command.ts | 4 +++- packages/sprotty/src/features/move/move.ts | 4 ++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/sprotty/src/base/commands/command-stack.ts b/packages/sprotty/src/base/commands/command-stack.ts index 129d9be..2b0b402 100644 --- a/packages/sprotty/src/base/commands/command-stack.ts +++ b/packages/sprotty/src/base/commands/command-stack.ts @@ -218,11 +218,11 @@ export class CommandStack implements ICommandStack { // If the command implements the IStoppableCommand interface, we first need to stop the execution of the // previous command with the same action kind and then store the new command as the last stoppable command. if (isStoppableCommand(command)) { - const stoppableCommand = this.stoppableCommands.get((command as any).action.kind); + const stoppableCommand = this.stoppableCommands.get(command.stoppableCommandKey); if (stoppableCommand) { stoppableCommand.stopExecution(); } - this.stoppableCommands.set((command as any).action.kind, command); + this.stoppableCommands.set(command.stoppableCommandKey, command); } this.currentPromise = this.currentPromise.then(state => diff --git a/packages/sprotty/src/base/commands/command.ts b/packages/sprotty/src/base/commands/command.ts index 176d8a2..950254e 100644 --- a/packages/sprotty/src/base/commands/command.ts +++ b/packages/sprotty/src/base/commands/command.ts @@ -22,6 +22,7 @@ import { AnimationFrameSyncer } from "../animations/animation-frame-syncer"; import { SModelRootImpl } from "../model/smodel"; import { IModelFactory } from "../model/smodel-factory"; import { IViewer } from "../views/viewer"; +import { hasOwnProperty } from 'sprotty-protocol'; /** * A command holds the behaviour of an action. @@ -57,10 +58,11 @@ export interface ICommand { */ export interface IStoppableCommand extends ICommand { stopExecution(): void + stoppableCommandKey: string } export function isStoppableCommand(command: any): command is IStoppableCommand { - return command && 'stopExecution' in command && typeof command.stopExecution === 'function'; + return command && hasOwnProperty(command, 'stoppableCommandKey') && 'stopExecution' in command && typeof command.stopExecution === 'function'; } /** diff --git a/packages/sprotty/src/features/move/move.ts b/packages/sprotty/src/features/move/move.ts index 9dbe2e3..606d47a 100644 --- a/packages/sprotty/src/features/move/move.ts +++ b/packages/sprotty/src/features/move/move.ts @@ -70,10 +70,14 @@ export class MoveCommand extends MergeableCommand implements IStoppableCommand { protected edgeMementi: EdgeMemento[] = []; protected animation: Animation | undefined; + stoppableCommandKey: string; + constructor(@inject(TYPES.Action) protected readonly action: MoveAction) { super(); + this.stoppableCommandKey = MoveCommand.KIND; } + // stop the execution of the CompoundAnimation started below stopExecution(): void { if (this.animation) {