From c28407f77dd30c194edb9b84fec14cd00b40600d Mon Sep 17 00:00:00 2001 From: MingLiang Wang Date: Tue, 10 Jan 2023 18:19:40 +0800 Subject: [PATCH 1/2] feat: synced socket upload --- packages/data-center/src/datacenter.ts | 2 +- .../data-center/src/provider/affine/affine.ts | 50 ++++++++++++++----- packages/data-center/src/provider/base.ts | 10 ++++ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index 8350013952..06c2a8dbab 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -306,7 +306,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; diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts index 478aa583d5..0a3599c22c 100644 --- a/packages/data-center/src/provider/affine/affine.ts +++ b/packages/data-center/src/provider/affine/affine.ts @@ -3,7 +3,7 @@ import type { ProviderConstructorParams } from '../base'; import type { User, WorkspaceInfo, WorkspaceMeta } 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'; @@ -64,6 +64,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).'); @@ -78,20 +95,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) { @@ -335,4 +342,21 @@ export class AffineProvider extends BaseProvider { } : null; } + + public override async assign( + to: BlocksuiteWorkspace, + from: BlocksuiteWorkspace + ): Promise { + assert(to.room, 'Blocksuite Workspace without room(workspaceId).'); + const ws = this._getWebsocketProvider(to); + applyUpdate(to.doc, encodeStateAsUpdate(from.doc)); + await new Promise((resolve, reject) => { + ws.once('synced', () => { + resolve(); + }); + ws.once('lost-connection', () => reject()); + ws.once('connection-error', () => reject()); + }); + return to; + } } diff --git a/packages/data-center/src/provider/base.ts b/packages/data-center/src/provider/base.ts index f9851098df..8a6668edd5 100644 --- a/packages/data-center/src/provider/base.ts +++ b/packages/data-center/src/provider/base.ts @@ -196,4 +196,14 @@ export class BaseProvider { ): Promise { return workspace; } + + /** + * merge one workspaces to another + * @param workspace + * @returns + */ + public async assign(to: BlocksuiteWorkspace, from: BlocksuiteWorkspace) { + from; + return to; + } } From 22606966759cc5ca806fe44ba65c56cb1dd1e947 Mon Sep 17 00:00:00 2001 From: MingLiang Wang Date: Tue, 10 Jan 2023 18:32:06 +0800 Subject: [PATCH 2/2] feat: remove not used --- packages/data-center/src/datacenter.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index 06c2a8dbab..349dab9e17 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -7,7 +7,6 @@ import { AffineProvider } from './provider'; import type { Message, WorkspaceMeta } 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';