From cab86ee23a9af990695834c070541518a29aeb05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Sp=C3=B6nemann?= Date: Wed, 4 Oct 2023 16:15:22 +0000 Subject: [PATCH] #384: Add request context to avoid collisions in request IDs --- packages/sprotty-protocol/src/actions.ts | 11 ++++++++++- .../sprotty/src/base/actions/action-dispatcher.ts | 6 +++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/sprotty-protocol/src/actions.ts b/packages/sprotty-protocol/src/actions.ts index 46c4be1e..910abb20 100644 --- a/packages/sprotty-protocol/src/actions.ts +++ b/packages/sprotty-protocol/src/actions.ts @@ -61,12 +61,21 @@ export function isRequestAction(object?: Action): object is RequestAction(object, 'requestId', 'string'); } +let requestContext = ''; let nextRequestId = 1; /** * Generate a unique `requestId` for a request action. */ export function generateRequestId(): string { - return (nextRequestId++).toString(); + return `${requestContext}_${nextRequestId++}`; +} + +/** + * Configure the context in which request actions are created. This is typically either + * 'client' or 'server' to avoid collisions of request IDs. + */ +export function setRequestContext(context: string): void { + requestContext = context; } /** diff --git a/packages/sprotty/src/base/actions/action-dispatcher.ts b/packages/sprotty/src/base/actions/action-dispatcher.ts index 8cb70c33..7022e4d7 100644 --- a/packages/sprotty/src/base/actions/action-dispatcher.ts +++ b/packages/sprotty/src/base/actions/action-dispatcher.ts @@ -17,7 +17,7 @@ import { inject, injectable } from 'inversify'; import { Action, isAction, isRequestAction, isResponseAction, RedoAction, RejectAction, RequestAction, - ResponseAction, SetModelAction, UndoAction + ResponseAction, SetModelAction, setRequestContext, UndoAction } from 'sprotty-protocol/lib/actions'; import { Deferred } from 'sprotty-protocol/lib/utils/async'; import { TYPES } from '../types'; @@ -34,6 +34,10 @@ export interface IActionDispatcher { request(action: RequestAction): Promise } +// This code should be used only in the client part of a Sprotty application. +// We set the request context to 'client' to avoid collisions with requests created by the server. +setRequestContext('client'); + /** * Collects actions, converts them to commands and dispatches them. * Also acts as the proxy to model sources such as diagram servers.