Skip to content

Commit

Permalink
fix(instance) simplify loading state and avoid races on start/stoppin…
Browse files Browse the repository at this point in the history
…g of instances

Signed-off-by: David Edler <david.edler@canonical.com>
  • Loading branch information
edlerd committed Feb 13, 2024
1 parent c663825 commit a231995
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/context/instanceLoading.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import React, {
createContext,
FC,
ReactNode,
useContext,
useState,
} from "react";
import React, { createContext, FC, ReactNode, useContext } from "react";
import { LxdInstance } from "types/instance";

type LoadingTypes = "Starting" | "Stopping" | "Restarting" | "Freezing";
Expand All @@ -26,20 +20,14 @@ interface Props {
}

export const InstanceLoadingProvider: FC<Props> = ({ children }) => {
const [instanceStates, setInstanceStates] = useState(
new Map<string, LoadingTypes>(),
);
const instanceStates = new Map<string, LoadingTypes>();

const setLoading = (instance: LxdInstance, loadingType: LoadingTypes) => {
const newMap = new Map(instanceStates);
newMap.set(instance.name, loadingType);
setInstanceStates(newMap);
instanceStates.set(instance.name, loadingType);
};

const setFinish = (instance: LxdInstance) => {
const newMap = new Map(instanceStates);
newMap.delete(instance.name);
setInstanceStates(newMap);
instanceStates.delete(instance.name);
};

return (
Expand Down

0 comments on commit a231995

Please sign in to comment.