mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
refactor: replace with data source (#4447)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import type { DatasourceDocAdapter } from '@affine/y-provider';
|
||||
import type { DocDataSource } from '@affine/y-provider';
|
||||
import type { Socket } from 'socket.io-client';
|
||||
import { Manager } from 'socket.io-client';
|
||||
import {
|
||||
@@ -129,7 +129,7 @@ export const createAffineDataSource = (
|
||||
socket.disconnect();
|
||||
};
|
||||
},
|
||||
} satisfies DatasourceDocAdapter & { readonly socket: Socket };
|
||||
} satisfies DocDataSource & { readonly socket: Socket };
|
||||
};
|
||||
|
||||
function setupAffineAwareness(
|
||||
|
||||
@@ -10,10 +10,10 @@ import type { DocProviderCreator } from '@blocksuite/store';
|
||||
import { Workspace } from '@blocksuite/store';
|
||||
import { createBroadcastChannelProvider } from '@blocksuite/store/providers/broadcast-channel';
|
||||
import {
|
||||
createIndexedDBDatasource,
|
||||
createIndexedDBProvider as create,
|
||||
downloadBinary,
|
||||
} from '@toeverything/y-indexeddb';
|
||||
import type { Doc } from 'yjs';
|
||||
import { encodeStateVector } from 'yjs';
|
||||
|
||||
import { createAffineDataSource } from '../affine';
|
||||
import {
|
||||
@@ -87,18 +87,13 @@ const createIndexedDBDownloadProvider: DocProviderCreator = (
|
||||
id,
|
||||
doc
|
||||
): LocalIndexedDBDownloadProvider => {
|
||||
const datasource = createIndexedDBDatasource({});
|
||||
let _resolve: () => void;
|
||||
let _reject: (error: unknown) => void;
|
||||
const promise = new Promise<void>((resolve, reject) => {
|
||||
_resolve = resolve;
|
||||
_reject = reject;
|
||||
});
|
||||
async function downloadAndApply(doc: Doc) {
|
||||
const binary = await downloadBinary(doc.guid);
|
||||
if (binary) {
|
||||
Y.applyUpdate(doc, binary, indexedDBDownloadOrigin);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
flavour: 'local-indexeddb',
|
||||
@@ -111,7 +106,17 @@ const createIndexedDBDownloadProvider: DocProviderCreator = (
|
||||
},
|
||||
sync: () => {
|
||||
logger.info('sync indexeddb provider', id);
|
||||
downloadAndApply(doc).then(_resolve).catch(_reject);
|
||||
datasource
|
||||
.queryDocState(doc.guid, {
|
||||
stateVector: encodeStateVector(doc),
|
||||
})
|
||||
.then(docState => {
|
||||
if (docState) {
|
||||
Y.applyUpdate(doc, docState.missing, indexedDBDownloadOrigin);
|
||||
}
|
||||
_resolve();
|
||||
})
|
||||
.catch(_reject);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -2,10 +2,7 @@ import type {
|
||||
SQLiteDBDownloadProvider,
|
||||
SQLiteProvider,
|
||||
} from '@affine/env/workspace';
|
||||
import {
|
||||
createLazyProvider,
|
||||
type DatasourceDocAdapter,
|
||||
} from '@affine/y-provider';
|
||||
import { createLazyProvider, type DocDataSource } from '@affine/y-provider';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
import type { DocProviderCreator } from '@blocksuite/store';
|
||||
import { Workspace as BlockSuiteWorkspace } from '@blocksuite/store';
|
||||
@@ -17,7 +14,7 @@ const Y = BlockSuiteWorkspace.Y;
|
||||
|
||||
const sqliteOrigin = 'sqlite-provider-origin';
|
||||
|
||||
const createDatasource = (workspaceId: string): DatasourceDocAdapter => {
|
||||
const createDatasource = (workspaceId: string): DocDataSource => {
|
||||
if (!window.apis?.db) {
|
||||
throw new Error('sqlite datasource is not available');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
createLazyProvider,
|
||||
type DatasourceDocAdapter,
|
||||
type DocDataSource,
|
||||
writeOperation,
|
||||
} from '@affine/y-provider';
|
||||
import { assertExists } from '@blocksuite/global/utils';
|
||||
@@ -26,10 +26,10 @@ export function setMergeCount(count: number) {
|
||||
}
|
||||
|
||||
export const createIndexedDBDatasource = ({
|
||||
dbName,
|
||||
dbName = DEFAULT_DB_NAME,
|
||||
mergeCount,
|
||||
}: {
|
||||
dbName: string;
|
||||
dbName?: string;
|
||||
mergeCount?: number;
|
||||
}) => {
|
||||
let dbPromise: Promise<IDBPDatabase<BlockSuiteBinaryDB>> | null = null;
|
||||
@@ -99,7 +99,7 @@ export const createIndexedDBDatasource = ({
|
||||
}
|
||||
}
|
||||
},
|
||||
} satisfies DatasourceDocAdapter;
|
||||
} satisfies DocDataSource;
|
||||
|
||||
return {
|
||||
...adapter,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { setTimeout } from 'node:timers/promises';
|
||||
import { describe, expect, test, vi } from 'vitest';
|
||||
import { applyUpdate, Doc, encodeStateAsUpdate, encodeStateVector } from 'yjs';
|
||||
|
||||
import type { DatasourceDocAdapter } from '../data-source';
|
||||
import type { DocDataSource } from '../data-source';
|
||||
import { createLazyProvider } from '../lazy-provider';
|
||||
import { getDoc } from '../utils';
|
||||
|
||||
@@ -54,7 +54,7 @@ const createMemoryDatasource = (rootDoc: Doc) => {
|
||||
listeners.delete(callback);
|
||||
};
|
||||
},
|
||||
} satisfies DatasourceDocAdapter;
|
||||
} satisfies DocDataSource;
|
||||
return {
|
||||
rootDoc, // expose rootDoc for testing
|
||||
...adapter,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
|
||||
import type { DocState } from './types';
|
||||
|
||||
export interface DatasourceDocAdapter {
|
||||
export interface DocDataSource {
|
||||
/**
|
||||
* request diff update from other clients
|
||||
*/
|
||||
@@ -31,7 +31,7 @@ export interface DatasourceDocAdapter {
|
||||
|
||||
export async function syncDocFromDataSource(
|
||||
rootDoc: YDoc,
|
||||
datasource: DatasourceDocAdapter
|
||||
datasource: DocDataSource
|
||||
) {
|
||||
const downloadDocStateRecursively = async (doc: YDoc) => {
|
||||
const docState = await datasource.queryDocState(doc.guid);
|
||||
@@ -49,7 +49,7 @@ export async function syncDocFromDataSource(
|
||||
|
||||
export async function syncDataSourceFromDoc(
|
||||
rootDoc: YDoc,
|
||||
datasource: DatasourceDocAdapter
|
||||
datasource: DocDataSource
|
||||
) {
|
||||
const uploadDocStateRecursively = async (doc: YDoc) => {
|
||||
await datasource.sendDocUpdate(doc.guid, encodeStateAsUpdate(doc));
|
||||
@@ -72,8 +72,8 @@ export async function syncDataSourceFromDoc(
|
||||
*/
|
||||
export async function syncDataSource(
|
||||
listDocGuids: () => string[],
|
||||
remoteDataSource: DatasourceDocAdapter,
|
||||
localDataSource: DatasourceDocAdapter
|
||||
remoteDataSource: DocDataSource,
|
||||
localDataSource: DocDataSource
|
||||
) {
|
||||
const guids = listDocGuids();
|
||||
await Promise.all(
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
encodeStateVector,
|
||||
} from 'yjs';
|
||||
|
||||
import type { DatasourceDocAdapter } from './data-source';
|
||||
import type { DocDataSource } from './data-source';
|
||||
import type { DataSourceAdapter } from './types';
|
||||
import type { Status } from './types';
|
||||
|
||||
@@ -43,7 +43,7 @@ export type DocProvider = {
|
||||
*/
|
||||
export const createLazyProvider = (
|
||||
rootDoc: Doc,
|
||||
datasource: DatasourceDocAdapter,
|
||||
datasource: DocDataSource,
|
||||
options: LazyProviderOptions = {}
|
||||
): DocProvider & DataSourceAdapter => {
|
||||
let connected = false;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { DatasourceDocAdapter } from './data-source';
|
||||
import type { DocDataSource } from './data-source';
|
||||
|
||||
export type Status =
|
||||
| {
|
||||
@@ -16,7 +16,7 @@ export type Status =
|
||||
};
|
||||
|
||||
export interface DataSourceAdapter {
|
||||
datasource: DatasourceDocAdapter;
|
||||
datasource: DocDataSource;
|
||||
readonly status: Status;
|
||||
|
||||
subscribeStatusChange(onStatusChange: () => void): () => void;
|
||||
|
||||
Reference in New Issue
Block a user