mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-22 12:36:24 +08:00
feat(nbstore): improve nbstore (#9512)
This commit is contained in:
@@ -4,11 +4,17 @@ import {
|
||||
BlobStorageBase,
|
||||
type ListedBlobRecord,
|
||||
} from '../../storage';
|
||||
import { IDBConnection } from './db';
|
||||
import { IDBConnection, type IDBConnectionOptions } from './db';
|
||||
|
||||
export class IndexedDBBlobStorage extends BlobStorageBase {
|
||||
static readonly identifier = 'IndexedDBBlobStorage';
|
||||
|
||||
readonly connection = share(new IDBConnection(this.options));
|
||||
|
||||
constructor(private readonly options: IDBConnectionOptions) {
|
||||
super();
|
||||
}
|
||||
|
||||
get db() {
|
||||
return this.connection.inner.db;
|
||||
}
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
import { type IDBPDatabase, openDB } from 'idb';
|
||||
|
||||
import { AutoReconnectConnection } from '../../connection';
|
||||
import type { StorageOptions } from '../../storage';
|
||||
import type { SpaceType } from '../../utils/universal-id';
|
||||
import { type DocStorageSchema, migrator } from './schema';
|
||||
|
||||
export interface IDBConnectionOptions {
|
||||
flavour: string;
|
||||
type: SpaceType;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export class IDBConnection extends AutoReconnectConnection<{
|
||||
db: IDBPDatabase<DocStorageSchema>;
|
||||
channel: BroadcastChannel;
|
||||
}> {
|
||||
readonly dbName = `${this.opts.peer}:${this.opts.type}:${this.opts.id}`;
|
||||
readonly dbName = `${this.opts.flavour}:${this.opts.type}:${this.opts.id}`;
|
||||
|
||||
override get shareId() {
|
||||
return `idb(${migrator.version}):${this.dbName}`;
|
||||
}
|
||||
|
||||
constructor(private readonly opts: StorageOptions) {
|
||||
constructor(private readonly opts: IDBConnectionOptions) {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,9 @@ import {
|
||||
type DocClocks,
|
||||
type DocRecord,
|
||||
DocStorageBase,
|
||||
type DocStorageOptions,
|
||||
type DocUpdate,
|
||||
} from '../../storage';
|
||||
import { IDBConnection } from './db';
|
||||
import { IDBConnection, type IDBConnectionOptions } from './db';
|
||||
import { IndexedDBLocker } from './lock';
|
||||
|
||||
interface ChannelMessage {
|
||||
@@ -15,7 +14,9 @@ interface ChannelMessage {
|
||||
origin?: string;
|
||||
}
|
||||
|
||||
export class IndexedDBDocStorage extends DocStorageBase {
|
||||
export class IndexedDBDocStorage extends DocStorageBase<IDBConnectionOptions> {
|
||||
static readonly identifier = 'IndexedDBDocStorage';
|
||||
|
||||
readonly connection = new IDBConnection(this.options);
|
||||
|
||||
get db() {
|
||||
@@ -30,10 +31,6 @@ export class IndexedDBDocStorage extends DocStorageBase {
|
||||
|
||||
private _lastTimestamp = new Date(0);
|
||||
|
||||
constructor(options: DocStorageOptions) {
|
||||
super(options);
|
||||
}
|
||||
|
||||
private generateTimestamp() {
|
||||
const timestamp = new Date();
|
||||
if (timestamp.getTime() <= this._lastTimestamp.getTime()) {
|
||||
|
||||
@@ -1,3 +1,20 @@
|
||||
import type { StorageConstructor } from '..';
|
||||
import { IndexedDBBlobStorage } from './blob';
|
||||
import { IndexedDBDocStorage } from './doc';
|
||||
import { IndexedDBSyncStorage } from './sync';
|
||||
import { IndexedDBV1BlobStorage, IndexedDBV1DocStorage } from './v1';
|
||||
|
||||
export * from './blob';
|
||||
export * from './doc';
|
||||
export * from './sync';
|
||||
|
||||
export const idbStorages = [
|
||||
IndexedDBDocStorage,
|
||||
IndexedDBBlobStorage,
|
||||
IndexedDBSyncStorage,
|
||||
] satisfies StorageConstructor[];
|
||||
|
||||
export const idbv1Storages = [
|
||||
IndexedDBV1DocStorage,
|
||||
IndexedDBV1BlobStorage,
|
||||
] satisfies StorageConstructor[];
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import { share } from '../../connection';
|
||||
import { BasicSyncStorage, type DocClock, type DocClocks } from '../../storage';
|
||||
import { IDBConnection } from './db';
|
||||
export class IndexedDBSyncStorage extends BasicSyncStorage {
|
||||
import { type DocClock, type DocClocks, SyncStorageBase } from '../../storage';
|
||||
import { IDBConnection, type IDBConnectionOptions } from './db';
|
||||
|
||||
export class IndexedDBSyncStorage extends SyncStorageBase {
|
||||
static readonly identifier = 'IndexedDBSyncStorage';
|
||||
|
||||
constructor(private readonly options: IDBConnectionOptions) {
|
||||
super();
|
||||
}
|
||||
|
||||
readonly connection = share(new IDBConnection(this.options));
|
||||
|
||||
get db() {
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
import { share } from '../../../connection';
|
||||
import { BlobStorageBase, type ListedBlobRecord } from '../../../storage';
|
||||
import { BlobIDBConnection } from './db';
|
||||
import { BlobIDBConnection, type BlobIDBConnectionOptions } from './db';
|
||||
|
||||
/**
|
||||
* @deprecated readonly
|
||||
*/
|
||||
export class IndexedDBV1BlobStorage extends BlobStorageBase {
|
||||
readonly connection = share(new BlobIDBConnection(this.spaceId));
|
||||
static readonly identifier = 'IndexedDBV1BlobStorage';
|
||||
|
||||
constructor(private readonly options: BlobIDBConnectionOptions) {
|
||||
super();
|
||||
}
|
||||
|
||||
readonly connection = share(new BlobIDBConnection(this.options));
|
||||
|
||||
get db() {
|
||||
return this.connection.inner;
|
||||
|
||||
@@ -42,19 +42,23 @@ export interface BlobDBSchema extends DBSchema {
|
||||
};
|
||||
}
|
||||
|
||||
export interface BlobIDBConnectionOptions {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export class BlobIDBConnection extends AutoReconnectConnection<
|
||||
IDBPDatabase<BlobDBSchema>
|
||||
> {
|
||||
constructor(private readonly workspaceId: string) {
|
||||
constructor(private readonly options: BlobIDBConnectionOptions) {
|
||||
super();
|
||||
}
|
||||
|
||||
override get shareId() {
|
||||
return `idb(old-blob):${this.workspaceId}`;
|
||||
return `idb(old-blob):${this.options.id}`;
|
||||
}
|
||||
|
||||
override async doConnect() {
|
||||
return openDB<BlobDBSchema>(`${this.workspaceId}_blob`, 1, {
|
||||
return openDB<BlobDBSchema>(`${this.options.id}_blob`, 1, {
|
||||
upgrade: db => {
|
||||
db.createObjectStore('blob');
|
||||
},
|
||||
|
||||
@@ -10,6 +10,8 @@ import { DocIDBConnection } from './db';
|
||||
* @deprecated readonly
|
||||
*/
|
||||
export class IndexedDBV1DocStorage extends DocStorageBase {
|
||||
static readonly identifier = 'IndexedDBV1DocStorage';
|
||||
|
||||
readonly connection = share(new DocIDBConnection());
|
||||
|
||||
get db() {
|
||||
|
||||
Reference in New Issue
Block a user