import { createRoom , InternalRoom } from "./room" ; import { ClientOptions , Room , Client , Presence } from "./types" ; example export function createClient ( options ) { const clientOptions = options ; if ( typeof clientOptions . throttle === "number" ) { if ( clientOptions . throttle < 80 || clientOptions . throttle > 1000 ) { throw new Error ( "Liveblocks client throttle should be between 80 and 1000 ms" ) ; } } const rooms = new Map < string , InternalRoom > ( ) ; function getRoom ( roomId ) { const internalRoom = rooms . get ( roomId ) ; return internalRoom ? internalRoom . room : null ; } function enter < TStorageRoot > ( roomId , options = { } ) { let internalRoom = rooms . get ( roomId ) ; if ( internalRoom ) { return internalRoom . room ; } internalRoom = createRoom ( roomId , { ... clientOptions , ... options } ) ; rooms . set ( roomId , internalRoom ) ; internalRoom . connect ( ) ; return internalRoom . room ; } function leave ( roomId ) { let room = rooms . get ( roomId ) ; if ( room ) { room . disconnect ( ) ; rooms . delete ( roomId ) ; } } if ( typeof window !== "undefined" ) { window . addEventListener ( "online" , ( ) => { for ( const [ , room ] of rooms ) { room . onNavigatorOnline ( ) ; } } ) ; } if ( typeof document !== "undefined" ) { document . addEventListener ( "visibilitychange" , ( ) => { for ( const [ , room ] of rooms ) { room . onVisibilityChange ( document . visibilityState ) ; } } ) ; } return { getRoom , enter , leave , } ; }