mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
fix websocket refactor mess
This commit is contained in:
parent
fea21b5057
commit
53639a8448
19 changed files with 179 additions and 209 deletions
19
webclient/src/hooks/useWebClient.tsx
Normal file
19
webclient/src/hooks/useWebClient.tsx
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { createContext, useContext, useState, ReactNode } from 'react';
|
||||
import { WebClient } from '@app/websocket';
|
||||
import { createWebClientRequest, createWebClientResponse } from '@app/api';
|
||||
|
||||
const WebClientContext = createContext<WebClient | null>(null);
|
||||
|
||||
export function WebClientProvider({ children }: { children: ReactNode }) {
|
||||
const [client] = useState(() => new WebClient(createWebClientRequest(), createWebClientResponse()));
|
||||
|
||||
return <WebClientContext value={client}>{children}</WebClientContext>;
|
||||
}
|
||||
|
||||
export function useWebClient(): WebClient {
|
||||
const client = useContext(WebClientContext);
|
||||
if (!client) {
|
||||
throw new Error('useWebClient must be used within a WebClientProvider');
|
||||
}
|
||||
return client;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue