mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-15 05:37:32 +00:00
feat(nbstore): add nbstore worker (#9185)
This commit is contained in:
@@ -8,7 +8,20 @@ export type ConnectionStatus =
|
||||
| 'error'
|
||||
| 'closed';
|
||||
|
||||
export abstract class Connection<T = any> {
|
||||
export interface Connection<T = any> {
|
||||
readonly status: ConnectionStatus;
|
||||
readonly inner: T;
|
||||
connect(): void;
|
||||
disconnect(): void;
|
||||
waitForConnected(signal?: AbortSignal): Promise<void>;
|
||||
onStatusChanged(
|
||||
cb: (status: ConnectionStatus, error?: Error) => void
|
||||
): () => void;
|
||||
}
|
||||
|
||||
export abstract class AutoReconnectConnection<T = any>
|
||||
implements Connection<T>
|
||||
{
|
||||
private readonly event = new EventEmitter2();
|
||||
private _inner: T | null = null;
|
||||
private _status: ConnectionStatus = 'idle';
|
||||
@@ -160,12 +173,22 @@ export abstract class Connection<T = any> {
|
||||
};
|
||||
}
|
||||
|
||||
export class DummyConnection extends Connection<undefined> {
|
||||
doConnect() {
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
export class DummyConnection implements Connection<undefined> {
|
||||
readonly status: ConnectionStatus = 'connected';
|
||||
readonly inner: undefined;
|
||||
|
||||
doDisconnect() {
|
||||
connect(): void {
|
||||
return;
|
||||
}
|
||||
disconnect(): void {
|
||||
return;
|
||||
}
|
||||
waitForConnected(_signal?: AbortSignal): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
onStatusChanged(
|
||||
_cb: (status: ConnectionStatus, error?: Error) => void
|
||||
): () => void {
|
||||
return () => {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Connection } from './connection';
|
||||
import type { AutoReconnectConnection } from './connection';
|
||||
|
||||
const CONNECTIONS: Map<string, Connection<any>> = new Map();
|
||||
export function share<T extends Connection<any>>(conn: T): T {
|
||||
const CONNECTIONS: Map<string, AutoReconnectConnection<any>> = new Map();
|
||||
export function share<T extends AutoReconnectConnection<any>>(conn: T): T {
|
||||
if (!conn.shareId) {
|
||||
throw new Error(
|
||||
`Connection ${conn.constructor.name} is not shareable.\nIf you want to make it shareable, please override [shareId].`
|
||||
|
||||
Reference in New Issue
Block a user