/ concept-collection / hitandrun-commonview
Sign in
concept-collection / hitandrun-commonview
hitandrun-commonview / src / useNetwork.ts
16 lines · 644 BBlameHistoryRaw
1import {useSyncExternalStore} from 'react'
2import {Network, type Command, type Snapshot} from './p2p/network'
3import {Engine} from './engine/engine'
5// A single Network instance for the whole app (module-level so React StrictMode
6// double-mounting doesn't create two peer networks). The engine factory is only
7// invoked if/when this peer becomes central.
8const network = new Network(() => new Engine())
10export const useNetwork = (): {
11 snapshot: Snapshot
12 dispatch: (cmd: Command) => void
13} => {
14 const snapshot = useSyncExternalStore(network.subscribe, network.getSnapshot)
15 return {snapshot, dispatch: cmd => network.dispatch(cmd)}
moveopenescclose