Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
EYHN committed Dec 18, 2024
1 parent cb14aee commit ea29b11
Show file tree
Hide file tree
Showing 34 changed files with 463 additions and 159 deletions.
6 changes: 3 additions & 3 deletions packages/common/nbstore/src/__tests__/frontend.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AwarenessFrontend } from '../frontend/awareness';
import { DocFrontend } from '../frontend/doc';
import { BroadcastChannelAwarenessStorage } from '../impls/broadcast-channel/awareness';
import { IndexedDBDocStorage } from '../impls/idb';
import { AwarenessSync } from '../sync/awareness';
import { AwarenessSyncImpl } from '../sync/awareness';
import { expectYjsEqual } from './utils';

test('doc', async () => {
Expand Down Expand Up @@ -90,13 +90,13 @@ test('awareness', async () => {
const awarenessC = new Awareness(docC);

{
const sync = new AwarenessSync(storage1, [storage2]);
const sync = new AwarenessSyncImpl(storage1, [storage2]);
const frontend = new AwarenessFrontend(sync);
frontend.connect(awarenessA);
frontend.connect(awarenessB);
}
{
const sync = new AwarenessSync(storage2, [storage1]);
const sync = new AwarenessSyncImpl(storage2, [storage1]);
const frontend = new AwarenessFrontend(sync);
frontend.connect(awarenessC);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/frontend/awareness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
} from 'y-protocols/awareness.js';

import type { AwarenessRecord } from '../storage/awareness';
import type { AwarenessSync } from '../sync/awareness';
import type { AwarenessSyncImpl } from '../sync/awareness';

type AwarenessChanges = Record<'added' | 'updated' | 'removed', number[]>;

export class AwarenessFrontend {
constructor(private readonly sync: AwarenessSync) {}
constructor(private readonly sync: AwarenessSyncImpl) {}

connect(awareness: Awareness) {
const uniqueId = nanoid();
Expand Down
8 changes: 4 additions & 4 deletions packages/common/nbstore/src/frontend/blob.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { BlobRecord, BlobStorage } from '../storage';
import type { BlobSync } from '../sync/blob';
import type { BasicBlobStorage,BlobRecord } from '../storage';
import type { BlobSyncImpl } from '../sync/blob';

export class BlobFrontend {
constructor(
readonly storage: BlobStorage,
readonly sync?: BlobSync
readonly storage: BasicBlobStorage,
readonly sync?: BlobSyncImpl
) {}

get(blobId: string) {
Expand Down
10 changes: 5 additions & 5 deletions packages/common/nbstore/src/frontend/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
mergeUpdates,
} from 'yjs';

import type { DocRecord, DocStorage } from '../storage';
import type { DocSync } from '../sync/doc';
import type { BasicDocStorage,DocRecord } from '../storage';
import type { DocSyncImpl } from '../sync/doc';
import { AsyncPriorityQueue } from '../utils/async-priority-queue';
import { isEmptyUpdate } from '../utils/is-empty-update';
import { throwIfAborted } from '../utils/throw-if-aborted';
Expand Down Expand Up @@ -55,8 +55,8 @@ export class DocFrontend {
private readonly abort = new AbortController();

constructor(
private readonly storage: DocStorage,
private readonly sync: DocSync | null,
private readonly storage: BasicDocStorage,
private readonly sync: DocSyncImpl | null,
readonly options: DocFrontendOptions = {}
) {}

Expand Down Expand Up @@ -88,7 +88,7 @@ export class DocFrontend {
}),
]);

// eslint-disable-next-line no-constant-condition
while (true) {
throwIfAborted(signal);
const docId = await this.status.jobDocQueue.asyncPop(signal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { nanoid } from 'nanoid';

import {
type AwarenessRecord,
AwarenessStorage,
BasicAwarenessStorage,
} from '../../storage/awareness';
import { BroadcastChannelConnection } from './channel';

Expand All @@ -25,7 +25,7 @@ type ChannelMessage =
collectId: string;
};

export class BroadcastChannelAwarenessStorage extends AwarenessStorage {
export class BroadcastChannelAwarenessStorage extends BasicAwarenessStorage {
override readonly storageType = 'awareness';
override readonly connection = new BroadcastChannelConnection(this.options);
get channel() {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/cloud/awareness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { SocketOptions } from 'socket.io-client';
import { share } from '../../connection';
import {
type AwarenessRecord,
AwarenessStorage,
type AwarenessStorageOptions,
BasicAwarenessStorage,
} from '../../storage/awareness';
import {
base64ToUint8Array,
Expand All @@ -16,7 +16,7 @@ interface CloudAwarenessStorageOptions extends AwarenessStorageOptions {
socketOptions: SocketOptions;
}

export class CloudAwarenessStorage extends AwarenessStorage<CloudAwarenessStorageOptions> {
export class CloudAwarenessStorage extends BasicAwarenessStorage<CloudAwarenessStorageOptions> {
connection = share(
new SocketConnection(this.peer, this.options.socketOptions)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/cloud/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
} from '@affine/graphql';

import { DummyConnection } from '../../connection';
import { type BlobRecord, BlobStorage } from '../../storage';
import { BasicBlobStorage,type BlobRecord } from '../../storage';

export class CloudBlobStorage extends BlobStorage {
export class CloudBlobStorage extends BasicBlobStorage {
private readonly gql = gqlFetcherFactory(this.options.peer + '/graphql');
override connection = new DummyConnection();

Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/cloud/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { SocketOptions } from 'socket.io-client';

import { share } from '../../connection';
import {
BasicDocStorage,
type DocClock,
type DocClocks,
DocStorage,
type DocStorageOptions,
type DocUpdate,
} from '../../storage';
Expand All @@ -19,7 +19,7 @@ interface CloudDocStorageOptions extends DocStorageOptions {
socketOptions: SocketOptions;
}

export class CloudDocStorage extends DocStorage<CloudDocStorageOptions> {
export class CloudDocStorage extends BasicDocStorage<CloudDocStorageOptions> {
connection = share(
new SocketConnection(this.peer, this.options.socketOptions)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/idb/blob.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { share } from '../../connection';
import {
BasicBlobStorage,
type BlobRecord,
BlobStorage,
type ListedBlobRecord,
} from '../../storage';
import { IDBConnection } from './db';

export class IndexedDBBlobStorage extends BlobStorage {
export class IndexedDBBlobStorage extends BasicBlobStorage {
readonly connection = share(new IDBConnection(this.options));

get db() {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/idb/doc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
BasicDocStorage,
type DocClock,
type DocClocks,
type DocRecord,
DocStorage,
type DocStorageOptions,
type DocUpdate,
} from '../../storage';
Expand All @@ -15,7 +15,7 @@ interface ChannelMessage {
origin?: string;
}

export class IndexedDBDocStorage extends DocStorage {
export class IndexedDBDocStorage extends BasicDocStorage {
readonly connection = new IDBConnection(this.options);

get db() {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/idb/v1/blob.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { share } from '../../../connection';
import { BlobStorage, type ListedBlobRecord } from '../../../storage';
import { BasicBlobStorage, type ListedBlobRecord } from '../../../storage';
import { BlobIDBConnection } from './db';

/**
* @deprecated readonly
*/
export class IndexedDBV1BlobStorage extends BlobStorage {
export class IndexedDBV1BlobStorage extends BasicBlobStorage {
readonly connection = share(new BlobIDBConnection(this.spaceId));

get db() {
Expand Down
8 changes: 6 additions & 2 deletions packages/common/nbstore/src/impls/idb/v1/doc.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { share } from '../../../connection';
import { type DocRecord, DocStorage, type DocUpdate } from '../../../storage';
import {
BasicDocStorage,
type DocRecord,
type DocUpdate,
} from '../../../storage';
import { DocIDBConnection } from './db';

/**
* @deprecated readonly
*/
export class IndexedDBV1DocStorage extends DocStorage {
export class IndexedDBV1DocStorage extends BasicDocStorage {
readonly connection = share(new DocIDBConnection());

get db() {
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Storage } from '../storage';
import type { BasicStorage } from '../storage';
import { BroadcastChannelAwarenessStorage } from './broadcast-channel/awareness';
import {
CloudAwarenessStorage,
Expand All @@ -12,7 +12,7 @@ import {
} from './idb';
import { IndexedDBV1BlobStorage, IndexedDBV1DocStorage } from './idb/v1';

type StorageConstructor = new (...args: any[]) => Storage;
type StorageConstructor = new (...args: any[]) => BasicStorage;

const idb: StorageConstructor[] = [
IndexedDBDocStorage,
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/sqlite/blob.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { share } from '../../connection';
import { type BlobRecord, BlobStorage } from '../../storage';
import { BasicBlobStorage,type BlobRecord } from '../../storage';
import { NativeDBConnection } from './db';

export class SqliteBlobStorage extends BlobStorage {
export class SqliteBlobStorage extends BasicBlobStorage {
override connection = share(
new NativeDBConnection(this.peer, this.spaceType, this.spaceId)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/sqlite/doc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { share } from '../../connection';
import { type DocClock, DocStorage, type DocUpdate } from '../../storage';
import { BasicDocStorage, type DocClock, type DocUpdate } from '../../storage';
import { NativeDBConnection } from './db';

export class SqliteDocStorage extends DocStorage {
export class SqliteDocStorage extends BasicDocStorage {
override connection = share(
new NativeDBConnection(this.peer, this.spaceType, this.spaceId)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/common/nbstore/src/impls/sqlite/v1/blob.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { apis } from '@affine/electron-api';

import { DummyConnection, share } from '../../../connection';
import { BlobStorage } from '../../../storage';
import { BasicBlobStorage } from '../../../storage';

/**
* @deprecated readonly
*/
export class SqliteV1BlobStorage extends BlobStorage {
export class SqliteV1BlobStorage extends BasicBlobStorage {
override connection = share(new DummyConnection());

get db() {
Expand Down
8 changes: 6 additions & 2 deletions packages/common/nbstore/src/impls/sqlite/v1/doc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { apis } from '@affine/electron-api';

import { DummyConnection, share } from '../../../connection';
import { type DocRecord, DocStorage, type DocUpdate } from '../../../storage';
import {
BasicDocStorage,
type DocRecord,
type DocUpdate,
} from '../../../storage';

/**
* @deprecated readonly
*/
export class SqliteV1DocStorage extends DocStorage {
export class SqliteV1DocStorage extends BasicDocStorage {
override connection = share(new DummyConnection());

get db() {
Expand Down
24 changes: 18 additions & 6 deletions packages/common/nbstore/src/storage/awareness.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Storage, type StorageOptions } from './storage';
import { BasicStorage, type Storage, type StorageOptions } from './storage';

export interface AwarenessStorageOptions extends StorageOptions {}

Expand All @@ -7,16 +7,28 @@ export type AwarenessRecord = {
bin: Uint8Array;
};

export abstract class AwarenessStorage<
Options extends AwarenessStorageOptions = AwarenessStorageOptions,
> extends Storage<Options> {
override readonly storageType = 'awareness';

export interface AwarenessStorage extends Storage {
/**
* Update the awareness record.
*
* @param origin - Internal identifier to recognize the source in the "update" event. Will not be stored or transferred.
*/
update(record: AwarenessRecord, origin?: string): Promise<void>;
subscribeUpdate(
id: string,
onUpdate: (update: AwarenessRecord, origin?: string) => void,
onCollect: () => Promise<AwarenessRecord | null>
): () => void;
}

export abstract class BasicAwarenessStorage<
Options extends AwarenessStorageOptions = AwarenessStorageOptions,
>
extends BasicStorage<Options>
implements AwarenessStorage
{
override readonly storageType = 'awareness';

abstract update(record: AwarenessRecord, origin?: string): Promise<void>;

abstract subscribeUpdate(
Expand Down
24 changes: 20 additions & 4 deletions packages/common/nbstore/src/storage/blob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Storage, type StorageOptions } from './storage';
import { BasicStorage, type Storage, type StorageOptions } from './storage';

export interface BlobStorageOptions extends StorageOptions {}

Expand All @@ -16,9 +16,25 @@ export interface ListedBlobRecord {
createdAt?: Date;
}

export abstract class BlobStorage<
Options extends BlobStorageOptions = BlobStorageOptions,
> extends Storage<Options> {
export interface BlobStorage extends Storage {
readonly storageType: 'blob';
get(key: string, signal?: AbortSignal): Promise<BlobRecord | null>;
set(blob: BlobRecord, signal?: AbortSignal): Promise<void>;
delete(
key: string,
permanently: boolean,
signal?: AbortSignal
): Promise<void>;
release(signal?: AbortSignal): Promise<void>;
list(signal?: AbortSignal): Promise<ListedBlobRecord[]>;
}

export abstract class BasicBlobStorage<
Options extends BlobStorageOptions = BlobStorageOptions,
>
extends BasicStorage<Options>
implements BlobStorage
{
override readonly storageType = 'blob';

abstract get(key: string, signal?: AbortSignal): Promise<BlobRecord | null>;
Expand Down
Loading

0 comments on commit ea29b11

Please sign in to comment.