11import { parseExpressionAt , tokTypes } from "acorn" ;
22import type { Atom } from "jotai" ;
33import { atom } from "jotai" ;
4- import { isNil , merge , omitBy } from "lodash-es" ;
4+ import { groupBy , has , isNil , merge , omitBy } from "lodash-es" ;
55import type { Expression } from "../shared" ;
66import { isExpression , sanitizeJs , debug , error } from "../shared" ;
77import {
@@ -13,14 +13,14 @@ import {
1313 themeAtom ,
1414 envAtom ,
1515 defaultScreenContext ,
16- screenDataAtom ,
1716 screenInputAtom ,
1817 widgetFamilyAtom ,
1918 screenGlobalScriptAtom ,
2019 screenImportScriptAtom ,
2120 userAtom ,
2221 appAtom ,
2322 secretAtom ,
23+ screenDataFamilyAtom ,
2424} from "../state" ;
2525import { deviceAtom } from "../hooks/useDeviceObserver" ;
2626import { evaluate } from "./evaluate" ;
@@ -62,16 +62,23 @@ export const createBindingAtom = <T = unknown>(
6262
6363 const dependencyEntries = identifiers . map ( ( identifier ) => {
6464 debug ( `found dependency for ${ String ( widgetId ) } : ${ identifier } ` ) ;
65- // TODO: Account for data bindings also
66- const dependencyAtom = widgetFamilyAtom ( identifier ) ;
67- return { name : identifier , dependencyAtom } ;
65+ const widgetDepAtom = widgetFamilyAtom ( identifier ) ;
66+ const dataDepAtom = screenDataFamilyAtom ( identifier ) ;
67+ // TODO: find a better way to distinguish data and widget identifiers
68+ return { name : identifier , dependencies : [ dataDepAtom , widgetDepAtom ] } ;
6869 } ) ;
6970
7071 const bindingAtom = atom ( ( get ) => {
71- const data = get ( screenDataAtom ) ;
7272 const appData = get ( appAtom ) ;
73- const valueEntries = dependencyEntries . map ( ( { name, dependencyAtom } ) => {
74- const value = get ( dependencyAtom ) ;
73+ const valueEntries = dependencyEntries . map ( ( { name, dependencies } ) => {
74+ let value ;
75+ for ( const depAtom of dependencies ) {
76+ const depValue = get < unknown > ( depAtom ) ;
77+ if ( depValue ) {
78+ value = depValue ;
79+ break ;
80+ }
81+ }
7582 debug (
7683 `value for dependency ${ name } at ${ String ( widgetId ) } : ${ JSON . stringify (
7784 value ,
@@ -80,6 +87,14 @@ export const createBindingAtom = <T = unknown>(
8087 return [ name , value ] ;
8188 } ) ;
8289
90+ const values = groupBy ( valueEntries , ( [ , value ] ) => {
91+ // is widget state
92+ if ( has ( value , "values" ) || has ( value , "invokables" ) ) {
93+ return "widgets" ;
94+ }
95+ return "data" ;
96+ } ) ;
97+
8398 const evaluationContext = createEvaluationContext ( {
8499 applicationContext : {
85100 application : {
@@ -92,8 +107,8 @@ export const createBindingAtom = <T = unknown>(
92107 } ,
93108 screenContext : {
94109 inputs : get ( screenInputAtom ) ,
95- widgets : omitBy ( Object . fromEntries ( valueEntries ) , isNil ) ,
96- data,
110+ widgets : omitBy ( Object . fromEntries ( values . widgets ?? [ ] ) , isNil ) ,
111+ data : omitBy ( Object . fromEntries ( values . data ?? [ ] ) , isNil ) ,
97112 } ,
98113 ensemble : {
99114 storage : createStorageApi (
0 commit comments