embedded-react 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,351 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /*----------------------------------------------------------------------------------------------------------------------
17
+ - Implementation
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ /**
20
+ * Records what a local reads: a list row's item reads its list, and any other local reads what it was set from.
21
+ * A local with neither is untracked.
22
+ *
23
+ * @param local The local.
24
+ * @param found What the walk has found.
25
+ */
26
+ function readLocal(local, found) {
27
+ if (local.list) {
28
+ found.states.add(local.list);
29
+ }
30
+ else if (local.source) {
31
+ readSource(local.source, found);
32
+ }
33
+ else {
34
+ found.untracked = true;
35
+ }
36
+ }
37
+ /**
38
+ * Records what an expression, before typing, reads.
39
+ *
40
+ * @param source The expression.
41
+ * @param found What the walk has found.
42
+ */
43
+ function readSource(source, found) {
44
+ switch (source.kind) {
45
+ case 'number':
46
+ case 'string':
47
+ case 'boolean':
48
+ return;
49
+ case 'state':
50
+ found.states.add(source.state);
51
+ return;
52
+ case 'local':
53
+ readLocal(source.local, found);
54
+ return;
55
+ case 'ref':
56
+ found.untracked = true;
57
+ return;
58
+ case 'fixed':
59
+ readExpr(source.expr, found);
60
+ return;
61
+ case 'unary':
62
+ readSource(source.operand, found);
63
+ return;
64
+ case 'binary':
65
+ case 'logical':
66
+ readSource(source.left, found);
67
+ readSource(source.right, found);
68
+ return;
69
+ case 'conditional':
70
+ readSource(source.test, found);
71
+ readSource(source.consequent, found);
72
+ readSource(source.alternate, found);
73
+ return;
74
+ case 'math':
75
+ source.args.forEach(arg => readSource(arg, found));
76
+ return;
77
+ }
78
+ }
79
+ /**
80
+ * Records what an expression reads.
81
+ *
82
+ * @param expr The expression.
83
+ * @param found What the walk has found.
84
+ */
85
+ function readExpr(expr, found) {
86
+ switch (expr.kind) {
87
+ case 'number':
88
+ case 'string':
89
+ case 'boolean':
90
+ case 'pi':
91
+ return;
92
+ case 'state':
93
+ found.states.add(expr.state);
94
+ return;
95
+ case 'boot':
96
+ found.boot = true;
97
+ return;
98
+ case 'local':
99
+ readLocal(expr.local, found);
100
+ return;
101
+ case 'listLength':
102
+ found.states.add(expr.list);
103
+ return;
104
+ case 'itemField':
105
+ readLocal(expr.item, found);
106
+ return;
107
+ case 'ref':
108
+ case 'clock':
109
+ case 'event':
110
+ case 'layout':
111
+ case 'gesture':
112
+ found.untracked = true;
113
+ return;
114
+ case 'toFloat':
115
+ case 'unary':
116
+ case 'wholeRound':
117
+ case 'trunc':
118
+ case 'toInt':
119
+ readExpr(expr.operand, found);
120
+ return;
121
+ case 'helper':
122
+ case 'floatMath':
123
+ expr.args.forEach(arg => readExpr(arg, found));
124
+ return;
125
+ case 'arithmetic':
126
+ case 'floatDivide':
127
+ case 'narrowRemainder':
128
+ case 'compare':
129
+ case 'stringEquals':
130
+ case 'logical':
131
+ case 'pick':
132
+ readExpr(expr.left, found);
133
+ readExpr(expr.right, found);
134
+ return;
135
+ case 'conditional':
136
+ readExpr(expr.test, found);
137
+ readExpr(expr.consequent, found);
138
+ readExpr(expr.alternate, found);
139
+ return;
140
+ }
141
+ }
142
+ /**
143
+ * Records what a conditional's show condition reads, the conditions it sits within included.
144
+ *
145
+ * @param show The condition.
146
+ * @param found What the walk has found.
147
+ */
148
+ function readShow(show, found) {
149
+ if (show.kind === 'rowInUse') {
150
+ found.states.add(show.list);
151
+ return;
152
+ }
153
+ readExpr(show.test, found);
154
+ if (show.within) {
155
+ readShow(show.within, found);
156
+ }
157
+ }
158
+ /**
159
+ * Records what a state-driven prop value reads.
160
+ *
161
+ * @param value The value.
162
+ * @param found What the walk has found.
163
+ */
164
+ function readPropValue(value, found) {
165
+ switch (value.kind) {
166
+ case 'constant':
167
+ return;
168
+ case 'select':
169
+ readExpr(value.test, found);
170
+ readPropValue(value.whenTrue, found);
171
+ readPropValue(value.whenFalse, found);
172
+ return;
173
+ case 'opacity':
174
+ case 'dimension':
175
+ case 'visible':
176
+ case 'float':
177
+ readExpr(value.value, found);
178
+ return;
179
+ case 'shown':
180
+ readShow(value.show, found);
181
+ return;
182
+ case 'whileShown':
183
+ readShow(value.show, found);
184
+ readPropValue(value.value, found);
185
+ return;
186
+ case 'flag':
187
+ readExpr(value.test, found);
188
+ return;
189
+ case 'byte':
190
+ readPropValue(value.value, found);
191
+ return;
192
+ }
193
+ }
194
+ /**
195
+ * Records what a text reads.
196
+ *
197
+ * @param text The text.
198
+ * @param found What the walk has found.
199
+ */
200
+ function readText(text, found) {
201
+ for (const part of text.parts) {
202
+ if (part.kind !== 'literal') {
203
+ readExpr(part.value, found);
204
+ }
205
+ }
206
+ }
207
+ /**
208
+ * Records what one slot of a drawing's op-tape reads.
209
+ *
210
+ * @param value The slot.
211
+ * @param found What the walk has found.
212
+ */
213
+ function readTapeValue(value, found) {
214
+ switch (value.kind) {
215
+ case 'op':
216
+ case 'number':
217
+ case 'local':
218
+ case 'fullTurn':
219
+ return;
220
+ case 'float':
221
+ readExpr(value.value, found);
222
+ return;
223
+ case 'chain':
224
+ readTapeValue(value.first, found);
225
+ value.rest.forEach(step => readTapeValue(step.value, found));
226
+ return;
227
+ case 'radians':
228
+ readTapeValue(value.degrees, found);
229
+ return;
230
+ case 'clampRadius':
231
+ readTapeValue(value.radius, found);
232
+ readTapeValue(value.side, found);
233
+ return;
234
+ }
235
+ }
236
+ /**
237
+ * Records what a paint color reads.
238
+ *
239
+ * @param color The color.
240
+ * @param found What the walk has found.
241
+ */
242
+ function readPaintColor(color, found) {
243
+ if (color.kind === 'state') {
244
+ readPropValue(color.value, found);
245
+ }
246
+ }
247
+ /**
248
+ * What a walk found, as reads.
249
+ *
250
+ * @param found What the walk found.
251
+ *
252
+ * @returns The state read, or null when something untracked is read too.
253
+ */
254
+ const readsOf = (found) => found.untracked ? null : found.states;
255
+ /**
256
+ * The state a node's props, text and image read. app_update() sets them all again together, so this is
257
+ * everything any of them reads.
258
+ *
259
+ * @param node The node.
260
+ *
261
+ * @returns Its reads.
262
+ */
263
+ export const nodeReads = (node) => readsOf(nodeFound(node));
264
+ /**
265
+ * Whether a node's runtime props read only values worked out at boot, so er_app_build() sets them once.
266
+ *
267
+ * @param node The node.
268
+ *
269
+ * @returns True when they do.
270
+ */
271
+ export const isBootOnlyNode = (node) => isBootOnly(nodeFound(node));
272
+ /**
273
+ * Whether a drawing reads only values worked out at boot, so er_app_build() builds it once.
274
+ *
275
+ * @param vector The drawing.
276
+ *
277
+ * @returns True when it does.
278
+ */
279
+ export const isBootOnlyVector = (vector) => isBootOnly(vectorFound(vector));
280
+ /**
281
+ * Whether a walk found boot values and nothing that changes.
282
+ *
283
+ * @param found What the walk found.
284
+ *
285
+ * @returns True when it did.
286
+ */
287
+ const isBootOnly = (found) => !!found.boot && !found.untracked && found.states.size === 0;
288
+ /**
289
+ * Walks everything a node's props, text and image read.
290
+ *
291
+ * @param node The node.
292
+ *
293
+ * @returns What the walk found.
294
+ */
295
+ function nodeFound(node) {
296
+ const found = { states: new Set(), untracked: false };
297
+ node.stateProps.forEach(prop => readPropValue(prop.value, found));
298
+ if (node.text) {
299
+ readText(node.text, found);
300
+ }
301
+ if (node.image?.kind === 'value') {
302
+ readExpr(node.image.value, found);
303
+ }
304
+ return found;
305
+ }
306
+ /**
307
+ * The state a drawing reads: its locals and tape, and its paints and gradients.
308
+ *
309
+ * @param vector The drawing.
310
+ *
311
+ * @returns Its reads.
312
+ */
313
+ export const vectorReads = (vector) => readsOf(vectorFound(vector));
314
+ /**
315
+ * Walks everything a drawing reads.
316
+ *
317
+ * @param vector The drawing.
318
+ *
319
+ * @returns What the walk found.
320
+ */
321
+ function vectorFound(vector) {
322
+ const found = { states: new Set(), untracked: false };
323
+ vector.locals.forEach(local => readTapeValue(local.value, found));
324
+ vector.tape.forEach(value => readTapeValue(value, found));
325
+ for (const paint of vector.paints) {
326
+ readPaintColor(paint.fill, found);
327
+ readPaintColor(paint.stroke, found);
328
+ readTapeValue(paint.strokeWidth, found);
329
+ for (const gradient of [paint.fillGradient, paint.strokeGradient]) {
330
+ if (gradient.when) {
331
+ readExpr(gradient.when.test, found);
332
+ }
333
+ }
334
+ }
335
+ for (const gradient of vector.gradients) {
336
+ for (const stop of gradient.stops) {
337
+ readPaintColor(stop.color, found);
338
+ readTapeValue(stop.offset, found);
339
+ }
340
+ for (const value of [
341
+ gradient.ax,
342
+ gradient.ay,
343
+ gradient.bx,
344
+ gradient.by,
345
+ gradient.r,
346
+ ]) {
347
+ readTapeValue(value, found);
348
+ }
349
+ }
350
+ return found;
351
+ }
@@ -19,8 +19,9 @@
19
19
  import { checkNumber, lowerWithChecks, runChecks } from './check.mjs';
20
20
  import { aotError } from './diagnostics.mjs';
21
21
  import { evalStatic, withUndefined } from './static-eval.mjs';
22
- import { findShadowedClockCalls, emitSourcedWide } from './expressions.mjs';
23
- import { findComponent, declaredNames, moduleScope, collectState, findReturnJSX, collectComponents, collectHelpers, collectImageImports, collectSvgImports, collectCallbacks, collectMemos, collectEffects, } from './collect.mjs';
22
+ import { abandonBoot, beginBoot, endBoot, setBootLowering } from './boot.mjs';
23
+ import { findShadowedClockCalls, emitSourcedWide, lowerExpr, } from './expressions.mjs';
24
+ import { findComponent, declaredNames, moduleScope, collectState, findReturnJSX, collectComponents, collectHelpers, collectImageImports, collectSvgImports, collectCallbacks, collectMemos, collectEffects, bindConst, } from './collect.mjs';
24
25
  import { collectAnims, collectRefs } from './animations.mjs';
25
26
  import { lowerEffect } from './handlers.mjs';
26
27
  import { collectPanResponders } from './pan-responder.mjs';
@@ -28,7 +29,8 @@ import { emitNode } from './nodes.mjs';
28
29
  import { lowerKeyboardConfig } from './keyboard.mjs';
29
30
  import { normalizeUndefined, parseApp } from './parse.mjs';
30
31
  import { Out } from './out.mjs';
31
- import { isStateDriven, nodesOf, stateDrivenVectorOf } from './ir/nodes.mjs';
32
+ import { isStateDriven, nodesOf, updatedVectorOf } from './ir/nodes.mjs';
33
+ import { nodeReads, vectorReads } from './ir/reads.mjs';
32
34
  /*----------------------------------------------------------------------------------------------------------------------
33
35
  - Implementation
34
36
  ---------------------------------------------------------------------------------------------------------------------*/
@@ -100,6 +102,22 @@ function settleFunctions(out, hasUpdate) {
100
102
  }
101
103
  }
102
104
  }
105
+ /**
106
+ * Flags each state a node or drawing reads, so a write to it marks it changed and app_update() sets them again.
107
+ * A node that also reads something untracked is set on every update, so its reads need no flag.
108
+ *
109
+ * @param nodes The app's nodes.
110
+ */
111
+ function watchState(nodes) {
112
+ for (const node of nodes) {
113
+ const vector = updatedVectorOf(node);
114
+ for (const reads of [nodeReads(node), vector && vectorReads(vector)]) {
115
+ reads?.forEach(record => {
116
+ record.isWatched = true;
117
+ });
118
+ }
119
+ }
120
+ }
103
121
  /**
104
122
  * Refuses state C cannot hold: a number in a list item that has no C form, and a host value that would take
105
123
  * the name of the setter every app exports for the wall clock. A field an item leaves out starts at 0.
@@ -138,10 +156,16 @@ function checkStateRecords(stateRecords) {
138
156
  * @returns The lowered app.
139
157
  */
140
158
  export function lowerApp(src, opts, wide, stores) {
141
- const { result, checks } = lowerWithChecks(() => lowerProgram(src, opts, wide, stores));
142
- const app = { ...result, checks };
143
- runChecks(app.checks);
144
- return app;
159
+ beginBoot();
160
+ try {
161
+ const { result, checks } = lowerWithChecks(() => lowerProgram(src, opts, wide, stores));
162
+ const app = { ...result, boot: endBoot(), checks };
163
+ runChecks(app.checks);
164
+ return app;
165
+ }
166
+ finally {
167
+ abandonBoot();
168
+ }
145
169
  }
146
170
  /**
147
171
  * Runs the compiler passes over the parsed app, from its module scope and hooks through the element tree to its
@@ -181,7 +205,7 @@ function lowerProgram(src, opts, wide, stores) {
181
205
  if (decl.id.type !== 'Identifier' || !decl.init)
182
206
  continue;
183
207
  try {
184
- scope[decl.id.name] = evalStatic(decl.init, withUndefined(scope));
208
+ scope[decl.id.name] = bindConst(decl.id.name, evalStatic(decl.init, withUndefined(scope), 'boot'));
185
209
  }
186
210
  catch {
187
211
  // A dynamic const must hide a same-named module const, or the JSX quietly renders the module value.
@@ -229,10 +253,12 @@ function lowerProgram(src, opts, wide, stores) {
229
253
  stores,
230
254
  shadowedClock,
231
255
  };
256
+ // A value worked out at boot lowers its slot against the app's environment.
257
+ setBootLowering(env, lowerExpr);
232
258
  // Before emit, fold each memo or inline its C expression at each use (no cache needed: dep tracking re-applies).
233
259
  for (const [name, expr] of memos) {
234
260
  try {
235
- scope[name] = evalStatic(expr, scope);
261
+ scope[name] = bindConst(name, evalStatic(expr, scope, 'boot'));
236
262
  }
237
263
  catch {
238
264
  const cExpr = emitSourcedWide(expr, env);
@@ -262,8 +288,11 @@ function lowerProgram(src, opts, wide, stores) {
262
288
  lowerEffect(effect, env, state, out);
263
289
  }
264
290
  // app_update() exists only when something has to be re-applied after a state change.
265
- const hasUpdate = nodesOf(root).some(node => isStateDriven(node) || stateDrivenVectorOf(node) !== null) || out.depEffects.length > 0;
291
+ const hasUpdate = nodesOf(root).some(node => isStateDriven(node) || updatedVectorOf(node) !== null) || out.depEffects.length > 0;
266
292
  settleFunctions(out, hasUpdate);
293
+ if (hasUpdate) {
294
+ watchState(nodesOf(root));
295
+ }
267
296
  const stateRecords = [...state.byName.values(), ...out.childStateRecords];
268
297
  checkStateRecords(stateRecords);
269
298
  return {
@@ -19,6 +19,7 @@
19
19
  import { checkNumber } from './check.mjs';
20
20
  import { NODE_TYPES } from '../style-map.mjs';
21
21
  import { aotError, withLoc } from './diagnostics.mjs';
22
+ import { buildValueOf, foldBoot } from './boot.mjs';
22
23
  import { evalStaticOrThrow, evalStatic, foldScope } from './static-eval.mjs';
23
24
  import { lowerExpr } from './expressions.mjs';
24
25
  import { isFn } from './collect.mjs';
@@ -648,7 +649,11 @@ function omitUndefinedAttrs(element, scope, env) {
648
649
  undefined,
649
650
  });
650
651
  try {
651
- return evalStatic(attr.value.expression, foldingScope) !== undefined;
652
+ const value = evalStatic(attr.value.expression, foldingScope, 'boot');
653
+ if (buildValueOf(value) !== undefined)
654
+ return true;
655
+ foldBoot(value); // undefined on this panel only: omitting it assumes the panel
656
+ return false;
652
657
  }
653
658
  catch {
654
659
  return true; // not a compile-time value — its reader decides