Make code neccesary in hook

This commit is contained in:
Sam Nolan 2022-10-06 13:38:59 +11:00
parent f19c096e7c
commit a718b369c2

View File

@ -1,10 +1,10 @@
import { environment, SqProject, SqValue } from "@quri/squiggle-lang"; import { SqProject, SqValue } from "@quri/squiggle-lang";
import { useEffect, useMemo } from "react"; import { useEffect, useMemo } from "react";
import { JsImports, jsImportsToSquiggleCode } from "../jsImports"; import { JsImports, jsImportsToSquiggleCode } from "../jsImports";
import * as uuid from "uuid"; import * as uuid from "uuid";
type SquiggleArgs = { type SquiggleArgs = {
code?: string; code: string;
executionId?: number; executionId?: number;
jsImports?: JsImports; jsImports?: JsImports;
project: SqProject; project: SqProject;
@ -21,7 +21,7 @@ export const useSquiggle = (args: SquiggleArgs) => {
() => { () => {
const project = args.project; const project = args.project;
project.setSource(sourceName, args.code ?? ""); project.setSource(sourceName, args.code);
let includes = args.includes; let includes = args.includes;
if (args.jsImports && Object.keys(args.jsImports).length) { if (args.jsImports && Object.keys(args.jsImports).length) {
const importsSource = jsImportsToSquiggleCode(args.jsImports); const importsSource = jsImportsToSquiggleCode(args.jsImports);
@ -32,7 +32,7 @@ export const useSquiggle = (args: SquiggleArgs) => {
project.run(sourceName); project.run(sourceName);
const result = project.getResult(sourceName); const result = project.getResult(sourceName);
const bindings = project.getBindings(sourceName); const bindings = project.getBindings(sourceName);
return { result, bindings, sourceName }; return { result, bindings };
}, },
// This complains about executionId not being used inside the function body. // This complains about executionId not being used inside the function body.
// This is on purpose, as executionId simply allows you to run the squiggle // This is on purpose, as executionId simply allows you to run the squiggle
@ -53,17 +53,17 @@ export const useSquiggle = (args: SquiggleArgs) => {
useEffect(() => { useEffect(() => {
onChange?.( onChange?.(
result.result.tag === "Ok" ? result.result.value : undefined, result.result.tag === "Ok" ? result.result.value : undefined,
result.sourceName sourceName
); );
}, [result, onChange]); }, [result, onChange, sourceName]);
useEffect(() => { useEffect(() => {
return () => { return () => {
args.project.removeSource(result.sourceName); args.project.removeSource(sourceName);
if (args.project.getSource(importSourceName(result.sourceName))) if (args.project.getSource(importSourceName(sourceName)))
args.project.removeSource(result.sourceName); args.project.removeSource(sourceName);
}; };
}, [args.project, result.sourceName]); }, [args.project, sourceName]);
return result; return result;
}; };