Skip to content

Commit e7e0cd7

Browse files
committed
test時だけルール違反してrefを使う。
1 parent 13d679f commit e7e0cd7

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

app/terminal/page.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { Heading } from "@/markdown/heading";
44
import "mocha/mocha.css";
5-
import { Fragment, useEffect, useState } from "react";
5+
import { Fragment, useEffect, useRef, useState } from "react";
66
import { langConstants, RuntimeLang } from "@my-code/runtime/languages";
77
import { ReplTerminal } from "./repl";
88
import { EditorComponent } from "./editor";
@@ -202,7 +202,11 @@ function AnsiColorSample() {
202202
}
203203

204204
function MochaTest() {
205-
const runtimeRef = useRuntimeAll();
205+
const runtimeAll = useRuntimeAll();
206+
const runtimeRef = useRef(runtimeAll);
207+
for (const lang of Object.keys(runtimeAll) as RuntimeLang[]) {
208+
runtimeRef.current[lang] = runtimeAll[lang];
209+
}
206210

207211
const [searchParams, setSearchParams] = useState<string>("");
208212
useEffect(() => {

packages/runtime/src/context.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { ReactNode, RefObject, useEffect, useRef } from "react";
3+
import { ReactNode, useEffect } from "react";
44
import { RuntimeContext } from "./interface";
55
import { RuntimeLang } from "./languages";
66
import { TypeScriptProvider, useTypeScript } from "./typescript/runtime";
@@ -12,14 +12,14 @@ import { WorkerProvider } from "./worker/runtime";
1212

1313
export function useRuntime(language: RuntimeLang): RuntimeContext {
1414
const runtimes = useRuntimeAll();
15-
const runtime = runtimes.current[language];
15+
const runtime = runtimes[language];
1616
const { init } = runtime;
1717
useEffect(() => {
1818
init?.();
1919
}, [init]);
2020
return runtime;
2121
}
22-
export function useRuntimeAll(): RefObject<Record<RuntimeLang, RuntimeContext>> {
22+
export function useRuntimeAll(): Record<RuntimeLang, RuntimeContext> {
2323
// すべての言語のcontextをインスタンス化
2424
const pyodide = usePyodide();
2525
const ruby = useRuby();
@@ -28,16 +28,15 @@ export function useRuntimeAll(): RefObject<Record<RuntimeLang, RuntimeContext>>
2828
const wandboxCpp = useWandbox("cpp");
2929
const wandboxRust = useWandbox("rust");
3030

31-
const runtimes = useRef<Record<RuntimeLang, RuntimeContext>>({} as never);
32-
runtimes.current.python = pyodide;
33-
runtimes.current.ruby = ruby;
34-
runtimes.current.javascript = jsEval;
35-
runtimes.current.typescript = typescript;
36-
runtimes.current.cpp = wandboxCpp;
37-
runtimes.current.rust = wandboxRust;
38-
3931
// initはしない。呼び出し側でする必要がある
40-
return runtimes;
32+
return {
33+
python: pyodide,
34+
ruby: ruby,
35+
javascript: jsEval,
36+
typescript: typescript,
37+
cpp: wandboxCpp,
38+
rust: wandboxRust,
39+
};
4140
}
4241
export function RuntimeProvider({ children }: { children: ReactNode }) {
4342
return (

packages/runtime/tests/vitest-all.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ const RuntimeLoader = ({
1414
}: {
1515
runtimeRef: RefObject<Record<RuntimeLang, RuntimeContext> | null>;
1616
}) => {
17-
const runtimes = useRuntimeAll();
18-
runtimeRef.current = runtimes.current;
17+
const runtimeAll = useRuntimeAll();
18+
// eslint-disable-next-line react-hooks/refs
19+
runtimeRef.current = runtimeAll;
1920
return null;
2021
};
2122

0 commit comments

Comments
 (0)