/ concept-collection / commonview
Sign in
concept-collection / commonview
commonview / src / useNetwork.ts
14 lines · 497 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 = (): {
9 snapshot: Snapshot
10 dispatch: Network['dispatch']
11} => {
12 const snapshot = useSyncExternalStore(network.subscribe, network.getSnapshot)
13 return {snapshot, dispatch: cmd => network.dispatch(cmd)}
moveopenescclose