new jsImports (WIP)

This commit is contained in:
Vyacheslav Matyukhin 2022-09-01 17:26:11 +04:00
parent 407984344b
commit d76c2f8ac7
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
2 changed files with 38 additions and 10 deletions

View File

@ -4,12 +4,19 @@ import {
environment, environment,
defaultEnvironment, defaultEnvironment,
resultMap, resultMap,
SqValueLocation,
SqValueTag, SqValueTag,
} from "@quri/squiggle-lang"; } from "@quri/squiggle-lang";
import { useSquiggle } from "../lib/hooks"; import { useSquiggle } from "../lib/hooks";
import { SquiggleViewer } from "./SquiggleViewer"; import { SquiggleViewer } from "./SquiggleViewer";
type jsImports =
| number
| string
| jsImports[]
| {
[k: string]: jsImports;
};
export interface SquiggleChartProps { export interface SquiggleChartProps {
/** The input string for squiggle */ /** The input string for squiggle */
code?: string; code?: string;
@ -31,7 +38,7 @@ export interface SquiggleChartProps {
width?: number; width?: number;
height?: number; height?: number;
/** JS imported parameters */ /** JS imported parameters */
// jsImports?: jsImports; jsImports?: jsImports;
/** Whether to show a summary of the distribution */ /** Whether to show a summary of the distribution */
showSummary?: boolean; showSummary?: boolean;
/** Set the x scale to be logarithmic by deault */ /** Set the x scale to be logarithmic by deault */
@ -54,6 +61,7 @@ export interface SquiggleChartProps {
} }
const defaultOnChange = () => {}; const defaultOnChange = () => {};
const defaultImports = {};
export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo( export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
({ ({
@ -62,7 +70,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
environment, environment,
onChange = defaultOnChange, // defaultOnChange must be constant, don't move its definition here onChange = defaultOnChange, // defaultOnChange must be constant, don't move its definition here
height = 200, height = 200,
// jsImports = defaultImports, jsImports = defaultImports,
showSummary = false, showSummary = false,
width, width,
logX = false, logX = false,
@ -81,7 +89,7 @@ export const SquiggleChart: React.FC<SquiggleChartProps> = React.memo(
const { result, bindings } = useSquiggle({ const { result, bindings } = useSquiggle({
code, code,
environment, environment,
// jsImports, jsImports,
onChange, onChange,
executionId, executionId,
}); });

View File

@ -1,10 +1,18 @@
import { environment, run, SqValue } from "@quri/squiggle-lang"; import { environment, SqProject, SqValue } from "@quri/squiggle-lang";
import { useEffect, useMemo } from "react"; import { useEffect, useMemo } from "react";
export type jsImports =
| number
| string
| jsImports[]
| {
[k: string]: jsImports;
};
type SquiggleArgs = { type SquiggleArgs = {
code: string; code: string;
executionId?: number; executionId?: number;
// jsImports?: jsImports; jsImports?: jsImports;
environment?: environment; environment?: environment;
onChange?: (expr: SqValue | undefined) => void; onChange?: (expr: SqValue | undefined) => void;
}; };
@ -12,10 +20,22 @@ type SquiggleArgs = {
export const useSquiggle = (args: SquiggleArgs) => { export const useSquiggle = (args: SquiggleArgs) => {
const result = useMemo( const result = useMemo(
() => { () => {
const result = run(args.code, { const project = SqProject.create();
environment: args.environment, project.setSource("main", args.code);
}); if (args.environment) {
return result; project.setEnvironment(args.environment);
}
if (args.jsImports) {
console.log(JSON.stringify(args.jsImports));
project.setSource(
"zzz", // due to bug in topology implementation, can be renamed later
"imports = " + JSON.stringify(args.jsImports)
);
}
project.run("main");
const result = project.getResult("main");
const bindings = project.getBindings("main");
return { result, bindings };
}, },
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
[ [