web/app: initialize RPC client outside React

This commit is contained in:
Tulir Asokan 2024-10-28 00:05:48 +02:00
parent 0920c06077
commit 704ca2ca42

View file

@ -13,7 +13,7 @@
// //
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
import { useEffect, useMemo } from "react" import { useEffect } from "react"
import { ScaleLoader } from "react-spinners" import { ScaleLoader } from "react-spinners"
import Client from "./api/client.ts" import Client from "./api/client.ts"
import WSClient from "./api/wsclient.ts" import WSClient from "./api/wsclient.ts"
@ -22,19 +22,20 @@ import MainScreen from "./ui/MainScreen.tsx"
import { LoginScreen, VerificationScreen } from "./ui/login" import { LoginScreen, VerificationScreen } from "./ui/login"
import { LightboxWrapper } from "./ui/modal/Lightbox.tsx" import { LightboxWrapper } from "./ui/modal/Lightbox.tsx"
import { ModalWrapper } from "./ui/modal/Modal.tsx" import { ModalWrapper } from "./ui/modal/Modal.tsx"
import { useEventAsState } from "./util/eventdispatcher.ts" import { useCachedEventAsState } from "./util/eventdispatcher.ts"
const client = new Client(new WSClient("_gomuks/websocket"))
window.client = client
function App() { function App() {
const client = useMemo(() => new Client(new WSClient("_gomuks/websocket")), []) const connState = useCachedEventAsState(client.rpc.connect)
const connState = useEventAsState(client.rpc.connect) const clientState = useCachedEventAsState(client.state)
const clientState = useEventAsState(client.state)
window.client = client
useEffect(() => { useEffect(() => {
Notification.requestPermission() Notification.requestPermission()
.then(permission => console.log("Notification permission:", permission)) .then(permission => console.log("Notification permission:", permission))
client.rpc.start() client.rpc.start()
return () => client.rpc.stop() return () => client.rpc.stop()
}, [client]) }, [])
if (connState?.error) { if (connState?.error) {
return <div> return <div>