Skip to content

Commit

Permalink
Improved stoppable command.
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bicker <[email protected]>
  • Loading branch information
jbicker authored and spoenemann committed Feb 21, 2024
1 parent 48de74d commit a0bbb68
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/sprotty/src/base/commands/command-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
4 changes: 3 additions & 1 deletion packages/sprotty/src/base/commands/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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';
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/sprotty/src/features/move/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a0bbb68

Please sign in to comment.