diff --git a/packages/sprotty-protocol/src/actions.ts b/packages/sprotty-protocol/src/actions.ts index f41ade07..2d8548bb 100644 --- a/packages/sprotty-protocol/src/actions.ts +++ b/packages/sprotty-protocol/src/actions.ts @@ -56,12 +56,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.