Skip to content

Commit

Permalink
fix(socket-errors): fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
BLuEScioN committed Apr 25, 2024
1 parent 8f1591a commit ad46eda
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,63 @@ 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);

Check warning on line 22 in src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts#L20-L22

Added lines #L20 - L22 were not covered by tests
}
}, [socketClient]);

const connect = useCallback(
async (handleOnConnect?: (client: StacksApiSocketClient) => void) => {
async (
handleOnConnect?: (client: StacksApiSocketClient) => void,
handleError?: (client: StacksApiSocketClient | null) => void

Check warning on line 29 in src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts#L29

Added line #L29 was not covered by tests
) => {
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 });

Check warning on line 37 in src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts#L37

Added line #L37 was not covered by tests
client.socket.on('connect', () => {
setSocketClient(client);
handleOnConnect?.(client);
isSocketClientConnecting.current = false;
});
client.socket.on('disconnect', () => {
setSocketClient(null);
client.socket.removeAllListeners();
client.socket.close();
disconnect();

Check warning on line 46 in src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts#L44-L46

Added lines #L44 - L46 were not covered by tests
isSocketClientConnecting.current = false;
});
client.socket.on('connect_error', error => {
setSocketClient(null);
client.socket.removeAllListeners();
client.socket.close();
disconnect();

Check warning on line 52 in src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts#L50-L52

Added lines #L50 - L52 were not covered by tests
isSocketClientConnecting.current = false;
handleError?.(client);

Check warning on line 54 in src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts#L54

Added line #L54 was not covered by tests
});
} catch (error) {
setSocketClient(null);
disconnect();

Check warning on line 57 in src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/use-stacks-api-socket-client.ts#L57

Added line #L57 was not covered by tests
isSocketClientConnecting.current = false;
}
},
[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

Check warning on line 15 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L15

Added line #L15 was not covered by tests
) {
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);

Check warning on line 33 in src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts

View check run for this annotation

Codecov / codecov/patch

src/app/_components/BlockList/Sockets/useSubscribeBlocks.ts#L33

Added line #L33 was not covered by tests
}
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 ad46eda

Please sign in to comment.