fix(y-provider): syncing status (#3903)

This commit is contained in:
Alex Yang
2023-08-22 19:18:35 -05:00
committed by GitHub
parent bf062fb6d4
commit c7a4805e5c
2 changed files with 38 additions and 13 deletions

View File

@@ -21,6 +21,7 @@
"native", "native",
"templates", "templates",
"y-indexeddb", "y-indexeddb",
"y-provider",
"debug", "debug",
"storage", "storage",
"infra", "infra",

View File

@@ -52,7 +52,7 @@ export const createLazyProvider = (
const changeStatus = (newStatus: Status) => { const changeStatus = (newStatus: Status) => {
// simulate a stack, each syncing and synced should be paired // simulate a stack, each syncing and synced should be paired
if (newStatus.type === 'idle') { if (newStatus.type === 'idle') {
if (syncingStack !== 0) { if (connected && syncingStack !== 0) {
console.error('syncingStatus !== 0, this should not happen'); console.error('syncingStatus !== 0, this should not happen');
} }
syncingStack = 0; syncingStack = 0;
@@ -79,6 +79,9 @@ export const createLazyProvider = (
async function syncDoc(doc: Doc) { async function syncDoc(doc: Doc) {
const guid = doc.guid; const guid = doc.guid;
if (!connected) {
return;
}
changeStatus({ changeStatus({
type: 'syncing', type: 'syncing',
@@ -87,6 +90,18 @@ export const createLazyProvider = (
.queryDocState(guid, { .queryDocState(guid, {
stateVector: encodeStateVector(doc), stateVector: encodeStateVector(doc),
}) })
.then(remoteUpdate => {
if (!connected) {
changeStatus({
type: 'idle',
});
return;
}
changeStatus({
type: 'synced',
});
return remoteUpdate;
})
.catch(error => { .catch(error => {
changeStatus({ changeStatus({
type: 'error', type: 'error',
@@ -94,9 +109,6 @@ export const createLazyProvider = (
}); });
throw error; throw error;
}); });
changeStatus({
type: 'synced',
});
pendingMap.set(guid, []); pendingMap.set(guid, []);
@@ -171,6 +183,9 @@ export const createLazyProvider = (
*/ */
function setupDatasourceListeners() { function setupDatasourceListeners() {
datasourceUnsub = datasource.onDocUpdate?.((guid, update) => { datasourceUnsub = datasource.onDocUpdate?.((guid, update) => {
if (!connected) {
return;
}
changeStatus({ changeStatus({
type: 'syncing', type: 'syncing',
}); });
@@ -244,16 +259,25 @@ export const createLazyProvider = (
}); });
// root doc should be already loaded, // root doc should be already loaded,
// but we want to populate the cache for later update events // but we want to populate the cache for later update events
connectDoc(rootDoc).catch(error => { connectDoc(rootDoc)
changeStatus({ .then(() => {
type: 'error', if (!connected) {
error, changeStatus({
type: 'idle',
});
return;
}
changeStatus({
type: 'synced',
});
})
.catch(error => {
changeStatus({
type: 'error',
error,
});
console.error(error);
}); });
console.error(error);
});
changeStatus({
type: 'synced',
});
setupDatasourceListeners(); setupDatasourceListeners();
} }