mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-24 05:48:59 +08:00
refactor: replace with data source (#4447)
This commit is contained in:
@@ -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