@eclipse-glsp/server 2.6.0-next.3 → 2.7.0-next.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/lib/browser/di/app-module.d.ts +1 -1
  2. package/lib/browser/di/app-module.d.ts.map +1 -1
  3. package/lib/browser/di/app-module.js +5 -1
  4. package/lib/browser/di/app-module.js.map +1 -1
  5. package/lib/browser/di/browser-action-dispatch-scope.d.ts +39 -0
  6. package/lib/browser/di/browser-action-dispatch-scope.d.ts.map +1 -0
  7. package/lib/browser/di/browser-action-dispatch-scope.js +77 -0
  8. package/lib/browser/di/browser-action-dispatch-scope.js.map +1 -0
  9. package/lib/browser/di/browser-dispatch-context.d.ts +39 -0
  10. package/lib/browser/di/browser-dispatch-context.d.ts.map +1 -0
  11. package/lib/browser/di/browser-dispatch-context.js +57 -0
  12. package/lib/browser/di/browser-dispatch-context.js.map +1 -0
  13. package/lib/browser/di/sync-dispatch-context.d.ts +39 -0
  14. package/lib/browser/di/sync-dispatch-context.d.ts.map +1 -0
  15. package/lib/browser/di/sync-dispatch-context.js +57 -0
  16. package/lib/browser/di/sync-dispatch-context.js.map +1 -0
  17. package/lib/browser/index.d.ts +2 -1
  18. package/lib/browser/index.d.ts.map +1 -1
  19. package/lib/browser/index.js +2 -1
  20. package/lib/browser/index.js.map +1 -1
  21. package/lib/common/actions/action-dispatcher.d.ts +107 -5
  22. package/lib/common/actions/action-dispatcher.d.ts.map +1 -1
  23. package/lib/common/actions/action-dispatcher.js +199 -26
  24. package/lib/common/actions/action-dispatcher.js.map +1 -1
  25. package/lib/common/actions/client-action-handler.d.ts +1 -1
  26. package/lib/common/actions/client-action-handler.js +2 -2
  27. package/lib/common/actions/client-action-handler.js.map +1 -1
  28. package/lib/common/actions/global-action-provider.js +1 -2
  29. package/lib/common/actions/global-action-provider.js.map +1 -1
  30. package/lib/common/di/service-identifiers.d.ts +1 -1
  31. package/lib/common/features/clipboard/request-clipboard-data-action-handler.d.ts +1 -1
  32. package/lib/common/features/clipboard/request-clipboard-data-action-handler.js +1 -2
  33. package/lib/common/features/clipboard/request-clipboard-data-action-handler.js.map +1 -1
  34. package/lib/common/features/layout/computed-bounds-action-handler.d.ts +1 -1
  35. package/lib/common/features/layout/computed-bounds-action-handler.d.ts.map +1 -1
  36. package/lib/common/features/layout/computed-bounds-action-handler.js +1 -1
  37. package/lib/common/features/layout/computed-bounds-action-handler.js.map +1 -1
  38. package/lib/common/features/model/model-submission-handler.d.ts +6 -4
  39. package/lib/common/features/model/model-submission-handler.d.ts.map +1 -1
  40. package/lib/common/features/model/model-submission-handler.js +7 -5
  41. package/lib/common/features/model/model-submission-handler.js.map +1 -1
  42. package/lib/common/features/model/request-model-action-handler.d.ts +1 -1
  43. package/lib/common/features/model/request-model-action-handler.js +1 -1
  44. package/lib/common/features/progress/progress-service.js +1 -1
  45. package/lib/common/index.d.ts +2 -1
  46. package/lib/common/index.d.ts.map +1 -1
  47. package/lib/common/index.js +2 -1
  48. package/lib/common/index.js.map +1 -1
  49. package/lib/common/protocol/glsp-server.d.ts +4 -2
  50. package/lib/common/protocol/glsp-server.d.ts.map +1 -1
  51. package/lib/common/protocol/glsp-server.js +35 -4
  52. package/lib/common/protocol/glsp-server.js.map +1 -1
  53. package/lib/common/utils/action-channel.d.ts +58 -0
  54. package/lib/common/utils/action-channel.d.ts.map +1 -0
  55. package/lib/common/utils/action-channel.js +99 -0
  56. package/lib/common/utils/action-channel.js.map +1 -0
  57. package/lib/common/utils/action-queue.d.ts +58 -0
  58. package/lib/common/utils/action-queue.d.ts.map +1 -0
  59. package/lib/common/utils/action-queue.js +99 -0
  60. package/lib/common/utils/action-queue.js.map +1 -0
  61. package/lib/common/utils/promise-queue.d.ts +5 -1
  62. package/lib/common/utils/promise-queue.d.ts.map +1 -1
  63. package/lib/common/utils/promise-queue.js +5 -1
  64. package/lib/common/utils/promise-queue.js.map +1 -1
  65. package/lib/node/di/app-module.d.ts +1 -1
  66. package/lib/node/di/app-module.d.ts.map +1 -1
  67. package/lib/node/di/app-module.js +2 -0
  68. package/lib/node/di/app-module.js.map +1 -1
  69. package/lib/node/di/async-dispatch-context.d.ts +26 -0
  70. package/lib/node/di/async-dispatch-context.d.ts.map +1 -0
  71. package/lib/node/di/async-dispatch-context.js +35 -0
  72. package/lib/node/di/async-dispatch-context.js.map +1 -0
  73. package/lib/node/di/node-action-dispatch-scope.d.ts +26 -0
  74. package/lib/node/di/node-action-dispatch-scope.d.ts.map +1 -0
  75. package/lib/node/di/node-action-dispatch-scope.js +45 -0
  76. package/lib/node/di/node-action-dispatch-scope.js.map +1 -0
  77. package/lib/node/di/node-dispatch-context.d.ts +26 -0
  78. package/lib/node/di/node-dispatch-context.d.ts.map +1 -0
  79. package/lib/node/di/node-dispatch-context.js +35 -0
  80. package/lib/node/di/node-dispatch-context.js.map +1 -0
  81. package/lib/node/index.d.ts +2 -1
  82. package/lib/node/index.d.ts.map +1 -1
  83. package/lib/node/index.js +2 -1
  84. package/lib/node/index.js.map +1 -1
  85. package/package.json +4 -4
  86. package/src/browser/di/app-module.ts +6 -2
  87. package/src/browser/di/browser-action-dispatch-scope.spec.ts +75 -0
  88. package/src/browser/di/browser-action-dispatch-scope.ts +67 -0
  89. package/src/browser/index.ts +2 -1
  90. package/src/common/actions/action-dispatcher.ts +292 -25
  91. package/src/common/actions/client-action-handler.ts +2 -2
  92. package/src/common/actions/global-action-provider.ts +2 -2
  93. package/src/common/command/recording-command.spec.ts +1 -2
  94. package/src/common/di/service-identifiers.ts +1 -1
  95. package/src/common/features/clipboard/request-clipboard-data-action-handler.ts +2 -2
  96. package/src/common/features/contextactions/context-actions-provider-registry.spec.ts +2 -2
  97. package/src/common/features/layout/computed-bounds-action-handler.ts +6 -3
  98. package/src/common/features/model/model-submission-handler.ts +8 -5
  99. package/src/common/features/model/request-model-action-handler.ts +1 -1
  100. package/src/common/features/progress/progress-service.ts +1 -1
  101. package/src/common/index.ts +2 -1
  102. package/src/common/protocol/glsp-server.ts +41 -2
  103. package/src/common/test/mock-util.ts +17 -3
  104. package/src/common/utils/action-queue.spec.ts +133 -0
  105. package/src/common/utils/action-queue.ts +107 -0
  106. package/src/common/utils/promise-queue.spec.ts +6 -2
  107. package/src/common/utils/promise-queue.ts +5 -1
  108. package/src/node/actions/action-dispatcher.spec.ts +656 -0
  109. package/src/node/di/app-module.ts +4 -2
  110. package/src/node/di/node-action-dispatch-scope.ts +35 -0
  111. package/src/node/index.ts +2 -1
  112. package/src/common/actions/action-dispatcher.spec.ts +0 -291
@@ -0,0 +1,656 @@
1
+ /********************************************************************************
2
+ * Copyright (c) 2022-2026 STMicroelectronics and others.
3
+ *
4
+ * This program and the accompanying materials are made available under the
5
+ * terms of the Eclipse Public License v. 2.0 which is available at
6
+ * http://www.eclipse.org/legal/epl-2.0.
7
+ *
8
+ * This Source Code may also be made available under the following Secondary
9
+ * Licenses when the conditions for such availability set forth in the Eclipse
10
+ * Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ * with the GNU Classpath Exception which is available at
12
+ * https://www.gnu.org/software/classpath/license.html.
13
+ *
14
+ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ ********************************************************************************/
16
+ import { Action, Deferred, RequestAction, ResponseAction, UpdateModelAction } from '@eclipse-glsp/protocol';
17
+ import { expect } from 'chai';
18
+ import { Container, ContainerModule } from 'inversify';
19
+ import * as sinon from 'sinon';
20
+ import { ActionDispatchScope, DefaultActionDispatcher } from '../../common/actions/action-dispatcher';
21
+ import { ActionHandler } from '../../common/actions/action-handler';
22
+ import { ActionHandlerRegistry } from '../../common/actions/action-handler-registry';
23
+ import { ClientActionForwarder } from '../../common/actions/client-action-handler';
24
+ import { ClientActionKinds, ClientId } from '../../common/di/service-identifiers';
25
+ import { ClientSessionManager } from '../../common/session/client-session-manager';
26
+ import * as mock from '../../common/test/mock-util';
27
+ import { Logger } from '../../common/utils/logger';
28
+ import { NodeActionDispatchScope } from '../di/node-action-dispatch-scope';
29
+
30
+ function waitSync(timeInMillis: number): void {
31
+ const start = Date.now();
32
+ let now = start;
33
+ while (now - start < timeInMillis) {
34
+ now = Date.now();
35
+ }
36
+ }
37
+
38
+ describe('test DefaultActionDispatcher', () => {
39
+ const container = new Container();
40
+ const clientId = 'myClientId';
41
+ const actionHandlerRegistry = new ActionHandlerRegistry();
42
+ let registry_get_stub: sinon.SinonStub<[string], ActionHandler[]>;
43
+ const sandbox = sinon.createSandbox();
44
+
45
+ const clientActionForwarderStub = sinon.createStubInstance(ClientActionForwarder);
46
+
47
+ container.load(
48
+ new ContainerModule(bind => {
49
+ bind(Logger).toConstantValue(new mock.StubLogger());
50
+ bind(ClientSessionManager).toConstantValue(new mock.StubClientSessionManager());
51
+ bind(ClientId).toConstantValue(clientId);
52
+ bind(ActionHandlerRegistry).toConstantValue(actionHandlerRegistry);
53
+ bind(ClientActionKinds).toConstantValue(new Set(['response', 'response1', 'response2']));
54
+ bind(ClientActionForwarder).toConstantValue(clientActionForwarderStub);
55
+ bind(ActionDispatchScope).to(NodeActionDispatchScope);
56
+ })
57
+ );
58
+ const actionDispatcher = container.resolve(DefaultActionDispatcher);
59
+
60
+ beforeEach(() => {
61
+ registry_get_stub = sandbox.stub(actionHandlerRegistry, 'get');
62
+ });
63
+
64
+ afterEach(() => {
65
+ sandbox.restore();
66
+ });
67
+
68
+ describe('test with one-way actions (no response actions)', () => {
69
+ it('dispatch- unhandled action', async () => {
70
+ mock.expectToThrowAsync(() => actionDispatcher.dispatch({ kind: 'unhandled' }));
71
+ });
72
+
73
+ it('dispatch - one action', async () => {
74
+ // Mock setup
75
+ const action = 'action';
76
+ const handler = new mock.StubActionHandler([action]);
77
+ const getHandler = (kind: string): ActionHandler[] => (kind === action ? [handler] : []);
78
+ registry_get_stub.callsFake(getHandler);
79
+ const spy_handler_execute = sinon.spy(handler, 'execute');
80
+ // Test execution
81
+ await actionDispatcher.dispatch({ kind: action });
82
+ expect(spy_handler_execute.calledOnce).true;
83
+ });
84
+
85
+ describe('test multi dispatch with single-handled actions', () => {
86
+ // Mock Setup
87
+ const action1 = 'a1';
88
+ const action2 = 'a2';
89
+ const action3 = 'a3';
90
+
91
+ const handler1 = new mock.StubActionHandler([action1]);
92
+ const handler2 = new mock.StubActionHandler([action2]);
93
+ const handler3 = new mock.StubActionHandler([action3]);
94
+
95
+ const spy_handler1_execute = sinon.stub(handler1, 'execute').returns([]);
96
+ const spy_handler2_execute = sinon.stub(handler2, 'execute').returns([]);
97
+ const spy_handler3_execute = sinon.stub(handler3, 'execute').returns([]);
98
+ const handlerMockImpl = (kind: string): ActionHandler[] => {
99
+ switch (kind) {
100
+ case action1:
101
+ return [handler1];
102
+ case action2:
103
+ return [handler2];
104
+ case action3:
105
+ return [handler3];
106
+ default:
107
+ return [];
108
+ }
109
+ };
110
+
111
+ it('dispatch - multiple actions', async () => {
112
+ // Mock setup
113
+ registry_get_stub.callsFake(handlerMockImpl);
114
+ // Test execution
115
+ actionDispatcher.dispatch({ kind: action1 });
116
+ actionDispatcher.dispatch({ kind: action2 });
117
+ await actionDispatcher.dispatch({ kind: action3 });
118
+ // Check if all handlers have been called
119
+ expect(spy_handler1_execute.called).true;
120
+ expect(spy_handler2_execute.called).true;
121
+ expect(spy_handler3_execute.called).true;
122
+ // Check if all handlers have been called in the right order
123
+ sinon.assert.callOrder(spy_handler1_execute, spy_handler2_execute, spy_handler3_execute);
124
+ });
125
+
126
+ it('dispatchAll- multiple actions', async () => {
127
+ // Mock setup
128
+ registry_get_stub.callsFake(handlerMockImpl);
129
+ // Test execution
130
+ await actionDispatcher.dispatchAll([{ kind: action1 }, { kind: action2 }, { kind: action3 }]);
131
+ // Check if all handlers have been called
132
+ expect(spy_handler1_execute.calledOnce);
133
+ expect(spy_handler2_execute.calledOnce);
134
+ expect(spy_handler3_execute.calledOnce);
135
+ // Check if all handlers have been called in the right order
136
+ sinon.assert.callOrder(spy_handler1_execute, spy_handler2_execute, spy_handler3_execute);
137
+ });
138
+
139
+ it('dispatch - multiple actions (racing execution times)', async () => {
140
+ // Mock setup
141
+ registry_get_stub.callsFake(handlerMockImpl);
142
+ spy_handler1_execute.callsFake((_action: Action) => {
143
+ waitSync(500);
144
+ return [];
145
+ });
146
+ spy_handler2_execute.callsFake((_action: Action) => {
147
+ waitSync(200);
148
+ return [];
149
+ });
150
+ spy_handler3_execute.callsFake((_action: Action) => {
151
+ waitSync(100);
152
+ return [];
153
+ });
154
+ // Test execution
155
+ actionDispatcher.dispatch({ kind: action1 });
156
+ actionDispatcher.dispatch({ kind: action2 });
157
+ await actionDispatcher.dispatch({ kind: action3 });
158
+ // Check if all handlers have been called
159
+ expect(spy_handler1_execute.calledOnce);
160
+ expect(spy_handler2_execute.calledOnce);
161
+ expect(spy_handler3_execute.calledOnce);
162
+ // Check if all handlers have been called in the right order
163
+ sinon.assert.callOrder(spy_handler1_execute, spy_handler2_execute, spy_handler3_execute);
164
+ });
165
+ });
166
+
167
+ it('dispatch- one action & multiple handlers', async () => {
168
+ // Mock setup
169
+ const action1 = 'a1';
170
+
171
+ const handler1 = new mock.StubActionHandler([action1]);
172
+ const handler2 = new mock.StubActionHandler([action1]);
173
+
174
+ registry_get_stub.callsFake((kind: string) => (kind === action1 ? [handler1, handler2] : []));
175
+ const spy_handler1_execute = sinon.spy(handler1, 'execute');
176
+ const spy_handler2_execute = sinon.spy(handler2, 'execute');
177
+ // Test execution
178
+ await actionDispatcher.dispatch({ kind: action1 });
179
+ expect(spy_handler1_execute.calledOnce);
180
+ expect(spy_handler2_execute.calledOnce);
181
+ sinon.assert.callOrder(spy_handler1_execute, spy_handler2_execute);
182
+ });
183
+ });
184
+
185
+ describe('test with handler response actions ', () => {
186
+ it('dispatch - one action & one handler response action', async () => {
187
+ // Mock setup
188
+ const request = 'request';
189
+ const response = 'response';
190
+
191
+ const requestHandler = new mock.StubActionHandler([request]);
192
+ const responseHandler = new mock.StubActionHandler([response]);
193
+
194
+ const spy_requestHandler_execute = sinon.stub(requestHandler, 'execute').returns([{ kind: response }]);
195
+ const spy_responseHandler_execute = sinon.spy(responseHandler, 'execute');
196
+ registry_get_stub.callsFake((kind: string) => {
197
+ switch (kind) {
198
+ case request:
199
+ return [requestHandler];
200
+ case response:
201
+ return [responseHandler];
202
+ default:
203
+ return [];
204
+ }
205
+ });
206
+ // Test execution
207
+ await actionDispatcher.dispatch({ kind: request });
208
+ // Add a delay so that the action dispatcher has time to dispatch the handler response
209
+ await mock.delay(200);
210
+ // Check if all handlers have been called
211
+ expect(spy_requestHandler_execute.calledOnce);
212
+ expect(spy_responseHandler_execute.calledOnce);
213
+ });
214
+
215
+ it('dispatch - multiple actions & multiple response', async () => {
216
+ // Mock setup
217
+ const request1 = 'request1';
218
+ const request2 = 'request2';
219
+ const response1 = 'response1';
220
+ const response2 = 'response2';
221
+
222
+ const responseHandler1 = new mock.StubActionHandler([response1]);
223
+ const responseHandler2 = new mock.StubActionHandler([response2]);
224
+ const requestHandler1 = new mock.StubActionHandler([request1]);
225
+ const requestHandler2 = new mock.StubActionHandler([request2]);
226
+
227
+ const spy_requestHandler1_execute = sinon.stub(requestHandler1, 'execute').returns([{ kind: response1 }, { kind: response2 }]);
228
+ const spy_requestHandler2_execute = sinon.stub(requestHandler2, 'execute').returns([{ kind: response2 }]);
229
+ const spy_responseHandler1_execute = sinon.spy(responseHandler1, 'execute');
230
+ const spy_responseHandler2_execute = sinon.spy(responseHandler2, 'execute');
231
+ registry_get_stub.callsFake((kind: string) => {
232
+ switch (kind) {
233
+ case request1:
234
+ return [requestHandler1];
235
+ case request2:
236
+ return [requestHandler2];
237
+ case response1:
238
+ return [responseHandler1, responseHandler2];
239
+ case response2:
240
+ return [responseHandler2];
241
+ default:
242
+ return [];
243
+ }
244
+ });
245
+ // Test execution
246
+ actionDispatcher.dispatch({ kind: request1 });
247
+ await actionDispatcher.dispatch({ kind: request2 });
248
+
249
+ // Add a delay so that the action dispatcher has time to dispatch the handler response
250
+ await mock.delay(100);
251
+ // Check if all handlers have been called correctly
252
+ expect(spy_requestHandler1_execute.calledOnce);
253
+ expect(spy_requestHandler2_execute.calledOnce);
254
+ expect(spy_responseHandler1_execute.calledOnce);
255
+ expect(spy_responseHandler2_execute.calledThrice);
256
+ // Check if all handlers have been called in the right order
257
+ sinon.assert.callOrder(spy_requestHandler1_execute, spy_requestHandler2_execute);
258
+ sinon.assert.callOrder(spy_responseHandler1_execute, spy_responseHandler2_execute);
259
+ });
260
+ });
261
+
262
+ describe('test dispatch after next update', () => {
263
+ it('dispatchAfterNextUpdate', async () => {
264
+ // Mock setup
265
+ const updateModelAction = UpdateModelAction.create({ id: 'newRoot', type: 'myType' });
266
+ const intermediateAction = 'intermediate';
267
+ const postUpdateAction = 'postUpdate';
268
+ const handler = new mock.StubActionHandler([updateModelAction.kind, intermediateAction]);
269
+ const postUpdateHandler = new mock.StubActionHandler([postUpdateAction]);
270
+
271
+ const getHandler = (kind: string): ActionHandler[] => {
272
+ if (kind === updateModelAction.kind || kind === intermediateAction) {
273
+ return [handler];
274
+ } else if (kind === postUpdateAction) {
275
+ return [postUpdateHandler];
276
+ }
277
+
278
+ return [];
279
+ };
280
+ registry_get_stub.callsFake(getHandler);
281
+ const spy_postUpdateHandler_execute = sinon.spy(postUpdateHandler, 'execute');
282
+
283
+ // Test execution
284
+ actionDispatcher.dispatchAfterNextUpdate({ kind: postUpdateAction });
285
+ expect(spy_postUpdateHandler_execute.called).to.be.false;
286
+ await actionDispatcher.dispatch({ kind: intermediateAction });
287
+ expect(spy_postUpdateHandler_execute.called).to.be.false;
288
+ await actionDispatcher.dispatch(updateModelAction);
289
+ expect(spy_postUpdateHandler_execute.calledOnce);
290
+ // Check that action does not get dispatched again
291
+ await actionDispatcher.dispatch(updateModelAction);
292
+ expect(spy_postUpdateHandler_execute.calledOnce);
293
+ });
294
+ });
295
+
296
+ describe('test request/response', () => {
297
+ it('request - resolves when matching response is dispatched', async () => {
298
+ const requestAction: RequestAction<ResponseAction> = {
299
+ kind: 'testRequest',
300
+ requestId: 'req_1'
301
+ };
302
+ const responseAction: ResponseAction = {
303
+ kind: 'testResponse',
304
+ responseId: 'req_1'
305
+ };
306
+
307
+ // Configure forwarder: testRequest is forwarded to the client
308
+ clientActionForwarderStub.shouldForwardToClient.callsFake(action => action.kind === 'testRequest');
309
+ clientActionForwarderStub.handle.callsFake(action => action.kind === 'testRequest');
310
+ registry_get_stub.callsFake(() => []);
311
+
312
+ const responsePromise = actionDispatcher.request(requestAction);
313
+ await actionDispatcher.dispatch(responseAction);
314
+
315
+ const result = await responsePromise;
316
+ expect(result.responseId).to.equal('req_1');
317
+ });
318
+
319
+ it('request - response bypasses queue even when queue is busy', async () => {
320
+ const requestAction: RequestAction<ResponseAction> = {
321
+ kind: 'testRequest',
322
+ requestId: 'req_deadlock'
323
+ };
324
+ const responseAction: ResponseAction = {
325
+ kind: 'testResponse',
326
+ responseId: 'req_deadlock'
327
+ };
328
+
329
+ clientActionForwarderStub.shouldForwardToClient.callsFake(action => action.kind === 'testRequest');
330
+ clientActionForwarderStub.handle.callsFake(action => action.kind === 'testRequest');
331
+
332
+ const handlerRunning = new Deferred<void>();
333
+
334
+ const slowAction = 'slowAction';
335
+ const slowHandler = new mock.StubActionHandler([slowAction]);
336
+ slowHandler.execute = async () => {
337
+ const resultPromise = actionDispatcher.request(requestAction);
338
+ handlerRunning.resolve();
339
+ const result = await resultPromise;
340
+ expect(result.responseId).to.equal('req_deadlock');
341
+ return [];
342
+ };
343
+ registry_get_stub.callsFake((kind: string) => (kind === slowAction ? [slowHandler] : []));
344
+
345
+ const dispatchPromise = actionDispatcher.dispatch({ kind: slowAction });
346
+ await handlerRunning.promise;
347
+
348
+ // Response must resolve even though the queue is busy
349
+ await actionDispatcher.dispatch(responseAction);
350
+ await dispatchPromise;
351
+ });
352
+
353
+ it('request - resolves for locally handled request (server→server)', async () => {
354
+ const localRequestKind = 'localRequest';
355
+ const localResponseKind = 'localResponse';
356
+
357
+ const handler = new mock.StubActionHandler([localRequestKind]);
358
+ sinon.stub(handler, 'execute').callsFake(() => {
359
+ const response: ResponseAction = { kind: localResponseKind, responseId: '' };
360
+ return [response];
361
+ });
362
+ registry_get_stub.callsFake((kind: string) => (kind === localRequestKind ? [handler] : []));
363
+
364
+ const requestAction: RequestAction<ResponseAction> = {
365
+ kind: localRequestKind,
366
+ requestId: ''
367
+ };
368
+
369
+ const result = await actionDispatcher.request(requestAction);
370
+ expect(result).to.exist;
371
+ expect(result.responseId).to.equal(requestAction.requestId);
372
+ });
373
+
374
+ it('request - resolves for locally handled request called from inside a handler', async () => {
375
+ const innerRequestKind = 'innerRequest';
376
+ const innerResponseKind = 'innerResponse';
377
+ const outerActionKind = 'outerAction';
378
+
379
+ const innerHandler = new mock.StubActionHandler([innerRequestKind]);
380
+ sinon.stub(innerHandler, 'execute').callsFake(() => {
381
+ const response: ResponseAction = { kind: innerResponseKind, responseId: '' };
382
+ return [response];
383
+ });
384
+
385
+ const outerHandler = new mock.StubActionHandler([outerActionKind]);
386
+ outerHandler.execute = async () => {
387
+ const innerRequest: RequestAction<ResponseAction> = {
388
+ kind: innerRequestKind,
389
+ requestId: ''
390
+ };
391
+ const result = await actionDispatcher.request(innerRequest);
392
+ expect(result).to.exist;
393
+ return [];
394
+ };
395
+
396
+ registry_get_stub.callsFake((kind: string) => {
397
+ if (kind === outerActionKind) return [outerHandler];
398
+ if (kind === innerRequestKind) return [innerHandler];
399
+ return [];
400
+ });
401
+
402
+ // This must not deadlock
403
+ await actionDispatcher.dispatch({ kind: outerActionKind });
404
+ });
405
+
406
+ it('requestUntil - rejects on timeout when rejectOnTimeout is true', async () => {
407
+ const requestAction: RequestAction<ResponseAction> = {
408
+ kind: 'testRequest',
409
+ requestId: 'req_hard'
410
+ };
411
+
412
+ clientActionForwarderStub.shouldForwardToClient.callsFake(action => action.kind === 'testRequest');
413
+ clientActionForwarderStub.handle.callsFake(action => action.kind === 'testRequest');
414
+ registry_get_stub.callsFake(() => []);
415
+
416
+ try {
417
+ await actionDispatcher.requestUntil(requestAction, 100, true);
418
+ expect.fail('Should have thrown');
419
+ } catch (error: unknown) {
420
+ expect((error as Error).message).to.include('timed out');
421
+ }
422
+ });
423
+
424
+ it('requestUntil - resolves undefined on timeout when rejectOnTimeout is false', async () => {
425
+ const requestAction: RequestAction<ResponseAction> = {
426
+ kind: 'testRequest',
427
+ requestId: 'req_soft'
428
+ };
429
+
430
+ clientActionForwarderStub.shouldForwardToClient.callsFake(action => action.kind === 'testRequest');
431
+ clientActionForwarderStub.handle.callsFake(action => action.kind === 'testRequest');
432
+ registry_get_stub.callsFake(() => []);
433
+
434
+ const result = await actionDispatcher.requestUntil(requestAction, 100, false);
435
+ expect(result).to.be.undefined;
436
+ });
437
+
438
+ it('request - auto-generates requestId when empty', async () => {
439
+ const requestAction: RequestAction<ResponseAction> = {
440
+ kind: 'testRequest',
441
+ requestId: ''
442
+ };
443
+
444
+ clientActionForwarderStub.shouldForwardToClient.callsFake(action => action.kind === 'testRequest');
445
+ clientActionForwarderStub.handle.callsFake(action => action.kind === 'testRequest');
446
+ registry_get_stub.callsFake(() => []);
447
+
448
+ const responsePromise = actionDispatcher.request(requestAction);
449
+ expect(requestAction.requestId).to.match(/^server_.*_\d+$/);
450
+
451
+ await actionDispatcher.dispatch({
452
+ kind: 'testResponse',
453
+ responseId: requestAction.requestId
454
+ } as ResponseAction);
455
+
456
+ const result = await responsePromise;
457
+ expect(result).to.exist;
458
+ });
459
+
460
+ it('request - rejects when dispatch fails (no handler, not a client action)', async () => {
461
+ const requestAction: RequestAction<ResponseAction> = {
462
+ kind: 'unknownRequest',
463
+ requestId: 'req_fail'
464
+ };
465
+
466
+ // NOT forwarded to client, no handler registered → dispatch throws
467
+ clientActionForwarderStub.shouldForwardToClient.returns(false);
468
+ clientActionForwarderStub.handle.returns(false);
469
+ registry_get_stub.callsFake(() => []);
470
+
471
+ try {
472
+ await actionDispatcher.request(requestAction);
473
+ expect.fail('Should have thrown');
474
+ } catch (error: unknown) {
475
+ expect((error as Error).message).to.include('No handler registered');
476
+ }
477
+ });
478
+
479
+ it('requestUntil - late response after timeout has responseId cleared', async () => {
480
+ const requestAction: RequestAction<ResponseAction> = {
481
+ kind: 'testRequest',
482
+ requestId: 'req_late'
483
+ };
484
+
485
+ clientActionForwarderStub.shouldForwardToClient.callsFake(action => action.kind === 'testRequest');
486
+ clientActionForwarderStub.handle.callsFake(action => action.kind === 'testRequest');
487
+ // Register a no-op handler for testResponse so the late response can be dispatched normally
488
+ const noopHandler = new mock.StubActionHandler(['testResponse']);
489
+ registry_get_stub.callsFake((kind: string) => (kind === 'testResponse' ? [noopHandler] : []));
490
+
491
+ // Request times out
492
+ try {
493
+ await actionDispatcher.requestUntil(requestAction, 50, true);
494
+ expect.fail('Should have thrown');
495
+ } catch (error: unknown) {
496
+ expect((error as Error).message).to.include('timed out');
497
+ }
498
+
499
+ // Late response arrives — responseId should be cleared, dispatched as normal action
500
+ const lateResponse: ResponseAction = { kind: 'testResponse', responseId: 'req_late' };
501
+ await actionDispatcher.dispatch(lateResponse);
502
+ expect(lateResponse.responseId).to.equal('');
503
+ });
504
+
505
+ it('request - resolves when response intercept happens from inside doDispatch', async () => {
506
+ // A local handler for the request kind returns the matching response action directly.
507
+ // The response is dispatched via dispatchResponses() -> dispatch() and intercepted
508
+ // via the reentrant path (not via the external dispatch() entry).
509
+ const requestKind = 'inlineRequest';
510
+ const responseKind = 'inlineResponse';
511
+
512
+ const handler = new mock.StubActionHandler([requestKind]);
513
+ sinon.stub(handler, 'execute').callsFake(() => [{ kind: responseKind, responseId: '' } as ResponseAction]);
514
+ registry_get_stub.callsFake((kind: string) => (kind === requestKind ? [handler] : []));
515
+
516
+ const requestAction: RequestAction<ResponseAction> = { kind: requestKind, requestId: '' };
517
+ const result = await actionDispatcher.request(requestAction);
518
+
519
+ expect(result.responseId).to.equal(requestAction.requestId);
520
+ });
521
+ });
522
+
523
+ describe('test reentrancy and ordering', () => {
524
+ it('dispatch from within a handler runs inline before the next queued action', async () => {
525
+ const outerKind = 'reentrantOuter';
526
+ const innerKind = 'reentrantInner';
527
+ const followerKind = 'reentrantFollower';
528
+
529
+ const events: string[] = [];
530
+
531
+ const innerHandler = new mock.StubActionHandler([innerKind]);
532
+ sinon.stub(innerHandler, 'execute').callsFake(() => {
533
+ events.push('inner');
534
+ return [];
535
+ });
536
+
537
+ const outerHandler = new mock.StubActionHandler([outerKind]);
538
+ outerHandler.execute = async () => {
539
+ events.push('outer-start');
540
+ await actionDispatcher.dispatch({ kind: innerKind });
541
+ events.push('outer-end');
542
+ return [];
543
+ };
544
+
545
+ const followerHandler = new mock.StubActionHandler([followerKind]);
546
+ sinon.stub(followerHandler, 'execute').callsFake(() => {
547
+ events.push('follower');
548
+ return [];
549
+ });
550
+
551
+ registry_get_stub.callsFake((kind: string) => {
552
+ if (kind === outerKind) return [outerHandler];
553
+ if (kind === innerKind) return [innerHandler];
554
+ if (kind === followerKind) return [followerHandler];
555
+ return [];
556
+ });
557
+
558
+ actionDispatcher.dispatch({ kind: outerKind });
559
+ await actionDispatcher.dispatch({ kind: followerKind });
560
+
561
+ expect(events).to.deep.equal(['outer-start', 'inner', 'outer-end', 'follower']);
562
+ });
563
+
564
+ it('handler response actions are dispatched in order without an ephemeral queue', async () => {
565
+ const requestKind = 'orderedRequest';
566
+ const firstResponse = 'orderedResponse1';
567
+ const secondResponse = 'orderedResponse2';
568
+ const order: string[] = [];
569
+
570
+ const requestHandler = new mock.StubActionHandler([requestKind]);
571
+ sinon.stub(requestHandler, 'execute').callsFake(() => [{ kind: firstResponse }, { kind: secondResponse }]);
572
+
573
+ const firstHandler = new mock.StubActionHandler([firstResponse]);
574
+ sinon.stub(firstHandler, 'execute').callsFake(async () => {
575
+ await mock.delay(20);
576
+ order.push(firstResponse);
577
+ return [];
578
+ });
579
+
580
+ const secondHandler = new mock.StubActionHandler([secondResponse]);
581
+ sinon.stub(secondHandler, 'execute').callsFake(() => {
582
+ order.push(secondResponse);
583
+ return [];
584
+ });
585
+
586
+ registry_get_stub.callsFake((kind: string) => {
587
+ if (kind === requestKind) return [requestHandler];
588
+ if (kind === firstResponse) return [firstHandler];
589
+ if (kind === secondResponse) return [secondHandler];
590
+ return [];
591
+ });
592
+
593
+ await actionDispatcher.dispatch({ kind: requestKind });
594
+ expect(order).to.deep.equal([firstResponse, secondResponse]);
595
+ });
596
+ });
597
+
598
+ describe('test dispose', () => {
599
+ it('dispose - rejects all pending requests', async () => {
600
+ const requestAction: RequestAction<ResponseAction> = {
601
+ kind: 'testRequest',
602
+ requestId: 'req_dispose'
603
+ };
604
+
605
+ clientActionForwarderStub.shouldForwardToClient.callsFake(action => action.kind === 'testRequest');
606
+ clientActionForwarderStub.handle.callsFake(action => action.kind === 'testRequest');
607
+ registry_get_stub.callsFake(() => []);
608
+
609
+ const responsePromise = actionDispatcher.request(requestAction);
610
+ actionDispatcher.dispose();
611
+
612
+ try {
613
+ await responsePromise;
614
+ expect.fail('Should have thrown');
615
+ } catch (error: unknown) {
616
+ expect((error as Error).message).to.include('cancelled');
617
+ expect((error as Error).message).to.include('req_dispose');
618
+ }
619
+ });
620
+
621
+ it('dispose - rejects queued dispatch() promises instead of orphaning them', async () => {
622
+ // Use a fresh dispatcher so the shared one is not affected.
623
+ const localDispatcher = container.resolve(DefaultActionDispatcher);
624
+ const slowKind = 'slowDispose';
625
+ const queuedKind = 'queuedDispose';
626
+
627
+ const slowHandler = new mock.StubActionHandler([slowKind]);
628
+ const slowStarted = new Deferred<void>();
629
+ const releaseSlow = new Deferred<void>();
630
+ slowHandler.execute = async () => {
631
+ slowStarted.resolve();
632
+ await releaseSlow.promise;
633
+ return [];
634
+ };
635
+
636
+ registry_get_stub.callsFake((kind: string) => (kind === slowKind ? [slowHandler] : []));
637
+
638
+ const slowPromise = localDispatcher.dispatch({ kind: slowKind });
639
+ await slowStarted.promise;
640
+ const queuedPromise = localDispatcher.dispatch({ kind: queuedKind });
641
+
642
+ localDispatcher.dispose();
643
+
644
+ try {
645
+ await queuedPromise;
646
+ expect.fail('Queued dispatch should have rejected');
647
+ } catch (error: unknown) {
648
+ expect((error as Error).message).to.include('ActionDispatcher disposed');
649
+ }
650
+
651
+ // Let the slow handler finish so the local dispatcher's consumer loop can exit cleanly.
652
+ releaseSlow.resolve();
653
+ await slowPromise;
654
+ });
655
+ });
656
+ });
@@ -1,5 +1,5 @@
1
1
  /********************************************************************************
2
- * Copyright (c) 2022-2025 STMicroelectronics and others.
2
+ * Copyright (c) 2022-2026 STMicroelectronics and others.
3
3
  *
4
4
  * This program and the accompanying materials are made available under the
5
5
  * terms of the Eclipse Public License v. 2.0 which is available at
@@ -16,13 +16,15 @@
16
16
  import { BindingContext } from '@eclipse-glsp/protocol/lib/di';
17
17
  import { ContainerModule } from 'inversify';
18
18
  import * as winston from 'winston';
19
- import { InjectionContainer, LogLevel, Logger, LoggerFactory, NullLogger, getRequestParentName } from '../../common';
19
+ import { ActionDispatchScope, InjectionContainer, LogLevel, Logger, LoggerFactory, NullLogger, getRequestParentName } from '../../common';
20
20
  import { LaunchOptions } from '../launch/cli-parser';
21
+ import { NodeActionDispatchScope } from './node-action-dispatch-scope';
21
22
  import { WinstonLogger } from './winston-logger';
22
23
 
23
24
  export function createAppModule(options: LaunchOptions): ContainerModule {
24
25
  return new ContainerModule((bind, unbind, isBound, rebind) => {
25
26
  bind(InjectionContainer).toDynamicValue(dynamicContext => dynamicContext.container);
27
+ bind(ActionDispatchScope).to(NodeActionDispatchScope).inSingletonScope();
26
28
  const context = { bind, unbind, isBound, rebind };
27
29
  configureWinstonLogger(context, options);
28
30
  });