refactor: replace with data source (#4447)

This commit is contained in:
Alex Yang
2023-09-21 12:31:17 -05:00
committed by GitHub
parent 98f6b3e685
commit d09f6fb7cc
8 changed files with 33 additions and 31 deletions
@@ -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,
+5 -5
View File
@@ -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(
+2 -2
View File
@@ -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;
+2 -2
View File
@@ -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;