mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-03 18:40:00 +08:00
feat(nbstore): remove async on connection api (#9187)
We should not use async on `connect` and `disconnect`, for `WebSocketConnection` will never connect when offline. We should handle the connection status of each storage in sync, using the `connection.waitForConnect` This PR also puts the connection reference count on the `connect` and disconnect`
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import EventEmitter2 from 'eventemitter2';
|
||||
|
||||
import type { ConnectionStatus } from '../connection';
|
||||
import type { AwarenessStorage } from './awareness';
|
||||
import type { BlobStorage } from './blob';
|
||||
import type { DocStorage } from './doc';
|
||||
@@ -39,59 +38,27 @@ export class SpaceStorage {
|
||||
return storage as Extract<Storages, { storageType: T }>;
|
||||
}
|
||||
|
||||
async connect() {
|
||||
await Promise.allSettled(
|
||||
Array.from(this.storages.values()).map(async storage => {
|
||||
// FIXME: multiple calls will register multiple listeners
|
||||
this.disposables.add(
|
||||
storage.connection.onStatusChanged((status, error) => {
|
||||
this.event.emit('connection', {
|
||||
storage: storage.storageType,
|
||||
status,
|
||||
error,
|
||||
});
|
||||
})
|
||||
);
|
||||
await storage.connect();
|
||||
})
|
||||
connect() {
|
||||
Array.from(this.storages.values()).forEach(storage => {
|
||||
storage.connect();
|
||||
});
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
Array.from(this.storages.values()).forEach(storage => {
|
||||
storage.disconnect();
|
||||
});
|
||||
}
|
||||
|
||||
async waitForConnected() {
|
||||
await Promise.all(
|
||||
Array.from(this.storages.values()).map(storage =>
|
||||
storage.waitForConnected()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
async disconnect() {
|
||||
await Promise.allSettled(
|
||||
Array.from(this.storages.values()).map(async storage => {
|
||||
await storage.disconnect();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
on(
|
||||
event: 'connection',
|
||||
cb: (payload: {
|
||||
storage: StorageType;
|
||||
status: ConnectionStatus;
|
||||
error?: Error;
|
||||
}) => void
|
||||
): () => void {
|
||||
this.event.on(event, cb);
|
||||
return () => {
|
||||
this.event.off(event, cb);
|
||||
};
|
||||
}
|
||||
|
||||
off(
|
||||
event: 'connection',
|
||||
cb: (payload: {
|
||||
storage: StorageType;
|
||||
status: ConnectionStatus;
|
||||
error?: Error;
|
||||
}) => void
|
||||
): void {
|
||||
this.event.off(event, cb);
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
await this.disconnect();
|
||||
this.disposables.forEach(disposable => disposable());
|
||||
this.event.removeAllListeners();
|
||||
this.storages.clear();
|
||||
|
||||
@@ -102,11 +102,15 @@ export abstract class Storage<Opts extends StorageOptions = StorageOptions> {
|
||||
|
||||
constructor(public readonly options: Opts) {}
|
||||
|
||||
async connect() {
|
||||
await this.connection.connect();
|
||||
connect() {
|
||||
this.connection.connect();
|
||||
}
|
||||
|
||||
async disconnect() {
|
||||
await this.connection.disconnect();
|
||||
disconnect() {
|
||||
this.connection.disconnect();
|
||||
}
|
||||
|
||||
async waitForConnected() {
|
||||
await this.connection.waitForConnected();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user