feat: sync with keck

This commit is contained in:
DarkSky
2022-09-14 17:32:08 +08:00
parent 914f5ad436
commit 3ff0dbe781
11 changed files with 350 additions and 35 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ function getCollaborationPoint() {
const { protocol, host } = getLocation();
const ws = protocol.startsWith('https') ? 'wss' : 'ws';
const isOnline = host.endsWith('affine.pro');
const site = isOnline ? host : 'localhost:4200';
const site = isOnline ? host : 'localhost:3000';
return `${ws}://${site}/collaboration/`;
}
+7 -7
View File
@@ -120,13 +120,13 @@ async function _initYjsDatabase(
[name]: p,
};
}),
p({
awareness,
doc: binaries,
token,
workspace: `${workspace}_binaries`,
emitState,
}).then(p => ({ [`${name}_binaries`]: p })),
// p({
// awareness,
// doc: binaries,
// token,
// workspace: `${workspace}_binaries`,
// emitState,
// }).then(p => ({ [`${name}_binaries`]: p })),
])
);
}
+33
View File
@@ -79,6 +79,39 @@ export const getYjsProviders = (
}
) as any; // TODO: type is erased after cascading references
// Wait for ws synchronization to complete, otherwise the data will be modified in reverse, which can be optimized later
return new Promise<void>((resolve, reject) => {
// TODO: synced will also be triggered on reconnection after losing sync
// There needs to be an event mechanism to emit the synchronization state to the upper layer
ws.once('synced', () => resolve());
ws.once('lost-connection', () => resolve());
ws.once('connection-error', () => reject());
ws.on('synced', () => instances.emitState('connected'));
ws.on('lost-connection', () =>
instances.emitState('retry')
);
ws.on('connection-error', () =>
instances.emitState('retry')
);
});
} else {
return;
}
}
},
keck: async (instances: YjsDefaultInstances) => {
if (options.enabled.includes('ws')) {
if (instances.token) {
const ws = new WebsocketProvider(
instances.token,
options.backend,
instances.workspace,
instances.doc,
{
params: options.params,
}
) as any; // TODO: type is erased after cascading references
// Wait for ws synchronization to complete, otherwise the data will be modified in reverse, which can be optimized later
return new Promise<void>((resolve, reject) => {
// TODO: synced will also be triggered on reconnection after losing sync