Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

7.6 KB
Raw Blame
diff --git a/dist/hocuspocus-provider.cjs b/dist/hocuspocus-provider.cjs
index cf5602474c793441b87de30417191d98eb83c7e3..05db107ceef2a762d77b7c65ca367c8b6d4da468 100644
--- a/dist/hocuspocus-provider.cjs
+++ b/dist/hocuspocus-provider.cjs
@@ -1183,6 +1183,7 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 		this.connectionAttempt = null;
 		this.receivedOnOpenPayload = void 0;
 		this.closeTries = 0;
+		this.reconnectTimeout = null;
 		this.setConfiguration(configuration);
 		this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket;
 		this.on("open", this.configuration.onOpen);
@@ -1231,8 +1232,15 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 		};
 		if (!this.configuration.autoConnect) this.shouldConnect = false;
 	}
+	clearReconnectTimeout() {
+		if (this.reconnectTimeout) {
+			clearTimeout(this.reconnectTimeout);
+			this.reconnectTimeout = null;
+		}
+	}
 	async connect() {
 		if (this.status === WebSocketStatus.Connected) return;
+		this.clearReconnectTimeout();
 		if (this.cancelWebsocketRetry) {
 			this.cancelWebsocketRetry();
 			this.cancelWebsocketRetry = void 0;
@@ -1361,7 +1369,8 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 		if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) return;
 		this.closeTries += 1;
 		if (this.closeTries > 2) {
-			this.onClose({ event: {
+			// OK patch: forced-close emits close. See src/HocuspocusProviderWebsocket.ts for why.
+			this.emit("close", { event: {
 				code: 4408,
 				reason: "forced"
 			} });
@@ -1382,6 +1391,7 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 	}
 	disconnect() {
 		this.shouldConnect = false;
+		this.clearReconnectTimeout();
 		if (this.webSocket === null) return;
 		try {
 			this.webSocket.close();
@@ -1424,9 +1434,12 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 		this.status = WebSocketStatus.Disconnected;
 		this.emit("status", { status: WebSocketStatus.Disconnected });
 		this.emit("disconnect", { event });
-		if (!this.cancelWebsocketRetry && this.shouldConnect) setTimeout(() => {
-			this.connect();
-		}, this.configuration.delay);
+		if (!this.cancelWebsocketRetry && this.shouldConnect) {
+			this.reconnectTimeout = setTimeout(() => {
+				this.reconnectTimeout = null;
+				this.connect();
+			}, this.configuration.delay);
+		}
 	}
 	destroy() {
 		this.emit("destroy");
diff --git a/dist/hocuspocus-provider.esm.js b/dist/hocuspocus-provider.esm.js
index f51bb321df577ba21a218f2cd7c98cd29f2a12c5..39bea37401037bb6867debca5148668c806b3f24 100644
--- a/dist/hocuspocus-provider.esm.js
+++ b/dist/hocuspocus-provider.esm.js
@@ -1154,6 +1154,7 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 		this.connectionAttempt = null;
 		this.receivedOnOpenPayload = void 0;
 		this.closeTries = 0;
+		this.reconnectTimeout = null;
 		this.setConfiguration(configuration);
 		this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket;
 		this.on("open", this.configuration.onOpen);
@@ -1202,8 +1203,15 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 		};
 		if (!this.configuration.autoConnect) this.shouldConnect = false;
 	}
+	clearReconnectTimeout() {
+		if (this.reconnectTimeout) {
+			clearTimeout(this.reconnectTimeout);
+			this.reconnectTimeout = null;
+		}
+	}
 	async connect() {
 		if (this.status === WebSocketStatus.Connected) return;
+		this.clearReconnectTimeout();
 		if (this.cancelWebsocketRetry) {
 			this.cancelWebsocketRetry();
 			this.cancelWebsocketRetry = void 0;
@@ -1332,7 +1340,8 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 		if (this.configuration.messageReconnectTimeout >= getUnixTime() - this.lastMessageReceived) return;
 		this.closeTries += 1;
 		if (this.closeTries > 2) {
-			this.onClose({ event: {
+			// OK patch: forced-close emits close. See src/HocuspocusProviderWebsocket.ts for why.
+			this.emit("close", { event: {
 				code: 4408,
 				reason: "forced"
 			} });
@@ -1353,6 +1362,7 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 	}
 	disconnect() {
 		this.shouldConnect = false;
+		this.clearReconnectTimeout();
 		if (this.webSocket === null) return;
 		try {
 			this.webSocket.close();
@@ -1395,9 +1405,12 @@ var HocuspocusProviderWebsocket = class HocuspocusProviderWebsocket extends Even
 		this.status = WebSocketStatus.Disconnected;
 		this.emit("status", { status: WebSocketStatus.Disconnected });
 		this.emit("disconnect", { event });
-		if (!this.cancelWebsocketRetry && this.shouldConnect) setTimeout(() => {
-			this.connect();
-		}, this.configuration.delay);
+		if (!this.cancelWebsocketRetry && this.shouldConnect) {
+			this.reconnectTimeout = setTimeout(() => {
+				this.reconnectTimeout = null;
+				this.connect();
+			}, this.configuration.delay);
+		}
 	}
 	destroy() {
 		this.emit("destroy");
diff --git a/src/HocuspocusProviderWebsocket.ts b/src/HocuspocusProviderWebsocket.ts
index 770670c06bcd41e5d5941e5ceeb97cecdb933961..d69bdbe5cc79bf6803add70faa906b0a8508f0c1 100644
--- a/src/HocuspocusProviderWebsocket.ts
+++ b/src/HocuspocusProviderWebsocket.ts
@@ -265,11 +265,22 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
 
 	cancelWebsocketRetry?: () => void;
 
+	reconnectTimeout: ReturnType<typeof setTimeout> | null = null;
+
+	clearReconnectTimeout() {
+		if (this.reconnectTimeout) {
+			clearTimeout(this.reconnectTimeout);
+			this.reconnectTimeout = null;
+		}
+	}
+
 	async connect() {
 		if (this.status === WebSocketStatus.Connected) {
 			return;
 		}
 
+		this.clearReconnectTimeout();
+
 		// Always cancel any previously initiated connection retryer instances
 		if (this.cancelWebsocketRetry) {
 			this.cancelWebsocketRetry();
@@ -470,7 +481,19 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
 		this.closeTries += 1;
 		// https://bugs.webkit.org/show_bug.cgi?id=247943
 		if (this.closeTries > 2) {
-			this.onClose({
+			// OK patch: forced-close emits close.
+			// This branch stands in for a socket close the platform failed to
+			// report, so it has to produce the same emission a real close does.
+			// Calling `onClose` directly runs this websocket's own teardown but
+			// never emits `close`, and `HocuspocusProvider.onClose` (the only
+			// writer of `synced = false`) is subscribed to `close`. Without the
+			// emission `isSynced` stays true across the teardown, and because the
+			// setter is edge-gated the reconnect's SyncStep2 writes true over
+			// true. The transport drop stays observable (status and disconnect
+			// fire either way); what no consumer can learn is that the DOCUMENT
+			// went out of sync. Emitting keeps this websocket's own teardown
+			// ahead of every attached provider's handler, as on a native close.
+			this.emit("close", {
 				event: {
 					code: 4408,
 					reason: "forced",
@@ -503,6 +526,7 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
 
 	disconnect() {
 		this.shouldConnect = false;
+		this.clearReconnectTimeout();
 
 		if (this.webSocket === null) {
 			return;
@@ -574,7 +598,8 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
 
 		// trigger connect if no retry is running and we want to have a connection
 		if (!this.cancelWebsocketRetry && this.shouldConnect) {
-			setTimeout(() => {
+			this.reconnectTimeout = setTimeout(() => {
+				this.reconnectTimeout = null;
 				this.connect();
 			}, this.configuration.delay);
 		}