fix(nbstore): connect before do operation (#11569)

This commit is contained in:
EYHN
2025-04-10 16:05:46 +08:00
committed by GitHub
parent 3629a725d2
commit a759a1988e
9 changed files with 150 additions and 104 deletions
@@ -16,6 +16,7 @@ export class BlobFrontend {
}
async get(blobId: string) {
await this.waitForConnected();
await using lock = await this.lock.lock('blob', blobId);
const local = await this.storage.get(blobId);
if (local) {
@@ -30,6 +31,7 @@ export class BlobFrontend {
}
async set(blob: BlobRecord) {
await this.waitForConnected();
if (blob.data.byteLength > this.maxBlobSize) {
for (const cb of this.onReachedMaxBlobSizeCallbacks) {
cb(blob.data.byteLength);
@@ -57,6 +59,7 @@ export class BlobFrontend {
* @throws This method will throw an error if the blob is not found locally, if the upload is aborted, or if it fails due to storage limitations.
*/
async upload(blobIdOrRecord: string | BlobRecord): Promise<boolean> {
await this.waitForConnected();
const blob =
typeof blobIdOrRecord === 'string'
? await this.storage.get(blobIdOrRecord)
@@ -84,4 +87,8 @@ export class BlobFrontend {
this.onReachedMaxBlobSizeCallbacks.add(cb);
return () => this.onReachedMaxBlobSizeCallbacks.delete(cb);
}
private waitForConnected(signal?: AbortSignal) {
return this.storage.connection.waitForConnected(signal);
}
}
@@ -1,5 +1,14 @@
import type { IndexerStorage } from '../storage';
import { switchMap } from 'rxjs';
import type {
AggregateOptions,
IndexerSchema,
IndexerStorage,
Query,
SearchOptions,
} from '../storage';
import type { IndexerSync } from '../sync/indexer';
import { fromPromise } from '../utils/from-promise';
export class IndexerFrontend {
constructor(
@@ -15,17 +24,50 @@ export class IndexerFrontend {
return this.sync.docState$(docId);
}
search = this.storage.search.bind(this.storage);
aggregate = this.storage.aggregate.bind(this.storage);
// eslint-disable-next-line rxjs/finnish
search$ = this.storage.search$.bind(this.storage);
// eslint-disable-next-line rxjs/finnish
aggregate$ = this.storage.aggregate$.bind(this.storage);
async search<T extends keyof IndexerSchema, const O extends SearchOptions<T>>(
table: T,
query: Query<T>,
options?: O
) {
await this.waitForConnected();
return this.storage.search(table, query, options);
}
async aggregate<
T extends keyof IndexerSchema,
const O extends AggregateOptions<T>,
>(table: T, query: Query<T>, field: keyof IndexerSchema[T], options?: O) {
await this.waitForConnected();
return this.storage.aggregate(table, query, field, options);
}
search$<T extends keyof IndexerSchema, const O extends SearchOptions<T>>(
table: T,
query: Query<T>,
options?: O
) {
return fromPromise(signal => this.waitForConnected(signal)).pipe(
switchMap(() => this.storage.search$(table, query, options))
);
}
aggregate$<
T extends keyof IndexerSchema,
const O extends AggregateOptions<T>,
>(table: T, query: Query<T>, field: keyof IndexerSchema[T], options?: O) {
return fromPromise(signal => this.waitForConnected(signal)).pipe(
switchMap(() => this.storage.aggregate$(table, query, field, options))
);
}
addPriority(docId: string, priority: number) {
return this.sync.addPriority(docId, priority);
}
private waitForConnected(signal?: AbortSignal) {
return this.storage.connection.waitForConnected(signal);
}
waitForCompleted(signal?: AbortSignal) {
return this.sync.waitForCompleted(signal);
}