From c7a4805e5cf1c68f575fbeb207e462ffbf4682ce Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 22 Aug 2023 19:18:35 -0500 Subject: [PATCH] fix(y-provider): syncing status (#3903) --- .commitlintrc.json | 1 + packages/y-provider/src/lazy-provider.ts | 50 ++++++++++++++++++------ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/.commitlintrc.json b/.commitlintrc.json index bd052fb43a..1c33fe25bc 100644 --- a/.commitlintrc.json +++ b/.commitlintrc.json @@ -21,6 +21,7 @@ "native", "templates", "y-indexeddb", + "y-provider", "debug", "storage", "infra", diff --git a/packages/y-provider/src/lazy-provider.ts b/packages/y-provider/src/lazy-provider.ts index 852cb6ff3e..670d2e0cf2 100644 --- a/packages/y-provider/src/lazy-provider.ts +++ b/packages/y-provider/src/lazy-provider.ts @@ -52,7 +52,7 @@ export const createLazyProvider = ( const changeStatus = (newStatus: Status) => { // simulate a stack, each syncing and synced should be paired if (newStatus.type === 'idle') { - if (syncingStack !== 0) { + if (connected && syncingStack !== 0) { console.error('syncingStatus !== 0, this should not happen'); } syncingStack = 0; @@ -79,6 +79,9 @@ export const createLazyProvider = ( async function syncDoc(doc: Doc) { const guid = doc.guid; + if (!connected) { + return; + } changeStatus({ type: 'syncing', @@ -87,6 +90,18 @@ export const createLazyProvider = ( .queryDocState(guid, { stateVector: encodeStateVector(doc), }) + .then(remoteUpdate => { + if (!connected) { + changeStatus({ + type: 'idle', + }); + return; + } + changeStatus({ + type: 'synced', + }); + return remoteUpdate; + }) .catch(error => { changeStatus({ type: 'error', @@ -94,9 +109,6 @@ export const createLazyProvider = ( }); throw error; }); - changeStatus({ - type: 'synced', - }); pendingMap.set(guid, []); @@ -171,6 +183,9 @@ export const createLazyProvider = ( */ function setupDatasourceListeners() { datasourceUnsub = datasource.onDocUpdate?.((guid, update) => { + if (!connected) { + return; + } changeStatus({ type: 'syncing', }); @@ -244,16 +259,25 @@ export const createLazyProvider = ( }); // root doc should be already loaded, // but we want to populate the cache for later update events - connectDoc(rootDoc).catch(error => { - changeStatus({ - type: 'error', - error, + connectDoc(rootDoc) + .then(() => { + if (!connected) { + changeStatus({ + type: 'idle', + }); + return; + } + changeStatus({ + type: 'synced', + }); + }) + .catch(error => { + changeStatus({ + type: 'error', + error, + }); + console.error(error); }); - console.error(error); - }); - changeStatus({ - type: 'synced', - }); setupDatasourceListeners(); }