Skip to content

Commit acbf000

Browse files
authored
Revert "Support asynchronous action handlers on client-side (#457)" (#459)
This reverts commit 609c2ba.
1 parent 609c2ba commit acbf000

File tree

5 files changed

+31
-180
lines changed

5 files changed

+31
-180
lines changed

packages/client/src/base/action-dispatcher.ts

Lines changed: 15 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,17 @@
1616
import {
1717
Action,
1818
ActionDispatcher,
19-
ActionHandler,
2019
ActionHandlerRegistry,
2120
Deferred,
2221
EMPTY_ROOT,
2322
GModelRoot,
2423
IActionDispatcher,
25-
RejectAction,
2624
RequestAction,
2725
ResponseAction,
2826
SetModelAction,
2927
TYPES
3028
} from '@eclipse-glsp/sprotty';
3129
import { inject, injectable } from 'inversify';
32-
import * as sprotty from 'sprotty-protocol/lib/actions';
3330
import { GLSPActionHandlerRegistry } from './action-handler-registry';
3431
import { IGModelRootListener } from './editor-context-service';
3532
import { OptionalAction } from './model/glsp-model-source';
@@ -128,96 +125,26 @@ export class GLSPActionDispatcher extends ActionDispatcher implements IGModelRoo
128125
return result;
129126
}
130127

131-
protected override async handleAction(action: Action): Promise<void> {
128+
protected override handleAction(action: Action): Promise<void> {
132129
if (ResponseAction.hasValidResponseId(action)) {
133-
return this.handleResponseAction(action);
134-
}
135-
if (OptionalAction.is(action) && !this.hasHandler(action)) {
136-
return Promise.resolve();
137-
}
138-
if (sprotty.UndoAction.KIND === action.kind) {
139-
return this.handleUndoAction(action);
140-
}
141-
if (sprotty.RedoAction.KIND === action.kind) {
142-
return this.handleRedoAction(action);
143-
}
144-
return this.doHandleAction(action);
145-
}
146-
147-
protected handleResponseAction(action: ResponseAction): Promise<void> {
148-
// clear any pending timeout
149-
const timeout = this.timeouts.get(action.responseId);
150-
if (timeout !== undefined) {
151-
clearTimeout(timeout);
152-
this.timeouts.delete(action.responseId);
153-
}
154-
155-
// check for matching request
156-
const request = this.requests.get(action.responseId);
157-
if (!request) {
158-
// treat no matching request by dispatching action normally
159-
this.logger.log(this, 'No matching request for response, dispatch normally', action);
160-
action.responseId = '';
161-
return this.handleAction(action);
162-
}
163-
164-
this.requests.delete(action.responseId);
165-
if (RejectAction.is(action)) {
166-
// translation reject action to request rejection
167-
request.reject(new Error(action.message));
168-
this.logger.warn(this, `Request with id ${action.responseId} failed.`, action.message, action.detail);
169-
} else {
170-
request.resolve(action);
171-
}
172-
return Promise.resolve();
173-
}
174-
175-
protected handleUndoAction(action: Action): Promise<void> {
176-
return this.commandStack.undo().then(() => {});
177-
}
178-
179-
protected handleRedoAction(action: Action): Promise<void> {
180-
return this.commandStack.redo().then(() => {});
181-
}
182-
183-
protected async doHandleAction(action: Action): Promise<void> {
184-
const handlers = this.actionHandlerRegistry.get(action.kind);
185-
if (handlers.length === 0) {
186-
return this.handleActionWithoutHandler(action);
187-
} else {
188-
return this.handlerActionWithHandler(action, handlers);
189-
}
190-
}
130+
// clear timeout
131+
const timeout = this.timeouts.get(action.responseId);
132+
if (timeout !== undefined) {
133+
clearTimeout(timeout);
134+
this.timeouts.delete(action.responseId);
135+
}
191136

192-
protected handleActionWithoutHandler(action: Action): Promise<void> {
193-
this.logger.warn(this, 'Missing handler for action', action);
194-
const error = new Error(`Missing handler for action '${action.kind}'`);
195-
if (RequestAction.is(action)) {
196-
const request = this.requests.get(action.requestId);
197-
if (request !== undefined) {
198-
this.requests.delete(action.requestId);
199-
request.reject(error);
137+
// Check if we have a pending request for the response.
138+
// If not the we clear the responseId => action will be dispatched normally
139+
const deferred = this.requests.get(action.responseId);
140+
if (deferred === undefined) {
141+
action.responseId = '';
200142
}
201143
}
202-
return Promise.reject(error);
203-
}
204-
205-
protected async handlerActionWithHandler(action: Action, handlers: ActionHandler[]): Promise<any> {
206-
this.logger.log(this, 'Handle', action);
207-
const handlerResults: Promise<any>[] = [];
208-
for (const handler of handlers) {
209-
const handlerResult = Promise.resolve(handler.handle(action)).then<any>(result => {
210-
if (Action.is(result)) {
211-
return this.dispatch(result);
212-
} else if (result !== undefined) {
213-
this.blockUntil = result.blockUntil;
214-
return this.commandStack.execute(result);
215-
}
216-
return undefined;
217-
});
218-
handlerResults.push(handlerResult);
144+
if (!this.hasHandler(action) && OptionalAction.is(action)) {
145+
return Promise.resolve();
219146
}
220-
return Promise.all(handlerResults) as Promise<any>;
147+
return super.handleAction(action);
221148
}
222149

223150
override request<Res extends ResponseAction>(action: RequestAction<Res>): Promise<Res> {

packages/glsp-sprotty/src/action-handler-override.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

packages/glsp-sprotty/src/api-override.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
SModelRootImpl as GModelRoot,
2323
ICommand,
2424
IActionDispatcher as SIActionDispatcher,
25+
IActionHandler as SIActionHandler,
2526
IButtonHandler as SIButtonHandler,
2627
ICommandPaletteActionProvider as SICommandPaletteActionProvider,
2728
ICommandStack as SICommandStack,
@@ -42,6 +43,14 @@ import {
4243
*
4344
*/
4445

46+
/**
47+
* An action handler accepts an action and reacts to it by returning either a command to be
48+
* executed, or another action to be dispatched.
49+
*/
50+
export interface IActionHandler extends SIActionHandler {
51+
handle(action: Action): ICommand | Action | void;
52+
}
53+
4554
export interface IButtonHandler extends SIButtonHandler {
4655
buttonPressed(button: GButton): (Action | Promise<Action>)[];
4756
}

packages/glsp-sprotty/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
*
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
********************************************************************************/
16-
export * from './action-handler-override';
1716
export * from './api-override';
1817
export * from './feature-modules';
1918
export * from './layout-override';

packages/glsp-sprotty/src/re-exports.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ export * from '@eclipse-glsp/protocol/lib/di';
2929
// export * from 'sprotty/lib/base/actions/action';
3030
// Exclude IActionDispatcher and IActionDispatcherProvider. Exported via api-override module instead
3131
export { ActionDispatcher, PostponedAction } from 'sprotty/lib/base/actions/action-dispatcher';
32-
// export * from 'sprotty/lib/base/actions/action-handler';
32+
export {
33+
ActionHandlerRegistration,
34+
ActionHandlerRegistry,
35+
configureActionHandler,
36+
IActionHandlerInitializer,
37+
onAction
38+
} from 'sprotty/lib/base/actions/action-handler';
3339
export * from 'sprotty/lib/base/actions/diagram-locker';
3440

3541
export * from 'sprotty/lib/base/animations/animation';

0 commit comments

Comments
 (0)