mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-12-29 04:13:52 +03:00
Merge remote-tracking branch 'origin/feat/cloud-sync-saika' into feat/cloud-sync-saika
This commit is contained in:
commit
b4571539ac
@ -11,7 +11,6 @@ import { AffineProvider } from './provider';
|
||||
import type { Message } from './types';
|
||||
import assert from 'assert';
|
||||
import { getLogger } from './logger';
|
||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
import { createBlocksuiteWorkspace } from './utils/index.js';
|
||||
import { MessageCenter } from './message';
|
||||
|
||||
@ -309,7 +308,7 @@ export class DataCenter {
|
||||
this._logger(
|
||||
`update workspace data from ${workspaceInfo.provider} to ${providerId}`
|
||||
);
|
||||
applyUpdate(newWorkspace.doc, encodeStateAsUpdate(workspace.doc));
|
||||
await newProvider.assign(newWorkspace, workspace);
|
||||
assert(newWorkspace, 'Create workspace failed');
|
||||
await currentProvider.deleteWorkspace(workspace.room);
|
||||
return newWorkspace.room;
|
||||
|
@ -8,7 +8,7 @@ import type {
|
||||
import type { User } from '../../types';
|
||||
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
|
||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||
import { applyUpdate } from 'yjs';
|
||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
import { storage } from './storage.js';
|
||||
import assert from 'assert';
|
||||
import { WebsocketProvider } from './sync.js';
|
||||
@ -69,6 +69,23 @@ export class AffineProvider extends BaseProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private _getWebsocketProvider(workspace: BlocksuiteWorkspace) {
|
||||
const { doc, room } = workspace;
|
||||
assert(room);
|
||||
assert(doc);
|
||||
let ws = this._wsMap.get(room);
|
||||
if (!ws) {
|
||||
const wsUrl = `${
|
||||
window.location.protocol === 'https:' ? 'wss' : 'ws'
|
||||
}://${window.location.host}/api/sync/`;
|
||||
ws = new WebsocketProvider(wsUrl, room, doc, {
|
||||
params: { token: this._apis.token.refresh },
|
||||
});
|
||||
this._wsMap.set(room, ws);
|
||||
}
|
||||
return ws;
|
||||
}
|
||||
|
||||
private async _applyCloudUpdates(blocksuiteWorkspace: BlocksuiteWorkspace) {
|
||||
const { doc, room: workspaceId } = blocksuiteWorkspace;
|
||||
assert(workspaceId, 'Blocksuite Workspace without room(workspaceId).');
|
||||
@ -83,20 +100,10 @@ export class AffineProvider extends BaseProvider {
|
||||
|
||||
override async warpWorkspace(workspace: BlocksuiteWorkspace) {
|
||||
await this._applyCloudUpdates(workspace);
|
||||
const { doc, room } = workspace;
|
||||
const { room } = workspace;
|
||||
assert(room);
|
||||
this.linkLocal(workspace);
|
||||
|
||||
let ws = this._wsMap.get(room);
|
||||
if (!ws) {
|
||||
const wsUrl = `${
|
||||
window.location.protocol === 'https:' ? 'wss' : 'ws'
|
||||
}://${window.location.host}/api/sync/`;
|
||||
ws = new WebsocketProvider(wsUrl, room, doc, {
|
||||
params: { token: this._apis.token.refresh },
|
||||
});
|
||||
this._wsMap.set(room, ws);
|
||||
}
|
||||
const ws = this._getWebsocketProvider(workspace);
|
||||
// close all websocket links
|
||||
Array.from(this._wsMap.entries()).forEach(([id, ws]) => {
|
||||
if (id !== room) {
|
||||
@ -339,4 +346,21 @@ export class AffineProvider extends BaseProvider {
|
||||
}
|
||||
: null;
|
||||
}
|
||||
|
||||
public override async assign(
|
||||
to: BlocksuiteWorkspace,
|
||||
from: BlocksuiteWorkspace
|
||||
): Promise<BlocksuiteWorkspace> {
|
||||
assert(to.room, 'Blocksuite Workspace without room(workspaceId).');
|
||||
const ws = this._getWebsocketProvider(to);
|
||||
applyUpdate(to.doc, encodeStateAsUpdate(from.doc));
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
ws.once('synced', () => {
|
||||
resolve();
|
||||
});
|
||||
ws.once('lost-connection', () => reject());
|
||||
ws.once('connection-error', () => reject());
|
||||
});
|
||||
return to;
|
||||
}
|
||||
}
|
||||
|
@ -203,4 +203,14 @@ export class BaseProvider {
|
||||
): Promise<BlocksuiteWorkspace> {
|
||||
return workspace;
|
||||
}
|
||||
|
||||
/**
|
||||
* merge one workspaces to another
|
||||
* @param workspace
|
||||
* @returns
|
||||
*/
|
||||
public async assign(to: BlocksuiteWorkspace, from: BlocksuiteWorkspace) {
|
||||
from;
|
||||
return to;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user