import raf from 'random-access-file' ; import DiscoverySwarm from 'discovery-cloud-client' ; import * as ReactDOM from 'react-dom' ; import React from 'react' ; import { RepoBackend , DocUrl } from 'hypermerge' ; import { ToBackendRepoMsg } from 'hypermerge/dist/RepoMsg' ; import { Socket } from 'net' ; import ipc from '../ipc' ; import { HYPERMERGE_PATH , FILE_SERVER_PATH } from '../renderer/constants' ; import Root from './components/Root' ; import { ToSystemMsg } from '../renderer/System' ; window . _debug = { } ; const back = new RepoBackend ( { storage : raf , path : HYPERMERGE_PATH } ) ; const url = 'wss://discovery-cloud.herokuapp.com' ; const discovery = new DiscoverySwarm ( { url , id : back . id , stream : back . stream } ) ; back . setSwarm ( discovery as any ) ; back . startFileServer ( FILE_SERVER_PATH ) ; window . _debug . repo = back ; window . _debug . discovery = discovery ; ipc . config . id = 'background' ; ipc . serve ( ( ) => { ipc . server . on ( 'repo.msg' , ( msg ) => { back . receive ( msg ) ; } ) ; ipc . server . on ( 'system.msg' , ( msg ) => { switch ( msg . type ) { case 'Navigated' : mount ( msg . url , Root ) ; break ; } } ) ; ipc . server . on ( 'connect' , ( socket ) => { back . subscribe ( ( msg ) => ipc . server . emit ( socket , 'repo.msg' , msg ) ) ; } ) ; } ) ; ipc . server . start ( ) ; const container = document . createElement ( 'div' ) ; container . id = 'app' ; document . body . appendChild ( container ) ; let currentUrl ; function mount ( url , NewRoot ) { currentUrl = url ; try { ReactDOM . render ( < NewRoot repo = { back } currentUrl = { url } / > , container ) ; } catch ( e ) { console . error ( 'mount error' , e ) ; } } if ( module . hot ) { module . hot . accept ( './components/Root.tsx' , ( ) => { const NextRoot = require ( './components/Root' ) . default ; mount ( currentUrl , NextRoot ) ; } ) ; }