feat(nbstore): rename SyncStorage to DocSyncStorage (#10751)

This commit is contained in:
EYHN
2025-03-14 06:25:26 +00:00
parent 92effd9b51
commit f3ef9c4415
19 changed files with 90 additions and 79 deletions
@@ -6,7 +6,7 @@ import { Doc as YDoc, encodeStateAsUpdate } from 'yjs';
import {
IndexedDBBlobStorage,
IndexedDBDocStorage,
IndexedDBSyncStorage,
IndexedDBDocSyncStorage,
} from '../impls/idb';
import { SpaceStorage } from '../storage';
import { Sync } from '../sync';
@@ -23,7 +23,7 @@ test('doc', async () => {
type: 'workspace',
});
const peerASync = new IndexedDBSyncStorage({
const peerASync = new IndexedDBDocSyncStorage({
id: 'ws1',
flavour: 'a',
type: 'workspace',
@@ -42,7 +42,7 @@ test('doc', async () => {
const peerA = new SpaceStorage({
doc: peerADoc,
sync: peerASync,
docSync: peerASync,
});
const peerB = new SpaceStorage({
doc: peerBDoc,
@@ -1,9 +1,13 @@
import { share } from '../../connection';
import { type DocClock, type DocClocks, SyncStorageBase } from '../../storage';
import {
type DocClock,
type DocClocks,
DocSyncStorageBase,
} from '../../storage';
import { IDBConnection, type IDBConnectionOptions } from './db';
export class IndexedDBSyncStorage extends SyncStorageBase {
static readonly identifier = 'IndexedDBSyncStorage';
export class IndexedDBDocSyncStorage extends DocSyncStorageBase {
static readonly identifier = 'IndexedDBDocSyncStorage';
constructor(private readonly options: IDBConnectionOptions) {
super();
@@ -1,14 +1,14 @@
import type { StorageConstructor } from '..';
import { IndexedDBBlobStorage } from './blob';
import { IndexedDBDocStorage } from './doc';
import { IndexedDBSyncStorage } from './sync';
import { IndexedDBDocSyncStorage } from './doc-sync';
export * from './blob';
export * from './doc';
export * from './sync';
export * from './doc-sync';
export const idbStorages = [
IndexedDBDocStorage,
IndexedDBBlobStorage,
IndexedDBSyncStorage,
IndexedDBDocSyncStorage,
] satisfies StorageConstructor[];
@@ -1,9 +1,9 @@
import { share } from '../../connection';
import { type DocClock, SyncStorageBase } from '../../storage';
import { type DocClock, DocSyncStorageBase } from '../../storage';
import { NativeDBConnection, type SqliteNativeDBOptions } from './db';
export class SqliteSyncStorage extends SyncStorageBase {
static readonly identifier = 'SqliteSyncStorage';
export class SqliteDocSyncStorage extends DocSyncStorageBase {
static readonly identifier = 'SqliteDocSyncStorage';
override connection = share(new NativeDBConnection(this.options));
@@ -1,15 +1,15 @@
import type { StorageConstructor } from '..';
import { SqliteBlobStorage } from './blob';
import { SqliteDocStorage } from './doc';
import { SqliteSyncStorage } from './sync';
import { SqliteDocSyncStorage } from './doc-sync';
export * from './blob';
export { bindNativeDBApis, type NativeDBApis } from './db';
export * from './doc';
export * from './sync';
export * from './doc-sync';
export const sqliteStorages = [
SqliteDocStorage,
SqliteBlobStorage,
SqliteSyncStorage,
SqliteDocSyncStorage,
] satisfies StorageConstructor[];
@@ -2,8 +2,8 @@ import type { Connection } from '../connection';
import type { DocClock, DocClocks } from './doc';
import { type Storage } from './storage';
export interface SyncStorage extends Storage {
readonly storageType: 'sync';
export interface DocSyncStorage extends Storage {
readonly storageType: 'docSync';
getPeerRemoteClock(peer: string, docId: string): Promise<DocClock | null>;
getPeerRemoteClocks(peer: string): Promise<DocClocks>;
@@ -20,8 +20,8 @@ export interface SyncStorage extends Storage {
clearClocks(): Promise<void>;
}
export abstract class SyncStorageBase implements SyncStorage {
readonly storageType = 'sync';
export abstract class DocSyncStorageBase implements DocSyncStorage {
readonly storageType = 'docSync';
abstract readonly connection: Connection;
abstract getPeerRemoteClock(
@@ -1,8 +1,8 @@
import { DummyConnection } from '../../connection';
import type { DocClock, DocClocks } from '../doc';
import { SyncStorageBase } from '../sync';
import { DocSyncStorageBase } from '../doc-sync';
export class DummySyncStorage extends SyncStorageBase {
export class DummyDocSyncStorage extends DocSyncStorageBase {
override getPeerRemoteClock(
_peer: string,
_docId: string
@@ -9,6 +9,7 @@ import {
} from '../doc';
export class DummyDocStorage implements DocStorage {
spaceId = '';
readonly storageType = 'doc';
readonly isReadonly = true;
getDoc(_docId: string): Promise<DocRecord | null> {
+5 -5
View File
@@ -3,14 +3,14 @@ import EventEmitter2 from 'eventemitter2';
import type { AwarenessStorage } from './awareness';
import type { BlobStorage } from './blob';
import type { DocStorage } from './doc';
import type { DocSyncStorage } from './doc-sync';
import { DummyAwarenessStorage } from './dummy/awareness';
import { DummyBlobStorage } from './dummy/blob';
import { DummyDocStorage } from './dummy/doc';
import { DummySyncStorage } from './dummy/sync';
import { DummyDocSyncStorage } from './dummy/doc-sync';
import type { StorageType } from './storage';
import type { SyncStorage } from './sync';
type Storages = DocStorage | BlobStorage | SyncStorage | AwarenessStorage;
type Storages = DocStorage | BlobStorage | DocSyncStorage | AwarenessStorage;
export type SpaceStorageOptions = {
[K in StorageType]?: Storages & { storageType: K };
@@ -28,7 +28,7 @@ export class SpaceStorage {
awareness: storages.awareness ?? new DummyAwarenessStorage(),
blob: storages.blob ?? new DummyBlobStorage(),
doc: storages.doc ?? new DummyDocStorage(),
sync: storages.sync ?? new DummySyncStorage(),
['docSync']: storages['docSync'] ?? new DummyDocSyncStorage(),
};
}
@@ -71,7 +71,7 @@ export class SpaceStorage {
export * from './awareness';
export * from './blob';
export * from './doc';
export * from './doc-sync';
export * from './errors';
export * from './history';
export * from './storage';
export * from './sync';
@@ -1,6 +1,6 @@
import type { Connection } from '../connection';
export type StorageType = 'blob' | 'doc' | 'sync' | 'awareness';
export type StorageType = 'blob' | 'doc' | 'docSync' | 'awareness';
export interface Storage {
readonly storageType: StorageType;
@@ -1,9 +1,9 @@
import type { Observable } from 'rxjs';
import { combineLatest, map, of, ReplaySubject, share } from 'rxjs';
import type { DocStorage, SyncStorage } from '../../storage';
import type { DocStorage, DocSyncStorage } from '../../storage';
import { DummyDocStorage } from '../../storage/dummy/doc';
import { DummySyncStorage } from '../../storage/dummy/sync';
import { DummyDocSyncStorage } from '../../storage/dummy/doc-sync';
import { MANUALLY_STOP } from '../../utils/throw-if-aborted';
import type { PeerStorageOptions } from '../types';
import { DocSyncPeer } from './peer';
@@ -68,7 +68,7 @@ export class DocSyncImpl implements DocSync {
constructor(
readonly storages: PeerStorageOptions<DocStorage>,
readonly sync: SyncStorage
readonly sync: DocSyncStorage
) {}
/**
@@ -80,7 +80,7 @@ export class DocSyncImpl implements DocSync {
local: new DummyDocStorage(),
remotes: {},
},
new DummySyncStorage()
new DummyDocSyncStorage()
);
}
+2 -2
View File
@@ -3,7 +3,7 @@ import { nanoid } from 'nanoid';
import { Observable, ReplaySubject, share, Subject } from 'rxjs';
import { diffUpdate, encodeStateVectorFromUpdate, mergeUpdates } from 'yjs';
import type { DocStorage, SyncStorage } from '../../storage';
import type { DocStorage, DocSyncStorage } from '../../storage';
import { AsyncPriorityQueue } from '../../utils/async-priority-queue';
import { ClockMap } from '../../utils/clock';
import { isEmptyUpdate } from '../../utils/is-empty-update';
@@ -147,7 +147,7 @@ export class DocSyncPeer {
constructor(
readonly peerId: string,
readonly local: DocStorage,
readonly syncMetadata: SyncStorage,
readonly syncMetadata: DocSyncStorage,
readonly remote: DocStorage,
readonly options: DocSyncPeerOptions = {}
) {}
+2 -2
View File
@@ -23,7 +23,7 @@ export class Sync {
constructor(readonly storages: PeerStorageOptions<SpaceStorage>) {
const doc = storages.local.get('doc');
const blob = storages.local.get('blob');
const sync = storages.local.get('sync');
const docSync = storages.local.get('docSync');
const awareness = storages.local.get('awareness');
this.doc = new DocSyncImpl(
@@ -36,7 +36,7 @@ export class Sync {
])
),
},
sync
docSync
);
this.blob = new BlobSyncImpl({
local: blob,
+21 -21
View File
@@ -45,8 +45,8 @@ class StoreConsumer {
return this.ensureSync.blob;
}
get syncStorage() {
return this.ensureLocal.get('sync');
get docSyncStorage() {
return this.ensureLocal.get('docSync');
}
get awarenessStorage() {
@@ -170,25 +170,25 @@ class StoreConsumer {
this.blobStorage.delete(key, permanently),
'blobStorage.releaseBlobs': () => this.blobStorage.release(),
'blobStorage.listBlobs': () => this.blobStorage.list(),
'syncStorage.clearClocks': () => this.syncStorage.clearClocks(),
'syncStorage.getPeerPulledRemoteClock': ({ peer, docId }) =>
this.syncStorage.getPeerPulledRemoteClock(peer, docId),
'syncStorage.getPeerPulledRemoteClocks': ({ peer }) =>
this.syncStorage.getPeerPulledRemoteClocks(peer),
'syncStorage.setPeerPulledRemoteClock': ({ peer, clock }) =>
this.syncStorage.setPeerPulledRemoteClock(peer, clock),
'syncStorage.getPeerRemoteClock': ({ peer, docId }) =>
this.syncStorage.getPeerRemoteClock(peer, docId),
'syncStorage.getPeerRemoteClocks': ({ peer }) =>
this.syncStorage.getPeerRemoteClocks(peer),
'syncStorage.setPeerRemoteClock': ({ peer, clock }) =>
this.syncStorage.setPeerRemoteClock(peer, clock),
'syncStorage.getPeerPushedClock': ({ peer, docId }) =>
this.syncStorage.getPeerPushedClock(peer, docId),
'syncStorage.getPeerPushedClocks': ({ peer }) =>
this.syncStorage.getPeerPushedClocks(peer),
'syncStorage.setPeerPushedClock': ({ peer, clock }) =>
this.syncStorage.setPeerPushedClock(peer, clock),
'docSyncStorage.clearClocks': () => this.docSyncStorage.clearClocks(),
'docSyncStorage.getPeerPulledRemoteClock': ({ peer, docId }) =>
this.docSyncStorage.getPeerPulledRemoteClock(peer, docId),
'docSyncStorage.getPeerPulledRemoteClocks': ({ peer }) =>
this.docSyncStorage.getPeerPulledRemoteClocks(peer),
'docSyncStorage.setPeerPulledRemoteClock': ({ peer, clock }) =>
this.docSyncStorage.setPeerPulledRemoteClock(peer, clock),
'docSyncStorage.getPeerRemoteClock': ({ peer, docId }) =>
this.docSyncStorage.getPeerRemoteClock(peer, docId),
'docSyncStorage.getPeerRemoteClocks': ({ peer }) =>
this.docSyncStorage.getPeerRemoteClocks(peer),
'docSyncStorage.setPeerRemoteClock': ({ peer, clock }) =>
this.docSyncStorage.setPeerRemoteClock(peer, clock),
'docSyncStorage.getPeerPushedClock': ({ peer, docId }) =>
this.docSyncStorage.getPeerPushedClock(peer, docId),
'docSyncStorage.getPeerPushedClocks': ({ peer }) =>
this.docSyncStorage.getPeerPushedClocks(peer),
'docSyncStorage.setPeerPushedClock': ({ peer, clock }) =>
this.docSyncStorage.setPeerPushedClock(peer, clock),
'awarenessStorage.update': ({ awareness, origin }) =>
this.awarenessStorage.update(awareness, origin),
'awarenessStorage.subscribeUpdate': docId =>
+1 -1
View File
@@ -45,7 +45,7 @@ interface GroupedWorkerOps {
listBlobs: [void, ListedBlobRecord[]];
};
syncStorage: {
docSyncStorage: {
getPeerPulledRemoteClocks: [{ peer: string }, DocClocks];
getPeerPulledRemoteClock: [
{ peer: string; docId: string },
+4 -1
View File
@@ -23,7 +23,10 @@ const cache = createEmotionCache();
let storeManagerClient: StoreManagerClient;
if (window.SharedWorker) {
if (
window.SharedWorker &&
localStorage.getItem('disableSharedWorker') !== 'true'
) {
const worker = new SharedWorker(
new URL(/* webpackChunkName: "nbstore" */ './nbstore.ts', import.meta.url),
{ name: 'affine-shared-worker' }
@@ -1,5 +1,8 @@
import { IndexedDBDocStorage, IndexedDBSyncStorage } from '@affine/nbstore/idb';
import { SqliteDocStorage, SqliteSyncStorage } from '@affine/nbstore/sqlite';
import {
IndexedDBDocStorage,
IndexedDBDocSyncStorage,
} from '@affine/nbstore/idb';
import { SqliteDocStorage, SqliteDocSyncStorage } from '@affine/nbstore/sqlite';
import type { StoreClient } from '@affine/nbstore/worker/client';
import { Entity } from '@toeverything/infra';
@@ -16,10 +19,10 @@ export class UserDBEngine extends Entity<{
BUILD_CONFIG.isElectron || BUILD_CONFIG.isIOS
? SqliteDocStorage
: IndexedDBDocStorage;
SyncStorageType =
DocSyncStorageType =
BUILD_CONFIG.isElectron || BUILD_CONFIG.isIOS
? SqliteSyncStorage
: IndexedDBSyncStorage;
? SqliteDocSyncStorage
: IndexedDBDocSyncStorage;
canGracefulStop() {
// TODO(@eyhn): Implement this
@@ -44,8 +47,8 @@ export class UserDBEngine extends Entity<{
type: 'userspace',
},
},
sync: {
name: this.SyncStorageType.identifier,
docSync: {
name: this.DocSyncStorageType.identifier,
opts: {
id: `${serverService.server.id}:` + this.userId,
type: 'userspace',
@@ -14,7 +14,7 @@ import { CloudBlobStorage, StaticCloudDocStorage } from '@affine/nbstore/cloud';
import {
IndexedDBBlobStorage,
IndexedDBDocStorage,
IndexedDBSyncStorage,
IndexedDBDocSyncStorage,
} from '@affine/nbstore/idb';
import {
IndexedDBV1BlobStorage,
@@ -23,7 +23,7 @@ import {
import {
SqliteBlobStorage,
SqliteDocStorage,
SqliteSyncStorage,
SqliteDocSyncStorage,
} from '@affine/nbstore/sqlite';
import {
SqliteV1BlobStorage,
@@ -111,10 +111,10 @@ class CloudWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
: BUILD_CONFIG.isWeb || BUILD_CONFIG.isMobileWeb
? IndexedDBV1BlobStorage
: undefined;
SyncStorageType =
DocSyncStorageType =
BUILD_CONFIG.isElectron || BUILD_CONFIG.isIOS
? SqliteSyncStorage
: IndexedDBSyncStorage;
? SqliteDocSyncStorage
: IndexedDBDocSyncStorage;
async deleteWorkspace(id: string): Promise<void> {
await this.graphqlService.gql({
@@ -431,8 +431,8 @@ class CloudWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
id: workspaceId,
},
},
sync: {
name: this.SyncStorageType.identifier,
docSync: {
name: this.DocSyncStorageType.identifier,
opts: {
flavour: this.flavour,
type: 'workspace',
@@ -8,7 +8,7 @@ import {
import {
IndexedDBBlobStorage,
IndexedDBDocStorage,
IndexedDBSyncStorage,
IndexedDBDocSyncStorage,
} from '@affine/nbstore/idb';
import {
IndexedDBV1BlobStorage,
@@ -17,7 +17,7 @@ import {
import {
SqliteBlobStorage,
SqliteDocStorage,
SqliteSyncStorage,
SqliteDocSyncStorage,
} from '@affine/nbstore/sqlite';
import {
SqliteV1BlobStorage,
@@ -97,10 +97,10 @@ class LocalWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
: BUILD_CONFIG.isWeb || BUILD_CONFIG.isMobileWeb
? IndexedDBV1BlobStorage
: undefined;
SyncStorageType =
DocSyncStorageType =
BUILD_CONFIG.isElectron || BUILD_CONFIG.isIOS
? SqliteSyncStorage
: IndexedDBSyncStorage;
? SqliteDocSyncStorage
: IndexedDBDocSyncStorage;
async deleteWorkspace(id: string): Promise<void> {
setLocalWorkspaceIds(ids => ids.filter(x => x !== id));
@@ -321,8 +321,8 @@ class LocalWorkspaceFlavourProvider implements WorkspaceFlavourProvider {
id: workspaceId,
},
},
sync: {
name: this.SyncStorageType.identifier,
docSync: {
name: this.DocSyncStorageType.identifier,
opts: {
flavour: this.flavour,
type: 'workspace',