feat: fix type problem

This commit is contained in:
MingLiang Wang 2023-01-09 13:58:53 +08:00
parent 84bea54916
commit 2ff46fa831
3 changed files with 66 additions and 63 deletions

View File

@ -2,11 +2,13 @@ import { describe, test, expect } from 'vitest';
import { Workspaces } from '../../workspaces';
import { LocalProvider } from './local';
import 'fake-indexeddb/auto';
import { BlobStorage } from '@blocksuite/store';
describe('local provider', () => {
const workspaces = new Workspaces();
const provider = new LocalProvider({
workspaces: workspaces.createScope(),
blobs: new BlobStorage(),
});
const workspaceName = 'workspace-test';
@ -27,6 +29,7 @@ describe('local provider', () => {
const workspaces1 = new Workspaces();
const provider1 = new LocalProvider({
workspaces: workspaces1.createScope(),
blobs: new BlobStorage(),
});
await provider1.loadWorkspaces();
expect(workspaces1.workspaces.length).toEqual(1);

View File

@ -1,63 +0,0 @@
import assert from 'assert';
import { LocalProvider } from '../local/index.js';
import { WebsocketProvider } from './sync.js';
export class SelfHostedProvider extends LocalProvider {
static id = 'selfhosted';
private _ws?: WebsocketProvider;
constructor() {
super();
}
async destroy() {
this._ws?.disconnect();
}
async initData() {
const databases = await indexedDB.databases();
await super.initData(
// set locally to true if exists a same name db
databases
.map(db => db.name)
.filter(v => v)
.includes(this._workspace.room)
);
const workspace = this._workspace;
const doc = workspace.doc;
if (workspace.room) {
try {
// Wait for ws synchronization to complete, otherwise the data will be modified in reverse, which can be optimized later
this._ws = new WebsocketProvider(this.host, workspace.room, doc);
await 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
assert(this._ws);
this._ws.once('synced', () => resolve());
this._ws.once('lost-connection', () => resolve());
this._ws.once('connection-error', () => reject());
});
this._signals.listAdd.emit({
workspace: workspace.room,
provider: this.id,
locally: true,
});
} catch (e) {
this._logger('Failed to init cloud workspace', e);
}
}
// if after update, the space:meta is empty
// then we need to get map with doc
// just a workaround for yjs
doc.getMap('space:meta');
}
private get host() {
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
return `${protocol}//${location.host}/collaboration/`;
}
}

View File

@ -0,0 +1,63 @@
// import assert from 'assert';
// import { LocalProvider } from '../local/index.js';
// import { WebsocketProvider } from './sync.js';
// export class SelfHostedProvider extends LocalProvider {
// static id = 'selfhosted';
// private _ws?: WebsocketProvider;
// constructor() {
// super();
// }
// async destroy() {
// this._ws?.disconnect();
// }
// async initData() {
// const databases = await indexedDB.databases();
// await super.initData(
// // set locally to true if exists a same name db
// databases
// .map(db => db.name)
// .filter(v => v)
// .includes(this._workspace.room)
// );
// const workspace = this._workspace;
// const doc = workspace.doc;
// if (workspace.room) {
// try {
// // Wait for ws synchronization to complete, otherwise the data will be modified in reverse, which can be optimized later
// this._ws = new WebsocketProvider(this.host, workspace.room, doc);
// await 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
// assert(this._ws);
// this._ws.once('synced', () => resolve());
// this._ws.once('lost-connection', () => resolve());
// this._ws.once('connection-error', () => reject());
// });
// this._signals.listAdd.emit({
// workspace: workspace.room,
// provider: this.id,
// locally: true,
// });
// } catch (e) {
// this._logger('Failed to init cloud workspace', e);
// }
// }
// // if after update, the space:meta is empty
// // then we need to get map with doc
// // just a workaround for yjs
// doc.getMap('space:meta');
// }
// private get host() {
// const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
// return `${protocol}//${location.host}/collaboration/`;
// }
// }