Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smaller improvements in sprotty-protocol #409

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/sprotty-protocol/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Action } from './actions';
import { DiagramServer } from './diagram-server';
import { DiagramState } from './diagram-services';

export type ServerActionHandler<A extends Action = Action> = (action: A, state: DiagramState, server: DiagramServer) => Promise<void>;
export type ServerActionHandler<A extends Action = Action> = (action: A, state: DiagramState, server: DiagramServer) => void | Promise<void>;

/**
* Use this service to register handlers to specific actions. The `DiagramServer` queries this registry
Expand Down
4 changes: 2 additions & 2 deletions packages/sprotty-protocol/src/diagram-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ export class DiagramServer {
// Find a matching action handler in the registry
const handlers = this.actionHandlerRegistry?.getHandler(action.kind);
if (handlers && handlers.length === 1) {
return handlers[0](action, this.state, this);
return handlers[0](action, this.state, this) ?? Promise.resolve();
} else if (handlers && handlers.length > 1) {
return Promise.all(handlers.map(h => h(action, this.state, this))) as Promise<any>;
return Promise.all(handlers.map(h => h(action, this.state, this) ?? Promise.resolve())) as Promise<any>;
}
// If no handler is registered, call one of the default handling methods
switch (action.kind) {
Expand Down
7 changes: 4 additions & 3 deletions packages/sprotty-protocol/src/diagram-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export type DiagramOptions = JsonMap;
* The current state captured by a `DiagramServer`.
*/
export interface DiagramState {
options?: DiagramOptions;
currentRoot: SModelRoot;
revision: number;
options?: DiagramOptions
currentRoot: SModelRoot
revision: number
index?: SModelIndex
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/sprotty-protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

export * from './action-handler';
export * from './actions';
export * from './diagram-server';
export * from './diagram-services';
Expand Down
14 changes: 9 additions & 5 deletions packages/sprotty-protocol/src/utils/model-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ export function getSubType(schema: { type: string }): string {
* `SModelIndex` might be more effective.
*/
export function findElement(parent: SModelElement, elementId: string): SModelElement | undefined {
if (parent.id === elementId)
if (parent.id === elementId) {
return parent;
if (parent.children !== undefined) {
}
if (parent.children) {
for (const child of parent.children) {
const result = findElement(child, elementId);
if (result !== undefined)
if (result !== undefined) {
return result;
}
}
}
return undefined;
Expand All @@ -105,7 +107,7 @@ export class SModelIndex {
private readonly id2element: Map<string, SModelElement> = new Map();
private id2parent: Map<string, SModelElement> = new Map();

add(element: SModelElement): void {
add(element: SModelElement): this {
if (!element.id) {
throw new Error("Model element has no ID.");
} else if (this.contains(element)) {
Expand All @@ -118,16 +120,18 @@ export class SModelIndex {
this.id2parent.set(child.id, element);
}
}
return this;
}

remove(element: SModelElement): void {
remove(element: SModelElement): this {
this.id2element.delete(element.id);
if (Array.isArray(element.children)) {
for (const child of element.children) {
this.id2parent.delete(child.id);
this.remove(child as any);
}
}
return this;
}

contains(element: SModelElement): boolean {
Expand Down