diff --git a/frontend/src/lib/components/apps/components/helpers/InputValue.svelte b/frontend/src/lib/components/apps/components/helpers/InputValue.svelte index d041f6118f..2d1423054d 100644 --- a/frontend/src/lib/components/apps/components/helpers/InputValue.svelte +++ b/frontend/src/lib/components/apps/components/helpers/InputValue.svelte @@ -84,7 +84,8 @@ true, $state, $mode == 'dnd', - $componentControl + $componentControl, + $worldStore ) error = '' return r @@ -103,7 +104,8 @@ true, $state, $mode == 'dnd', - $componentControl + $componentControl, + $worldStore ) error = '' return r diff --git a/frontend/src/lib/components/apps/components/helpers/RunnableComponent.svelte b/frontend/src/lib/components/apps/components/helpers/RunnableComponent.svelte index a7c4e3e355..57ba23a3c3 100644 --- a/frontend/src/lib/components/apps/components/helpers/RunnableComponent.svelte +++ b/frontend/src/lib/components/apps/components/helpers/RunnableComponent.svelte @@ -163,7 +163,8 @@ false, $state, $mode == 'dnd', - $componentControl + $componentControl, + $worldStore ) setResult(r) $state = $state diff --git a/frontend/src/lib/components/apps/components/helpers/eval.ts b/frontend/src/lib/components/apps/components/helpers/eval.ts index aca5e464d9..459c99936a 100644 --- a/frontend/src/lib/components/apps/components/helpers/eval.ts +++ b/frontend/src/lib/components/apps/components/helpers/eval.ts @@ -1,4 +1,3 @@ -import { goto } from '$app/navigation' import { sendUserToast } from '$lib/utils' import type { World } from '../../rx' @@ -10,7 +9,7 @@ export function computeGlobalContext( return { ...Object.fromEntries( Object.entries(world?.outputsById ?? {}) - .filter(([k, _]) => k != id) + .filter(([k, _]) => k != id && k != 'state') .map(([key, value]) => { return [ key, @@ -47,17 +46,29 @@ function make_context_evaluator( } export async function eval_like( - text, + text: string, context = {}, noReturn: boolean, - state: any, + state: Record, editor: boolean, - controlComponents: Record void }> + controlComponents: Record void }>, + worldStore: World | undefined ) { + const proxiedState = new Proxy(state, { + set(target, key, value) { + if (typeof key !== 'string') { + throw new Error('Invalid key') + } + let o = worldStore?.newOutput('state', key, value) + o?.set(value) + target[key] = value + return true + } + }) let evaluator = make_context_evaluator(text, context, noReturn) return await evaluator( context, - state, + proxiedState, async (x, newTab) => { if (newTab || editor) { if (!newTab) { diff --git a/frontend/src/lib/components/apps/editor/GridEditor.svelte b/frontend/src/lib/components/apps/editor/GridEditor.svelte index fdb981b156..36681dfa2d 100644 --- a/frontend/src/lib/components/apps/editor/GridEditor.svelte +++ b/frontend/src/lib/components/apps/editor/GridEditor.svelte @@ -13,6 +13,7 @@ import { push } from '$lib/history' import { expandGriditem, findGridItem } from './appUtils' import Grid from '../svelte-grid/Grid.svelte' + import { settableOutput } from '../rx' export let policy: Policy diff --git a/frontend/src/lib/components/apps/editor/contextPanel/ContextPanel.svelte b/frontend/src/lib/components/apps/editor/contextPanel/ContextPanel.svelte index 53f4cf0ec0..ebb3d904ab 100644 --- a/frontend/src/lib/components/apps/editor/contextPanel/ContextPanel.svelte +++ b/frontend/src/lib/components/apps/editor/contextPanel/ContextPanel.svelte @@ -75,14 +75,14 @@ /> - - {#key $state} - {#if Object.keys(filteredState).length > 0} - - {:else} -
No results
- {/if} - {/key} + + { + $connectingInput = connectInput($connectingInput, 'state', detail) + }} + /> diff --git a/frontend/src/lib/components/apps/editor/contextPanel/components/OutputHeader.svelte b/frontend/src/lib/components/apps/editor/contextPanel/components/OutputHeader.svelte index d24cf40bb1..5243e4ab80 100644 --- a/frontend/src/lib/components/apps/editor/contextPanel/components/OutputHeader.svelte +++ b/frontend/src/lib/components/apps/editor/contextPanel/components/OutputHeader.svelte @@ -47,7 +47,7 @@
{id}
-
+
{name} {#if !open && !manuallyOpen} diff --git a/frontend/src/lib/components/apps/rx.ts b/frontend/src/lib/components/apps/rx.ts index 607c9b6426..ed417b044b 100644 --- a/frontend/src/lib/components/apps/rx.ts +++ b/frontend/src/lib/components/apps/rx.ts @@ -23,6 +23,7 @@ export type World = { outputsById: Record>> connect: (inputSpec: AppInput, next: (x: T) => void, id?: string) => Input stateId: Writable + newOutput: (id: string, name: string, previousValue: T) => Output } export function buildWorld( @@ -38,7 +39,8 @@ export function buildWorld( Object.entries(context).map(([k, v]) => { return [k, newWorld.newOutput('ctx', k, stateId, v)] }) - ) + ), + state: previousWorld?.outputsById?.state ?? {} } for (const [k, outputs] of Object.entries(components)) { outputsById[k] = {} @@ -52,9 +54,20 @@ export function buildWorld( ) } } + function newOutput(id: string, name: string, previousValue: T) { + if (outputsById[id]?.[name]) { + return outputsById[id][name] + } + let o = newWorld.newOutput(id, name, stateId, previousValue) + if (!outputsById[id]) { + outputsById[id] = {} + } + outputsById[id][name] = o + return o + } stateId.update((x) => x + 1) - return { outputsById, connect: newWorld.connect, stateId } + return { outputsById, connect: newWorld.connect, stateId, newOutput } } export function buildObservableWorld() { @@ -166,7 +179,6 @@ export function settableOutput(state: Writable, previousValue: T): Ou state.update((x) => x + 1) value = x - subscribers.forEach((x) => x.next(value!)) } }