mirror of
https://github.com/tulir/gomuks.git
synced 2025-04-20 10:33:41 -05:00
web: use helper instead of subscribing to event manually
This commit is contained in:
parent
758e3e9086
commit
3a68ec73f2
1 changed files with 4 additions and 9 deletions
|
@ -13,19 +13,18 @@
|
||||||
//
|
//
|
||||||
// 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, useState } from "react"
|
import { useEffect, useMemo } from "react"
|
||||||
import { ScaleLoader } from "react-spinners"
|
import { ScaleLoader } from "react-spinners"
|
||||||
|
import { useEventAsState } from "./util/eventdispatcher.ts"
|
||||||
import Client from "./api/client.ts"
|
import Client from "./api/client.ts"
|
||||||
import WSClient from "./api/wsclient.ts"
|
import WSClient from "./api/wsclient.ts"
|
||||||
import { ClientState } from "./api/types/hievents.ts"
|
|
||||||
import { ConnectionEvent } from "./api/rpc.ts"
|
|
||||||
import { LoginScreen, VerificationScreen } from "./ui/login"
|
import { LoginScreen, VerificationScreen } from "./ui/login"
|
||||||
import MainScreen from "./ui/MainScreen.tsx"
|
import MainScreen from "./ui/MainScreen.tsx"
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [connState, setConnState] = useState<ConnectionEvent>()
|
|
||||||
const [clientState, setClientState] = useState<ClientState>()
|
|
||||||
const client = useMemo(() => new Client(new WSClient("/_gomuks/websocket")), [])
|
const client = useMemo(() => new Client(new WSClient("/_gomuks/websocket")), [])
|
||||||
|
const connState = useEventAsState(client.rpc.connect)
|
||||||
|
const clientState = useEventAsState(client.state)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
((window as unknown) as { client: Client }).client = client
|
((window as unknown) as { client: Client }).client = client
|
||||||
|
|
||||||
|
@ -33,12 +32,8 @@ function App() {
|
||||||
const unlistenDebug = client.rpc.event.listen(ev => {
|
const unlistenDebug = client.rpc.event.listen(ev => {
|
||||||
console.debug("Received event:", ev)
|
console.debug("Received event:", ev)
|
||||||
})
|
})
|
||||||
const unlistenConnect = client.rpc.connect.listen(setConnState)
|
|
||||||
const unlistenState = client.state.listen(setClientState)
|
|
||||||
client.rpc.start()
|
client.rpc.start()
|
||||||
return () => {
|
return () => {
|
||||||
unlistenConnect()
|
|
||||||
unlistenState()
|
|
||||||
unlistenDebug()
|
unlistenDebug()
|
||||||
client.rpc.stop()
|
client.rpc.stop()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue