diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e168ea54..6daceb1c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,8 @@ and this project adheres to ## Added -🔧(helm) add option to disable default tls setting by @dominikkaminski #519 +- 🔧(helm) add option to disable default tls setting by @dominikkaminski #519 +- ✨Collaboration long polling fallback #517 ## Changed diff --git a/src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx b/src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx index d8d12d287..bd2c24e3a 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-management/api/syncDocPolling.tsx @@ -9,6 +9,7 @@ interface SyncDocPollingParams { interface SyncDocPollingResponse { yDoc64?: Base64; + connectionsCount: number; } export const syncDocPolling = async ({ diff --git a/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx b/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx index 3b8639fa8..245c833f4 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx @@ -14,7 +14,7 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { const { setBroadcastProvider } = useBroadcastStore(); const { provider, createProvider, destroyProvider, isProviderFailure } = useProviderStore(); - const [pollingInterval] = useState(1500); + const [pollingInterval, setPollingInterval] = useState(1500); const intervalRef = useRef(); useEffect(() => { @@ -68,7 +68,9 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { yDoc64: toBase64(Y.encodeStateAsUpdate(provider.document)), }) .then((response) => { - const { yDoc64 } = response; + const { yDoc64, connectionsCount } = response; + + setPollingInterval(connectionsCount ? 600 : 1500); if (!yDoc64) { return; diff --git a/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts b/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts index 8f1db5b8c..b5b718a88 100644 --- a/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts +++ b/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts @@ -49,10 +49,16 @@ export const collaborationHTTPHandler = async ( return; } - const syncYDoc64 = await syncDoc(room, yDoc64, canEdit, req); + const { syncYDoc64, connectionsCount } = await syncDoc( + room, + yDoc64, + canEdit, + req, + ); res.status(200).json({ yDoc64: syncYDoc64, + connectionsCount, }); }; @@ -69,6 +75,8 @@ const syncDoc = async ( ) => { let docExist = false; let syncYDoc = undefined; + let connectionsCount = 0; + hocusPocusServer.documents.forEach((hpYDoc) => { if (hpYDoc.name !== room) { return; @@ -77,6 +85,7 @@ const syncDoc = async ( docExist = true; const hpYDoc64 = toBase64(Y.encodeStateAsUpdate(hpYDoc)); + connectionsCount = hpYDoc.getConnectionsCount(); // If the document has not changed, return if (yDoc64 === hpYDoc64) { @@ -117,5 +126,8 @@ const syncDoc = async ( syncYDoc = hpYDoc.merge(ydoc); } - return syncYDoc && toBase64(Y.encodeStateAsUpdate(syncYDoc)); + return { + syncYDoc64: syncYDoc && toBase64(Y.encodeStateAsUpdate(syncYDoc)), + connectionsCount, + }; };