Skip to content

Commit

Permalink
Ensure that recording commands can be executed asychronous (#40)
Browse files Browse the repository at this point in the history
Change signature of `applyPatch` and `getJsonObject` to allso allow promise return values
Also: add missing exports to main index
  • Loading branch information
tortmayr authored Mar 3, 2023
1 parent 44aae0c commit 385269d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
22 changes: 11 additions & 11 deletions packages/server/src/common/command/recording-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export abstract class AbstractRecordingCommand<JsonObject extends AnyObject> imp
protected redoPatch?: jsonPatch.Operation[];

async execute(): Promise<void> {
const beforeState = this.deepClone(this.getJsonObject());
const beforeState = this.deepClone(await this.getJsonObject());
await this.doExecute();
const afterState = this.getJsonObject();
const afterState = await this.getJsonObject();
this.undoPatch = jsonPatch.compare(afterState, beforeState);
this.redoPatch = jsonPatch.compare(beforeState, afterState);
}
Expand All @@ -41,14 +41,14 @@ export abstract class AbstractRecordingCommand<JsonObject extends AnyObject> imp
* right after {@link AbstractRecordingCommand.doExecute} to derive the undo & redo patches.
* @returns The current state of the JSON object
*/
protected abstract getJsonObject(): JsonObject;
protected abstract getJsonObject(): MaybePromise<JsonObject>;

/**
* The actual execution i.e. series of changes applied to the JSOn object that should be captured.
*/
protected abstract doExecute(): MaybePromise<void>;

protected applyPatch(object: JsonObject, patch: jsonPatch.Operation[]): void {
protected applyPatch(object: JsonObject, patch: jsonPatch.Operation[]): MaybePromise<void> {
jsonPatch.applyPatch(object, patch, false, true);
}

Expand All @@ -62,15 +62,15 @@ export abstract class AbstractRecordingCommand<JsonObject extends AnyObject> imp
return jsonPatch.deepClone(object);
}

undo(): void {
async undo(): Promise<void> {
if (this.undoPatch) {
this.applyPatch(this.getJsonObject(), this.undoPatch);
return this.applyPatch(await this.getJsonObject(), this.undoPatch);
}
}

redo(): void {
async redo(): Promise<void> {
if (this.redoPatch) {
this.applyPatch(this.getJsonObject(), this.redoPatch);
return this.applyPatch(await this.getJsonObject(), this.redoPatch);
}
}

Expand All @@ -88,7 +88,7 @@ export class RecordingCommand<JsonObject extends AnyObject = AnyObject> extends
super();
}

protected getJsonObject(): JsonObject {
protected getJsonObject(): MaybePromise<JsonObject> {
return this.jsonObject;
}
}
Expand All @@ -107,11 +107,11 @@ export class GModelRecordingCommand extends AbstractRecordingCommand<GModelRootS
this.modelState.index.indexRoot(this.modelState.root);
}

protected getJsonObject(): GModelRootSchema {
protected getJsonObject(): MaybePromise<GModelRootSchema> {
return this.serializer.createSchema(this.modelState.root);
}

protected override applyPatch(rootSchema: GModelRootSchema, patch: jsonPatch.Operation[]): void {
protected override applyPatch(rootSchema: GModelRootSchema, patch: jsonPatch.Operation[]): MaybePromise<void> {
super.applyPatch(rootSchema, patch);
const newRoot = this.serializer.createRoot(rootSchema);
this.modelState.updateRoot(newRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { GModelRoot } from '@eclipse-glsp/graph';
import { Action, ComputedBoundsAction, MaybePromise } from '@eclipse-glsp/protocol';
import { inject, injectable } from 'inversify';
import { ActionHandler } from '../../actions/action-handler';
import { applyAlignment, applyBounds, applyRoute } from '../../utils/layout-util';
import { applyAlignment, applyElementAndBounds, applyRoute } from '../../utils/layout-util';
import { ModelState } from '../model/model-state';
import { ModelSubmissionHandler } from '../model/model-submission-handler';

Expand Down Expand Up @@ -47,7 +47,7 @@ export class ComputedBoundsActionHandler implements ActionHandler {

protected applyBounds(root: GModelRoot, action: ComputedBoundsAction): void {
const index = this.modelState.index;
action.bounds.forEach(bounds => applyBounds(bounds, index));
action.bounds.forEach(bounds => applyElementAndBounds(bounds, index));
(action.alignments ?? []).forEach(alignment => applyAlignment(alignment, index));
(action.routes ?? []).forEach(route => applyRoute(route, index));
}
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export * from './actions/client-action-handler';
export * from './actions/global-action-provider';
export * from './command/command';
export * from './command/command-stack';
export * from './command/recording-command';
export * from './command/undo-redo-action-handler';
export * from './di/binding-target';
export * from './di/client-session-module';
export * from './di/diagram-module';
Expand Down Expand Up @@ -65,7 +67,6 @@ export * from './features/popup/popup-model-factory';
export * from './features/popup/request-popup-model-action-handler';
export * from './features/validation/model-validator';
export * from './features/validation/request-markers-handler';
export * from './gmodel/cut-operation-handler';
export * from './gmodel/index';
export * from './launch/glsp-server-launcher';
export * from './operations/compound-operation-handler';
Expand All @@ -84,6 +85,7 @@ export * from './session/client-session-manager';
export * from './utils/args-util';
export * from './utils/client-options-util';
export * from './utils/glsp-server-error';
export * from './utils/layout-util';
export * from './utils/logger';
export * from './utils/promise-queue';
export * from './utils/registry';
2 changes: 1 addition & 1 deletion packages/server/src/common/utils/layout-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { getOrThrow, GLSPServerError } from './glsp-server-error';
* @param index The model index.
* @returns The changed element.
*/
export function applyBounds(bounds: ElementAndBounds, index: GModelIndex): GBoundsAware | undefined {
export function applyElementAndBounds(bounds: ElementAndBounds, index: GModelIndex): GBoundsAware | undefined {
const element = getOrThrow(index.get(bounds.elementId), 'Model element not found! ID: ' + bounds.elementId);
if (isGBoundsAware(element)) {
if (bounds.newPosition !== undefined) {
Expand Down

0 comments on commit 385269d

Please sign in to comment.