mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-16 05:47:09 +08:00
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`
21 lines
510 B
TypeScript
21 lines
510 B
TypeScript
import type { Connection } from './connection';
|
|
|
|
const CONNECTIONS: Map<string, Connection<any>> = new Map();
|
|
export function share<T extends Connection<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].`
|
|
);
|
|
}
|
|
|
|
const existing = CONNECTIONS.get(conn.shareId);
|
|
|
|
if (existing) {
|
|
return existing as T;
|
|
}
|
|
|
|
CONNECTIONS.set(conn.shareId, conn);
|
|
|
|
return conn;
|
|
}
|