Skip to content

Commit adc315f

Browse files
committed
feat: adds configuration option autoConnect (default=true) which allows creating the provider without triggering a connection attempt (fixes #938)
1 parent 1a3b5b8 commit adc315f

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

packages/provider/src/HocuspocusProviderWebsocket.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@ import {
1818
type onStatusParameters,
1919
} from "./types.ts";
2020

21-
export type HocusPocusWebSocket = WebSocket & { identifier: string };
21+
export type HocuspocusWebSocket = WebSocket & { identifier: string };
22+
export type HocusPocusWebSocket = HocuspocusWebSocket;
2223

2324
export type HocuspocusProviderWebsocketConfiguration = Required<
2425
Pick<CompleteHocuspocusProviderWebsocketConfiguration, "url">
2526
> &
2627
Partial<CompleteHocuspocusProviderWebsocketConfiguration>;
2728

2829
export interface CompleteHocuspocusProviderWebsocketConfiguration {
30+
/**
31+
* Whether to connect automatically when creating the provider instance. Default=true
32+
*/
33+
autoConnect: boolean;
34+
2935
/**
3036
* URL of your @hocuspocus/server instance
3137
*/
@@ -95,6 +101,7 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
95101

96102
public configuration: CompleteHocuspocusProviderWebsocketConfiguration = {
97103
url: "",
104+
autoConnect: true,
98105
// @ts-ignore
99106
document: undefined,
100107
WebSocketPolyfill: undefined,
@@ -179,11 +186,9 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
179186
this.configuration.messageReconnectTimeout / 10,
180187
);
181188

182-
if (!this.shouldConnect) {
183-
return;
189+
if (this.shouldConnect) {
190+
this.connect();
184191
}
185-
186-
this.connect();
187192
}
188193

189194
receivedOnOpenPayload?: Event | undefined = undefined;
@@ -222,6 +227,10 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
222227
configuration: Partial<HocuspocusProviderWebsocketConfiguration> = {},
223228
): void {
224229
this.configuration = { ...this.configuration, ...configuration };
230+
231+
if (!this.configuration.autoConnect) {
232+
this.shouldConnect = false;
233+
}
225234
}
226235

227236
cancelWebsocketRetry?: () => void;

0 commit comments

Comments
 (0)