Skip to content

Commit

Permalink
feat: allow to amend Atmosphere configuration (#1959) (CP: 1.4) (#1983)…
Browse files Browse the repository at this point in the history
… (CP: 1.3) (#1984)

feat: allow to amend Atmosphere configuration (#1959) (CP: 1.4) (#1983)

Co-authored-by: Luciano Vernaschi <[email protected]>
  • Loading branch information
vaadin-bot and cromoteca authored Jan 24, 2024
1 parent 4ba1115 commit 3b0ab58
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 14 additions & 1 deletion packages/ts/frontend/src/Connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ export interface ConnectClientOptions {
* The `middlewares` property value.
*/
middlewares?: Middleware[];

/**
* The Atmosphere options for the FluxConnection.
*/
atmosphereOptions?: Partial<Atmosphere.Request>;
}

export interface EndpointCallMetaInfo {
Expand Down Expand Up @@ -315,6 +320,10 @@ export class ConnectClient {
* The Hilla endpoint prefix
*/
prefix = '/connect';
/**
* The Atmosphere options for the FluxConnection.
*/
atmosphereOptions: Partial<Atmosphere.Request> = {};

/**
* The array of middlewares that are invoked during a call.
Expand All @@ -335,6 +344,10 @@ export class ConnectClient {
this.middlewares = options.middlewares;
}

if (options.atmosphereOptions) {
this.atmosphereOptions = options.atmosphereOptions;
}

// add connection indicator to DOM
ConnectionIndicator.create();

Expand Down Expand Up @@ -479,7 +492,7 @@ export class ConnectClient {
*/
get fluxConnection(): FluxConnection {
if (!this._fluxConnection) {
this._fluxConnection = new FluxConnection(this.prefix);
this._fluxConnection = new FluxConnection(this.prefix, this.atmosphereOptions);
}
return this._fluxConnection;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/ts/frontend/src/FluxConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ export class FluxConnection extends EventTarget {
state: State = State.INACTIVE;
private pendingMessages: ServerMessage[] = [];

constructor(connectPrefix: string) {
constructor(connectPrefix: string, atmosphereOptions?: Partial<Atmosphere.Request>) {
super();
if (!(window as any).Vaadin?.featureFlags?.hillaPush) {
// Remove when removing feature flag
throw new Error(
`Push support in Hilla is not enabled. Enable it in the debug window or by adding com.vaadin.experimental.hillaPush=true to vaadin-featureflags.properties`,
);
}
this.connectWebsocket(connectPrefix.replace(/connect$/, ''));
this.connectWebsocket(connectPrefix.replace(/connect$/, ''), atmosphereOptions ?? {});
}

private connectWebsocket(prefix: string) {
private connectWebsocket(prefix: string, atmosphereOptions: Partial<Atmosphere.Request>) {
const extraHeaders = getCsrfTokenHeadersForEndpointRequest(document);
const callback = {
onMessage: (response: any) => {
Expand Down Expand Up @@ -92,6 +92,7 @@ export class FluxConnection extends EventTarget {
enableProtocol: true,
headers: extraHeaders,
...callback,
...atmosphereOptions,
});
}

Expand Down

0 comments on commit 3b0ab58

Please sign in to comment.