-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinding.ts
More file actions
166 lines (155 loc) · 4.62 KB
/
Copy pathbinding.ts
File metadata and controls
166 lines (155 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { parseExpressionAt, tokTypes } from "acorn";
import type { Atom } from "jotai";
import { atom } from "jotai";
import { groupBy, has, isNil, merge, omitBy } from "lodash-es";
import type { Expression } from "../shared";
import { isExpression, sanitizeJs, debug, error } from "../shared";
import {
createStorageApi,
screenStorageAtom,
} from "../hooks/useEnsembleStorage";
import { DateFormatter } from "../date/dateFormatter";
import {
themeAtom,
envAtom,
defaultScreenContext,
screenInputAtom,
widgetFamilyAtom,
screenGlobalScriptAtom,
screenImportScriptAtom,
userAtom,
appAtom,
secretAtom,
screenDataFamilyAtom,
} from "../state";
import { deviceAtom } from "../hooks/useDeviceObserver";
import { evaluate } from "./evaluate";
import { createEvaluationContext } from "./context";
export const createBindingAtom = <T = unknown>(
expression?: Expression<unknown>,
context?: { [key: string]: unknown },
widgetId?: string,
): Atom<T | undefined | null> => {
if (!isExpression(expression)) {
return atom(undefined);
}
const rawJsExpression = sanitizeJs(expression);
const identifiers: string[] = [];
try {
parseExpressionAt(rawJsExpression, 0, {
ecmaVersion: 2020,
onToken: (token) => {
if (token.type !== tokTypes.name) {
return;
}
const name = rawJsExpression.substring(token.start, token.end);
const isPrecededByDot =
rawJsExpression.charCodeAt(token.start - 1) === DOT_CHARCODE;
const isDeclaredInContext = context && name in context;
if (!isPrecededByDot && !isDeclaredInContext) {
identifiers.push(name);
}
},
});
} catch (e) {
error(e);
return atom(undefined);
}
const dependencyEntries = identifiers.map((identifier) => {
debug(`found dependency for ${String(widgetId)}: ${identifier}`);
const widgetDepAtom = widgetFamilyAtom(identifier);
const dataDepAtom = screenDataFamilyAtom(identifier);
// TODO: find a better way to distinguish data and widget identifiers
return { name: identifier, dependencies: [dataDepAtom, widgetDepAtom] };
});
const bindingAtom = atom((get) => {
const appData = get(appAtom);
const valueEntries = dependencyEntries.map(({ name, dependencies }) => {
let value;
for (const depAtom of dependencies) {
const depValue = get<unknown>(depAtom);
if (depValue) {
value = depValue;
break;
}
}
debug(
`value for dependency ${name} at ${String(widgetId)}: ${JSON.stringify(
value,
)}`,
);
return [name, value];
});
const values = groupBy(valueEntries, ([, value]) => {
// is widget state
if (has(value, "values") || has(value, "invokables")) {
return "widgets";
}
return "data";
});
const evaluationContext = createEvaluationContext({
applicationContext: {
application: {
languages: appData.application?.languages,
themes: appData.application?.themes,
},
selectedTheme: get(themeAtom),
env: get(envAtom),
secrets: get(secretAtom),
},
screenContext: {
inputs: get(screenInputAtom),
widgets: omitBy(Object.fromEntries(values.widgets ?? []), isNil),
data: omitBy(Object.fromEntries(values.data ?? []), isNil),
},
ensemble: {
storage: createStorageApi(
rawJsExpression.includes("ensemble.storage")
? get(screenStorageAtom)
: undefined,
undefined,
get,
),
user: rawJsExpression.includes("ensemble.user")
? get(userAtom)
: undefined,
env: rawJsExpression.includes("ensemble.env")
? get(envAtom)
: undefined,
secrets: rawJsExpression.includes("ensemble.secrets")
? get(secretAtom)
: undefined,
formatter: DateFormatter(),
},
context: {
...(rawJsExpression.includes("device")
? { device: get(deviceAtom) }
: {}),
...context,
},
});
try {
const result = evaluate<T>(
merge({}, defaultScreenContext, {
model: {
global: get(screenGlobalScriptAtom),
importedScripts: get(screenImportScriptAtom),
},
}),
rawJsExpression,
evaluationContext,
);
debug(
`result for ${rawJsExpression} at ${String(widgetId)}: ${JSON.stringify(
result,
)}`,
);
return result;
} catch (e) {
debug(e);
return null;
}
});
return bindingAtom;
};
const DOT_CHARCODE = 46; // .