/ concept-collection / commonroom
Sign in
concept-collection / commonroom
commonroom / src / useNetwork.ts
11 lines · 448 BBlameHistoryRaw
1import {useSyncExternalStore} from 'react'
2import {Network, type Snapshot} from './p2p/network'
4// A single Network instance for the whole app (module-level so React StrictMode
5// double-mounting doesn't create two peer networks).
6const network = new Network()
8export const useNetwork = (): {snapshot: Snapshot; network: Network} => {
9 const snapshot = useSyncExternalStore(network.subscribe, network.getSnapshot)
10 return {snapshot, network}
moveopenescclose