Files
AFFiNE-Mirror/packages/common/nbstore/src/connection/shared-connection.ts
T
forehalo 4125038ff8 feat(nbstore): init (#7639)
TODO

- [x] basic
- [x] storages
- [x] producer/consumer
- [x] operation pattern
- [x] events
- [x] worker
- [x] readme
- [x] peer dependencies
2024-11-22 03:13:04 +00:00

23 lines
544 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) {
existing.ref();
return existing as T;
}
CONNECTIONS.set(conn.shareId, conn);
conn.ref();
return conn;
}