Skip to content

Commit

Permalink
fix(socket-errors): fixed (#1598)
Browse files Browse the repository at this point in the history
* fix(socket-errors): fixed
  • Loading branch information
BLuEScioN authored Apr 26, 2024
1 parent 8f1591a commit 73c1505
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,60 @@ import { StacksApiSocketClient } from '@stacks/blockchain-api-client';

export interface StacksApiSocketClientInfo {
client: StacksApiSocketClient | null;
connect: (handleOnConnect?: (client: StacksApiSocketClient) => void) => void;
connect: (
handleOnConnect?: (client: StacksApiSocketClient) => void,
handleError?: (client: StacksApiSocketClient | null) => void
) => void;
disconnect: () => void;
}

export function useStacksApiSocketClient(apiUrl: string): StacksApiSocketClientInfo {
const [socketClient, setSocketClient] = useState<StacksApiSocketClient | null>(null);
const socketUrlTracker = useRef<string | null>(null);
const isSocketClientConnecting = useRef(false);

const disconnect = useCallback(() => {
if (socketClient?.socket.connected) {
socketClient?.socket.removeAllListeners();
socketClient?.socket.close();
setSocketClient(null);
isSocketClientConnecting.current = false;
}
}, [socketClient]);

const connect = useCallback(
async (handleOnConnect?: (client: StacksApiSocketClient) => void) => {
async (
handleOnConnect?: (client: StacksApiSocketClient) => void,
handleError?: (client: StacksApiSocketClient | null) => void
) => {
if (!apiUrl) return;
if (socketClient?.socket.connected || isSocketClientConnecting.current) {
return;
}
try {
isSocketClientConnecting.current = true;
const socketUrl = apiUrl;
socketUrlTracker.current = socketUrl;
const client = await StacksApiSocketClient.connect({ url: socketUrl });
const client = await StacksApiSocketClient.connect({ url: apiUrl });
client.socket.on('connect', () => {
setSocketClient(client);
handleOnConnect?.(client);
isSocketClientConnecting.current = false;
});
client.socket.on('disconnect', () => {
setSocketClient(null);
isSocketClientConnecting.current = false;
client.socket.removeAllListeners();
client.socket.close();
disconnect();
});
client.socket.on('connect_error', error => {
setSocketClient(null);
isSocketClientConnecting.current = false;
client.socket.removeAllListeners();
client.socket.close();
disconnect();
handleError?.(client);
});
} catch (error) {
setSocketClient(null);
isSocketClientConnecting.current = false;
disconnect();
}
},
[apiUrl, socketClient]
[apiUrl, socketClient, disconnect]
);

const disconnect = useCallback(() => {
if (socketClient?.socket.connected) {
socketClient?.socket.close();
}
}, [socketClient]);

return {
client: socketClient,
connect,
Expand Down
13 changes: 7 additions & 6 deletions src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ interface Subscription {

export function useSubscribeBlocks(
liveUpdates: boolean,
handleBlock: (block: NakamotoBlock | Block) => void
handleBlock: (block: NakamotoBlock | Block) => void,
handleError?: (client: StacksApiSocketClient | null) => void
) {
const subscription = useRef<Subscription | undefined>(undefined);
const { stacksApiSocketClientInfo } = useGlobalContext();
const { client, connect, disconnect } = stacksApiSocketClientInfo || {};
const { connect, disconnect } = stacksApiSocketClientInfo || {};

useEffect(() => {
const subscribe = async (client: StacksApiSocketClient) => {
Expand All @@ -28,15 +29,15 @@ export function useSubscribeBlocks(
});
};

if (liveUpdates && !client?.socket.connected) {
connect?.(client => subscribe(client));
if (liveUpdates) {
connect?.(client => subscribe(client), handleError);
}
if (!liveUpdates && client?.socket.connected) {
if (!liveUpdates) {
disconnect?.();
}
return () => {
disconnect?.();
};
}, [client, handleBlock, connect, liveUpdates, disconnect]);
}, [handleBlock, connect, liveUpdates, disconnect, handleError]);
return subscription;
}

0 comments on commit 73c1505

Please sign in to comment.