Skip to content

Commit

Permalink
#384: Add request context to avoid collisions in request IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
spoenemann committed Oct 5, 2023
1 parent 6e9cd10 commit cab86ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/sprotty-protocol/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,21 @@ export function isRequestAction(object?: Action): object is RequestAction<Respon
return hasOwnProperty<string, string>(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;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/sprotty/src/base/actions/action-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -34,6 +34,10 @@ export interface IActionDispatcher {
request<Res extends ResponseAction>(action: RequestAction<Res>): Promise<Res>
}

// 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.
Expand Down

0 comments on commit cab86ee

Please sign in to comment.