Skip to content

Commit

Permalink
fix refresh bug (#16)
Browse files Browse the repository at this point in the history
- Fixed bug with client-side error on refresh
  • Loading branch information
komlanKodoh authored Jan 27, 2024
1 parent a903d7c commit 200be92
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 41 deletions.
7 changes: 1 addition & 6 deletions wasm-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ extern "C" {
#[wasm_bindgen]
pub fn initialize_web_assembly() {
set_panic_hook();
log!("hello world {}", 45);
alert("Hello, wasm-lib!");
log!("Rust wasm has been initialized {}", 45);
}

#[wasm_bindgen]
pub fn current_state() -> String {
return "Dead".into();
}
10 changes: 4 additions & 6 deletions website/GraphUniverse/Graph/SimpleGraph/SimpleGraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphWrapper, initialize_web_assembly } from "wasm-lib";
import { GraphWrapper } from "wasm-lib";
import { GraphOperationMode } from './../Graph';
import { Edge, Graph, Vertex } from "@/GraphUniverse/Graph/Graph";
import { AnyValue } from "@/utils/types";
Expand All @@ -10,9 +10,7 @@ export default class SimpleGraph<V = AnyValue, E = AnyValue> {

private graph: GraphWrapper = new GraphWrapper();

constructor(){
initialize_web_assembly();
}
constructor() { }

public getWasmGraph(): GraphWrapper {
return this.graph;
Expand All @@ -29,9 +27,9 @@ export default class SimpleGraph<V = AnyValue, E = AnyValue> {
}

getVertex(vertexIndex: number): Vertex<V> {
const vertexData = this.vertexData.get(vertexIndex);
const vertexData = this.vertexData.get(vertexIndex);

if (vertexData === undefined){
if (vertexData === undefined) {
throw Error("Vertex data does not exists");
}

Expand Down
1 change: 1 addition & 0 deletions website/app/editor/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use client";
import { useWebAssembly } from "@/utils/hooks";
import dynamic from "next/dynamic";

const GraphContainer = dynamic(() => import("@/components/GraphContainer"), {
Expand Down
1 change: 0 additions & 1 deletion website/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default function RootLayout({
children: React.ReactNode;
}) {
useWebAssembly();

return (
<html lang="en">
<body className={inter.className}>{children}</body>
Expand Down
63 changes: 39 additions & 24 deletions website/components/Algorithms/GraphAlgorithmSelection.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import GraphUniverse from "@/GraphUniverse/GraphUniverse";
import { DijkstraAlgorithmForm } from "./DijkstraAlgorithm/DijkstraAlgorithmForm";
import { AlgorithmDopdownValue as AlgorithmDropdownValue } from "./AlgorithmDropdown";
import {
AlgorithmDopdownValue as AlgorithmDropdownValue,
GraphAlgorithms,
} from "./AlgorithmDropdown";
import { BreathFirstSearch } from "./BreathFirstAlgorithm/BreathFirstSearchAlgorithmForm";
import { BruteForceMinimumNodeColoringForm } from "./BruteForceMinimumNodecoloring/BruteForceMinimumNodeColoringForm";
import { AnimatePresence, motion } from "framer-motion";
Expand All @@ -26,30 +29,42 @@ function AnimationStuff({ children }: { children: ReactNode }) {
);
}

const componentMap = {
None: (universe: GraphUniverse) => {
return <></>;
},

"breath-first-traversal": (universe: GraphUniverse) => {
return (
<AnimationStuff key={1}>
<BreathFirstSearch universe={universe}></BreathFirstSearch>
</AnimationStuff>
);
},

dijkstra: (universe: GraphUniverse) => {
return (
<AnimationStuff key={2}>
<DijkstraAlgorithmForm universe={universe}></DijkstraAlgorithmForm>;
</AnimationStuff>
);
},

"minimum-node-coloring-brute": (universe: GraphUniverse) => {
return (
<AnimationStuff key={3}>
<BruteForceMinimumNodeColoringForm universe={universe}></BruteForceMinimumNodeColoringForm>
</AnimationStuff>
);
},
} as const;

export function GraphAlgorithmSelection({ name, universe }: GraphAlgorithmSelectionProps) {
return (
<div className="relative">
<AnimatePresence>
{name === "None" ? (
<></>
) : name === "breath-first-traversal" ? (
<AnimationStuff key={1}>
<BreathFirstSearch universe={universe}></BreathFirstSearch>
</AnimationStuff>
) : name === "dijkstra" ? (
<AnimationStuff key={2}>
<DijkstraAlgorithmForm universe={universe}></DijkstraAlgorithmForm>;
</AnimationStuff>
) : name === "minimum-node-coloring-brute" ? (
<AnimationStuff key={3}>
<BruteForceMinimumNodeColoringForm
universe={universe}
></BruteForceMinimumNodeColoringForm>
</AnimationStuff>
) : (
<></>
)}
</AnimatePresence>
</div>
<div className="relative">
<AnimatePresence>
{name != null? componentMap[name](universe): <></>}
</AnimatePresence>
</div>
);
}
11 changes: 10 additions & 1 deletion website/components/GraphContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import { Edge } from "@/GraphUniverse/Graph/Graph";
import { AlgorithmDopdownValue, AlgorithmDropdown } from "./Algorithms/AlgorithmDropdown";
import { GraphAlgorithmSelection } from "./Algorithms/GraphAlgorithmSelection";
import { AnimatePresence, motion } from "framer-motion";
import { useWebAssembly } from "@/utils/hooks";

export type GraphUniverseVertexData = {};

// Maximum independent set
// Node excentricity
export type GraphUniverseEdgeData = {};

function GraphContainer() {
export type GraphContainerProps = {}

function GraphContainerInner(props: GraphContainerProps) {
const containerRef = useRef<HTMLDivElement>(null);
const graphUniverse = useRef<GraphUniverse<any, any> | null>(null);

const [selectedEdge, setSelectedEdge] = useState<Edge<any, any> | null>(null);
const [editorState, setEditorState] = useState<WellKnownGraphUniverseState>();

Expand Down Expand Up @@ -144,4 +148,9 @@ function GraphContainer() {
);
}

function GraphContainer(props: GraphContainerProps){
var hasInit = useWebAssembly();

return hasInit ? <GraphContainerInner {...props}></GraphContainerInner>: <></>;
}
export default GraphContainer;
2 changes: 1 addition & 1 deletion website/components/forms/VertexInputButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function VertexInputButton<T, K extends keyof T>({
cleanupVertexSelected();
cleanupStateUpdate();
};
}, [universe, active]);
}, [universe, active, updateFormInput, updateFormInputMode]);

return (
<Button
Expand Down
2 changes: 1 addition & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"start": "next -p 8080 start",
"lint": "next lint"
},
"dependencies": {
Expand Down
15 changes: 14 additions & 1 deletion website/utils/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,20 @@ export function userReactiveRef<T>(initialValue: T): [MutableRefObject<T>, Dispa
}

export function useWebAssembly() {
const [hasInitialized, setHasInitialized] = useState<boolean>(false)

useEffect(() => {
rust_wasm_init();
new Promise<void>(async (resolve, reject) => {
await rust_wasm_init();
await initialize_web_assembly();

setHasInitialized(true);

resolve()
});

}, []);


return hasInitialized;
}

0 comments on commit 200be92

Please sign in to comment.