output bindings when result is void

This commit is contained in:
Vyacheslav Matyukhin 2022-08-28 21:45:15 +04:00
parent 481483e937
commit b7aa126e6a
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
4 changed files with 32 additions and 4 deletions

View File

@ -254,7 +254,8 @@ export const ExpressionViewer: React.FC<Props> = ({
return ( return (
<VariableList path={path} heading="Module"> <VariableList path={path} heading="Module">
{(_) => {(_) =>
Object.entries(expression.value) expression.value
.entries()
.filter(([key, _]) => !key.match(/^(Math|System)\./)) .filter(([key, _]) => !key.match(/^(Math|System)\./))
.map(([key, r]) => ( .map(([key, r]) => (
<ExpressionViewer <ExpressionViewer

View File

@ -1,4 +1,10 @@
import { environment, run, SqValue } from "@quri/squiggle-lang"; import {
environment,
resultMap,
run,
SqValue,
SqValueTag,
} from "@quri/squiggle-lang";
import { useEffect, useMemo } from "react"; import { useEffect, useMemo } from "react";
type SquiggleArgs = { type SquiggleArgs = {
@ -12,8 +18,12 @@ type SquiggleArgs = {
export const useSquiggle = (args: SquiggleArgs) => { export const useSquiggle = (args: SquiggleArgs) => {
const result = useMemo( const result = useMemo(
() => { () => {
const { result } = run(args.code, { environment: args.environment }); const { result, bindings } = run(args.code, {
return result; environment: args.environment,
});
return resultMap(result, (v) =>
v.tag === SqValueTag.Void ? bindings.asValue() : v
);
}, },
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
[ [
@ -30,5 +40,7 @@ export const useSquiggle = (args: SquiggleArgs) => {
onChange?.(result.tag === "Ok" ? result.value : undefined); onChange?.(result.tag === "Ok" ? result.value : undefined);
}, [result, onChange]); }, [result, onChange]);
console.log(result);
return result; return result;
}; };

View File

@ -1,4 +1,5 @@
import * as RSModuleValue from "../rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Module.gen"; import * as RSModuleValue from "../rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Module.gen";
import { SqModuleValue, wrapValue } from "./SqValue";
export class SqModule { export class SqModule {
_value: RSModuleValue.squiggleValue_Module; _value: RSModuleValue.squiggleValue_Module;
@ -6,4 +7,14 @@ export class SqModule {
constructor(_value: RSModuleValue.squiggleValue_Module) { constructor(_value: RSModuleValue.squiggleValue_Module) {
this._value = _value; this._value = _value;
} }
entries() {
return RSModuleValue.getKeyValuePairs(this._value).map(
([k, v]) => [k, wrapValue(v)] as const
);
}
asValue() {
return new SqModuleValue(RSModuleValue.toSquiggleValue(this._value));
}
} }

View File

@ -8,3 +8,7 @@ let getKeyValuePairs = (v: squiggleValue_Module): array<(string, squiggleValue)>
@genType @genType
let toString = (v: squiggleValue_Module): string => let toString = (v: squiggleValue_Module): string =>
ReducerInterface_InternalExpressionValue.toStringNameSpace(v) ReducerInterface_InternalExpressionValue.toStringNameSpace(v)
@genType
let toSquiggleValue = (v: squiggleValue_Module): squiggleValue =>
IEvBindings(v)