Skip to content

Commit

Permalink
refactor: Use useCallback hook when using useEvalHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
munshkr committed Apr 3, 2024
1 parent 4d77f1c commit 1e05bae
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
9 changes: 3 additions & 6 deletions packages/web/src/hooks/use-eval-handler.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { EvalMessage } from "@flok-editor/session";
import { useEffect } from "react";

export function useEvalHandler(
cb: (evalMessage: EvalMessage) => void,
deps: any[]
) {
export function useEvalHandler(callback: (message: EvalMessage) => void) {
useEffect(() => {
const handleEval = (event: MessageEvent) => {
if (event.data.type === "eval") {
const msg = event.data.body as EvalMessage;
cb && cb(msg);
callback(msg);
}
};

Expand All @@ -18,7 +15,7 @@ export function useEvalHandler(
return () => {
window.removeEventListener("message", handleEval);
};
}, deps);
}, [callback]);

return;
}
14 changes: 8 additions & 6 deletions packages/web/src/routes/frames/hydra.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEvalHandler } from "@/hooks/use-eval-handler";
import { HydraWrapper } from "@/lib/hydra-wrapper";
import { sendToast } from "@/lib/utils";
import { isWebglSupported } from "@/lib/webgl-detector";
import { useMemo, useRef, useEffect, useState } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";

export function Component() {
const canvasRef = useRef<HTMLCanvasElement | null>(null);
Expand Down Expand Up @@ -41,11 +41,13 @@ export function Component() {
}, []);

useEvalHandler(
(msg) => {
if (!instance) return;
instance.tryEval(msg.body);
},
[instance]
useCallback(
(msg) => {
if (!instance) return;
instance.tryEval(msg.body);
},
[instance]
)
);

return hasWebGl && canvasRef && <HydraCanvas ref={canvasRef} fullscreen />;
Expand Down
14 changes: 8 additions & 6 deletions packages/web/src/routes/frames/mercury-web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEvalHandler } from "@/hooks/use-eval-handler";
import { MercuryWrapper } from "@/lib/mercury-wrapper";
import { sendToast } from "@/lib/utils";
import { type EvalMessage } from "@flok-editor/session";
import { useEffect, useState } from "react";
import { useCallback, useEffect, useState } from "react";

export function Component() {
const [instance, setInstance] = useState<any>(null);
Expand All @@ -23,11 +23,13 @@ export function Component() {
}, []);

useEvalHandler(
(msg: EvalMessage) => {
if (!instance) return;
instance.tryEval(msg.body);
},
[instance]
useCallback(
(msg: EvalMessage) => {
if (!instance) return;
instance.tryEval(msg.body);
},
[instance]
)
);

return null;
Expand Down
14 changes: 8 additions & 6 deletions packages/web/src/routes/frames/strudel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEvalHandler } from "@/hooks/use-eval-handler";
import { StrudelWrapper } from "@/lib/strudel-wrapper";
import { sendToast } from "@/lib/utils";
import { type EvalMessage } from "@flok-editor/session";
import { useEffect, useState } from "react";
import { useCallback, useEffect, useState } from "react";

export function Component() {
const [instance, setInstance] = useState<any>(null);
Expand All @@ -24,11 +24,13 @@ export function Component() {
}, []);

useEvalHandler(
(msg: EvalMessage) => {
if (!instance) return;
instance.tryEval(msg);
},
[instance]
useCallback(
(msg: EvalMessage) => {
if (!instance) return;
instance.tryEval(msg);
},
[instance]
)
);

return null;
Expand Down

0 comments on commit 1e05bae

Please sign in to comment.