Skip to content

Commit 86e89a1

Browse files
Avoid executing unnecessary operations (#105)
- Filter empty change bounds and change routing points operations - Filter empty compound commands - Ensure we only submit a new model if actual changes were applied Relates to eclipse-glsp/glsp#1477
1 parent 079fa99 commit 86e89a1

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

packages/server/src/common/gmodel/change-bounds-operation-handler.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
********************************************************************************/
16-
import { GModelRoot, GNode } from '@eclipse-glsp/graph';
17-
import { ChangeBoundsOperation, Dimension, MaybePromise, Point } from '@eclipse-glsp/protocol';
16+
import { GModelRoot, GNode, isGBoundsAware } from '@eclipse-glsp/graph';
17+
import { ChangeBoundsOperation, Dimension, ElementAndBounds, MaybePromise, Point } from '@eclipse-glsp/protocol';
1818
import { injectable } from 'inversify';
1919
import { Command } from '../command/command';
2020
import { GModelOperationHandler } from './gmodel-operation-handler';
@@ -27,7 +27,23 @@ export class GModelChangeBoundsOperationHandler extends GModelOperationHandler {
2727
operationType = ChangeBoundsOperation.KIND;
2828

2929
createCommand(operation: ChangeBoundsOperation): MaybePromise<Command | undefined> {
30-
return this.commandOf(() => this.executeChangeBounds(operation));
30+
const newBounds = operation.newBounds.filter(element => this.hasChanged(element));
31+
if (newBounds.length === 0) {
32+
return undefined;
33+
}
34+
return this.commandOf(() => this.executeChangeBounds({ ...operation, newBounds }));
35+
}
36+
37+
protected hasChanged(element: ElementAndBounds): boolean {
38+
const knownElement = this.modelState.index.find(element.elementId);
39+
if (!knownElement || !isGBoundsAware(knownElement)) {
40+
return true;
41+
}
42+
const sizeChanged = knownElement.size ? !Dimension.equals(knownElement.size, element.newSize) : true;
43+
if (sizeChanged) {
44+
return true;
45+
}
46+
return knownElement.position && element.newPosition ? !Point.equals(knownElement.position, element.newPosition) : true;
3147
}
3248

3349
protected executeChangeBounds(operation: ChangeBoundsOperation): void {

packages/server/src/common/gmodel/change-routing-points-operation-handler.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
********************************************************************************/
1616

17-
import { ChangeRoutingPointsOperation, MaybePromise } from '@eclipse-glsp/protocol';
17+
import { GEdge } from '@eclipse-glsp/graph';
18+
import { ChangeRoutingPointsOperation, ElementAndRoutingPoints, MaybePromise, Point } from '@eclipse-glsp/protocol';
1819
import { injectable } from 'inversify';
1920
import { Command } from '../command/command';
20-
import { GLSPServerError } from '../utils/glsp-server-error';
2121
import { applyRoutingPoints } from '../utils/layout-util';
2222
import { GModelOperationHandler } from './gmodel-operation-handler';
2323

@@ -26,14 +26,27 @@ export class GModelChangeRoutingPointsOperationHandler extends GModelOperationHa
2626
operationType = ChangeRoutingPointsOperation.KIND;
2727

2828
createCommand(operation: ChangeRoutingPointsOperation): MaybePromise<Command | undefined> {
29-
return this.commandOf(() => this.executeChangeRoutingPoints(operation));
29+
const newRoutingPoints = operation.newRoutingPoints.filter(newRoutingPoints => this.hasChanged(newRoutingPoints));
30+
if (newRoutingPoints.length === 0) {
31+
return undefined;
32+
}
33+
return this.commandOf(() => this.executeChangeRoutingPoints({ ...operation, newRoutingPoints }));
3034
}
3135

32-
executeChangeRoutingPoints(operation: ChangeRoutingPointsOperation): MaybePromise<void> {
33-
if (!operation.newRoutingPoints) {
34-
throw new GLSPServerError('Incomplete change routingPoints action');
36+
protected hasChanged(element: ElementAndRoutingPoints): boolean {
37+
const knownElement = this.modelState.index.findByClass(element.elementId, GEdge);
38+
if (!knownElement || knownElement.routingPoints.length !== element.newRoutingPoints?.length) {
39+
return true;
40+
}
41+
for (let i = 0; i < knownElement.routingPoints.length; i++) {
42+
if (!Point.equals(knownElement.routingPoints[i], element.newRoutingPoints[i])) {
43+
return true;
44+
}
3545
}
46+
return false;
47+
}
3648

49+
executeChangeRoutingPoints(operation: ChangeRoutingPointsOperation): MaybePromise<void> {
3750
const index = this.modelState.index;
3851
operation.newRoutingPoints.forEach(routingPoints => applyRoutingPoints(routingPoints, index));
3952
}

packages/server/src/common/operations/compound-operation-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ export class CompoundOperationHandler extends OperationHandler {
4040
commands.push(command);
4141
}
4242
}
43-
return new CompoundCommand(commands);
43+
return commands.length > 0 ? new CompoundCommand(commands) : undefined;
4444
}
4545
}

packages/server/src/common/operations/operation-action-handler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ export class OperationActionHandler implements ActionHandler {
6767
const command = await handler.execute(operation);
6868
if (command) {
6969
await this.executeCommand(command);
70+
return this.submitModel();
7071
}
71-
return this.modelSubmissionHandler.submitModel('operation');
72+
return [];
7273
}
7374

7475
protected async executeCommand(command: Command): Promise<void> {

0 commit comments

Comments
 (0)