Skip to content

Commit 5609ae3

Browse files
committed
refactor: elevate isStopping to global state
1 parent 3ab82d3 commit 5609ae3

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/pages/Networks.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, useState } from "react";
1+
import { useState } from "react";
22
import * as log from "@tauri-apps/plugin-log";
33
import * as path from "@tauri-apps/api/path";
44
import { exists, mkdir } from "@tauri-apps/plugin-fs";
@@ -14,7 +14,6 @@ import {
1414
} from "../utils";
1515

1616
export function Networks() {
17-
const isStoppingRef = useRef(false);
1817
const [dlProgress, setDlProgress] = useState(0);
1918
const [networkId, setNetworkId] = useState("");
2019

@@ -31,6 +30,7 @@ export function Networks() {
3130
const consoleAddLine = useStore((s) => s.consoleAddLine);
3231
const setClientPid = useStore((s) => s.setClientPid);
3332
const setIsConnected = useStore((s) => s.setIsConnected);
33+
const setIsStopping = useStore((s) => s.setIsStopping);
3434
const setMessage = useStore((s) => s.setMessage);
3535
const setNetworkConnected = useStore((s) => s.setNetworkConnected);
3636
const setNetworks = useStore((s) => s.setNetworks);
@@ -53,7 +53,7 @@ export function Networks() {
5353

5454
async function disconnect() {
5555
try {
56-
isStoppingRef.current = true;
56+
setIsStopping(true);
5757
await clientStop();
5858
setMessage("info", "Disconnected from Network");
5959
} catch (error: any) {
@@ -163,14 +163,15 @@ export function Networks() {
163163

164164
command.on("close", (data) => {
165165
log.debug(`closed: ${cmd} code=${data.code} signal=${data.signal}`);
166-
if (!isStoppingRef.current) {
166+
const isStopping = useStore.getState().isStopping;
167+
if (isStopping !== true) {
167168
setMessage("error", "Error: Network connection failed.");
168169
consoleAddLine(`Network connection failed: ${networkId}`);
169170
}
170-
isStoppingRef.current = false;
171171
consoleAddLine(`Disconnected from network: ${networkId}`);
172172
setClientPid(0);
173173
setIsConnected(false);
174+
setIsStopping(false);
174175
setNetworkConnected("");
175176
});
176177

src/store/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const useStore = create(
1212
consoleLines: [] as string[],
1313
consoleLinesLimit: 100,
1414
isConnected: false,
15+
isStopping: false,
1516
isPlatformSupported: false,
1617
message: "",
1718
messageType: "",
@@ -30,6 +31,7 @@ export const useStore = create(
3031
setAppVersion: (appVersion: string) => set({ appVersion }),
3132
setClientPid: (clientPid: number) => set({ clientPid }),
3233
setIsConnected: (isConnected: boolean) => set({ isConnected }),
34+
setIsStopping: (isStopping: boolean) => set({ isStopping }),
3335
setIsPlatformSupported: (isPlatformSupported: boolean) =>
3436
set({ isPlatformSupported }),
3537
setMessage: (

0 commit comments

Comments
 (0)