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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 13 deletions

View File

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

View File

@ -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();
}