embedded-react 0.12.0 → 0.14.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.
Files changed (38) hide show
  1. package/README.md +222 -6
  2. package/aot/compile.mjs +3316 -364
  3. package/aot/style-map.mjs +30 -6
  4. package/assets/bake-font.mjs +104 -12
  5. package/assets/bake-svg.mjs +11 -1
  6. package/assets/build-builtin-font.mjs +6 -12
  7. package/assets/emit-c.mjs +10 -2
  8. package/assets/emit-container.mjs +4 -2
  9. package/assets/font-config.mjs +49 -0
  10. package/assets/font-sizes.mjs +652 -0
  11. package/assets/glyph-coverage.mjs +281 -0
  12. package/assets/index.mjs +29 -5
  13. package/build.mjs +11 -21
  14. package/cli.mjs +41 -3
  15. package/compile-bin.mjs +71 -0
  16. package/hotreload/pack-app.mjs +8 -21
  17. package/hotreload/pack-split.mjs +12 -27
  18. package/pack-container.mjs +10 -47
  19. package/package.json +2 -1
  20. package/sim/embedded-react.wasm +0 -0
  21. package/sim/index.html +32 -3
  22. package/sim-server.mjs +33 -27
  23. package/src/embedded-react/Animated.js +23 -9
  24. package/src/embedded-react/Button.js +98 -0
  25. package/src/embedded-react/FlatList.js +88 -0
  26. package/src/embedded-react/ImageBackground.js +67 -0
  27. package/src/embedded-react/PanResponder.js +326 -0
  28. package/src/embedded-react/SectionList.js +148 -0
  29. package/src/embedded-react/TouchableOpacity.js +152 -0
  30. package/src/embedded-react/components.js +4 -3
  31. package/src/embedded-react/index.d.ts +867 -60
  32. package/src/embedded-react/index.js +7 -2
  33. package/src/embedded-react/list-child.js +54 -0
  34. package/src/embedded-react/svg-ops.js +309 -77
  35. package/src/embedded-react/warn-props.js +80 -0
  36. package/src/host-config.js +103 -3
  37. package/src/props.js +40 -0
  38. package/src/renderer.js +31 -0
@@ -35,12 +35,90 @@ import {
35
35
  } from './embedded-react/svg-ops.js';
36
36
  import {splitAnimatedStyle} from './embedded-react/split-style.js';
37
37
 
38
+ // --- Animated bindings ---------------------------------------------------------------------------
39
+ // A bound prop is owned by the engine from then on: it re-pushes the animated value over the static
40
+ // props of every later setProps (er_anim_reapply_bound), which is what keeps a re-render from jumping
41
+ // a running animation back to its declared value. Nothing in React says "this prop stopped being
42
+ // animated", so the binding has to be released explicitly or it keeps writing the prop forever — and a
43
+ // prop that swapped Animated.Values ends up written by both. Instances are bare node handles with no
44
+ // JS object to hang state on, so what we bound is remembered here: handle → (prop → animated value).
45
+ const boundValues = new Map();
46
+
47
+ /**
48
+ * True when two animated values bind a prop the same way, so a re-render can leave the live binding
49
+ * alone. Identity settles an Animated.Value; an interpolation is a throwaway object that
50
+ * `value.interpolate(...)` rebuilds on every render, so it answers __bindEq for itself.
51
+ */
52
+ function sameBinding(prev, next) {
53
+ if (prev === next) return true;
54
+ if (prev == null || next == null) return false;
55
+ return typeof prev.__bindEq === 'function' && prev.__bindEq(next);
56
+ }
57
+
58
+ /** The value `bindings` (from splitAnimatedStyle) drives `prop` with, or undefined if none does. */
59
+ function bindingFor(bindings, prop) {
60
+ for (const b of bindings) {
61
+ if (b.prop === prop) return b.value;
62
+ }
63
+ return undefined;
64
+ }
65
+
66
+ /**
67
+ * Releases the bindings a node has that its new props no longer ask for — a prop that went back to a
68
+ * static value, or moved to a different Animated.Value.
69
+ *
70
+ * Runs BEFORE the node's setProps, and that order is the whole point: setProps re-pushes every value
71
+ * still bound to the node over the static props it just wrote, so a binding released afterwards would
72
+ * leave its last animated float sitting on the node instead of the value React just set.
73
+ */
74
+ function releaseStaleBindings(handle, bindings) {
75
+ const bound = boundValues.get(handle);
76
+ if (bound === undefined) return;
77
+ for (const [prop, value] of bound) {
78
+ if (sameBinding(value, bindingFor(bindings, prop))) continue;
79
+ NativeUI.animUnbind(handle, prop);
80
+ bound.delete(prop);
81
+ }
82
+ if (bound.size === 0) boundValues.delete(handle);
83
+ }
84
+
85
+ /**
86
+ * Binds the props that aren't already bound to the value asking for them. The engine ignores a
87
+ * duplicate bind, but the bridge crossing to find that out isn't free on an MCU.
88
+ */
89
+ function applyBindings(handle, bindings) {
90
+ if (bindings.length === 0) return;
91
+ let bound = boundValues.get(handle);
92
+ for (const b of bindings) {
93
+ if (bound !== undefined && sameBinding(bound.get(b.prop), b.value))
94
+ continue;
95
+ b.value.__bind(handle, b.prop);
96
+ if (bound === undefined) {
97
+ bound = new Map();
98
+ boundValues.set(handle, bound);
99
+ }
100
+ bound.set(b.prop, b.value);
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Forgets what a handle was bound to. The engine releases the bindings itself when a node is destroyed
106
+ * (er_anim_unbind_node), so this only drops our record — but it has to happen, because handles are
107
+ * recycled: a record left behind would make the next node to claim that handle look already-bound.
108
+ * Called on destroy for the node React hands us, and again on create because destroyNode reclaims a
109
+ * whole SUBTREE in C — React never mentions the descendants, so their handles come back from the free
110
+ * list still carrying a record.
111
+ */
112
+ function forgetBindings(handle) {
113
+ boundValues.delete(handle);
114
+ }
115
+
38
116
  /**
39
117
  * Applies a node's resolved props, binding any Animated.Value found in its `style` to the matching
40
118
  * node prop (native driver). This makes animated styles work on ANY host element — `<Pressable
41
119
  * style={{ transform: [{ scale: v }] }}>` binds without an Animated.* wrapper — which is what the
42
- * Flow B AOT compiler does too, so the two render paths stay in parity. An Animated.* wrapper has
43
- * already stripped its bindings into a ref, so splitAnimatedStyle finds none here (no double bind).
120
+ * Flow B AOT compiler does too, so the two render paths stay in parity (and what an Animated.* wrapper
121
+ * relies on: it forwards its style untouched and is bound here like any other element).
44
122
  *
45
123
  * Returns the resolved flat style so a paired applyTextSpans call can reuse it instead of
46
124
  * re-flattening props.style from scratch — style flattening (recursive array walk and object merge)
@@ -48,8 +126,21 @@ import {splitAnimatedStyle} from './embedded-react/split-style.js';
48
126
  */
49
127
  function applyProps(type, handle, props) {
50
128
  const {staticStyle, bindings} = splitAnimatedStyle(props.style);
129
+ // <Dial value={animatedValue}>: the value is a native-driver binding (ER_PROP_ARC_VALUE), not a prop —
130
+ // the engine ramps it with no JS per frame and damages only the swept sliver.
131
+ if (type === 'Dial') {
132
+ if (props.value && props.value.__animated) {
133
+ bindings.push({prop: 'value', value: props.value});
134
+ props = {...props, value: undefined};
135
+ }
136
+ if (props.valueStart && props.valueStart.__animated) {
137
+ bindings.push({prop: 'valueStart', value: props.valueStart});
138
+ props = {...props, valueStart: undefined};
139
+ }
140
+ }
141
+ releaseStaleBindings(handle, bindings);
51
142
  NativeUI.setProps(handle, buildProps(type, props, staticStyle));
52
- for (const b of bindings) b.value.__bind(handle, b.prop);
143
+ applyBindings(handle, bindings);
53
144
  return staticStyle;
54
145
  }
55
146
 
@@ -276,10 +367,15 @@ export const hostConfig = {
276
367
  },
277
368
 
278
369
  // --- Commit lifecycle ---
370
+ // These two bracket React's mutation phase exactly, which is the only place the reconciler touches
371
+ // the bridge — so on an instrumented build they are what makes the marshaling bucket cover the JS
372
+ // glue (applyProps, style flattening) and not just the NativeUI calls it ends up making.
279
373
  prepareForCommit() {
374
+ NativeUI.perfMarshalBegin?.();
280
375
  return null;
281
376
  },
282
377
  resetAfterCommit() {
378
+ NativeUI.perfMarshalEnd?.();
283
379
  NativeUI.commit();
284
380
  },
285
381
 
@@ -289,11 +385,13 @@ export const hostConfig = {
289
385
  const raster = rasterSvgArtifact(type, props);
290
386
  if (raster) {
291
387
  const handle = NativeUI.createNode('Image');
388
+ forgetBindings(handle);
292
389
  applyProps('Image', handle, rasterImageProps(props, raster));
293
390
  applyEvents(handle, null, props);
294
391
  return handle;
295
392
  }
296
393
  const handle = NativeUI.createNode(type);
394
+ forgetBindings(handle);
297
395
  const flatStyle = applyProps(type, handle, props);
298
396
  applyTextSpans(type, handle, props, flatStyle);
299
397
  applyVectorOps(type, handle, props);
@@ -338,10 +436,12 @@ export const hostConfig = {
338
436
  removeChild(parent, child) {
339
437
  NativeUI.removeChild(parent, child);
340
438
  NativeUI.destroyNode(child);
439
+ forgetBindings(child);
341
440
  },
342
441
  removeChildFromContainer(container, child) {
343
442
  NativeUI.removeChild(container, child);
344
443
  NativeUI.destroyNode(child);
444
+ forgetBindings(child);
345
445
  },
346
446
  clearContainer() {
347
447
  // Children are removed individually via removeChildFromContainer.
package/src/props.js CHANGED
@@ -20,17 +20,47 @@
20
20
  // Top-level props NativeUI understands directly (not part of `style`). Everything else style-ish is
21
21
  // expected inside props.style. Event handlers (on*) are routed separately via setEvent.
22
22
  export const PASSTHROUGH = [
23
+ 'delayLongPress',
23
24
  'numberOfLines',
24
25
  'ellipsizeMode',
25
26
  'value',
26
27
  'placeholder',
27
28
  'placeholderTextColor',
28
29
  'editable',
30
+ 'secureTextEntry',
29
31
  'animating',
30
32
  'visible',
33
+ 'backdropColor', // <Modal>'s scrim, as React Native takes it (the style key works too)
31
34
  'resizeMode',
32
35
  'tintColor',
33
36
  'imageName',
37
+ // Switch colours.
38
+ 'trackColor',
39
+ 'thumbColor',
40
+ // <Dial> (the native arc widget, ER_NODE_ARC).
41
+ 'min',
42
+ 'max',
43
+ 'startAngle',
44
+ 'sweepAngle',
45
+ 'step',
46
+ 'gapAngle',
47
+ 'thickness',
48
+ 'bandThickness',
49
+ 'knobSize',
50
+ 'knobBorderWidth',
51
+ 'indicatorColor',
52
+ 'bandColor',
53
+ 'knobColor',
54
+ 'knobBorderColor',
55
+ 'segments',
56
+ 'cap',
57
+ 'knob',
58
+ 'knobImage',
59
+ 'adjustable',
60
+ 'range',
61
+ 'valueStart',
62
+ 'minSpan',
63
+ 'indicatorGradient',
34
64
  ];
35
65
 
36
66
  /**
@@ -168,6 +198,16 @@ export function buildProps(type, props, flatStyle) {
168
198
  const name = resolveImageSource(props.source);
169
199
  if (name != null) flat.imageName = name;
170
200
  }
201
+ // <Dial knobImage={require(...)}> takes an asset source like <Image source>; a plain string is an
202
+ // asset name already.
203
+ if (
204
+ type === 'Dial' &&
205
+ props.knobImage != null &&
206
+ typeof props.knobImage !== 'string'
207
+ ) {
208
+ const name = resolveImageSource(props.knobImage);
209
+ flat.knobImage = name != null ? name : undefined;
210
+ }
171
211
  // `visible` is the Modal's own show/hide prop (it maps to ERProps.modal_visible). On every other
172
212
  // component it used to be silently inert; it now means the same thing as `display`, so a subtree can
173
213
  // be pruned from layout, raster and hit-testing without unmounting it. An explicit style `display`
package/src/renderer.js CHANGED
@@ -27,6 +27,37 @@ const reconciler = Reconciler(hostConfig);
27
27
 
28
28
  const LegacyRoot = 0;
29
29
 
30
+ // Host-initiated updates — a native event, a timer, a promise continuation — are outside any React
31
+ // batch, so in LegacyRoot mode each setState renders and commits on its own: three animations
32
+ // ticking on the same frame cost three renders and three engine commits to paint one frame. The
33
+ // bridge runs the frame pump — and each native event — inside this wrapper, which puts everything a
34
+ // frame does in one batch and leaves it as one render and one commit. Installed here rather than at
35
+ // createRoot() so the reconciler that batches is the one that renders.
36
+ // On an instrumented build the batcher is also where the JS phase's split gets drawn (er_perf.h):
37
+ // `fn(a, b)` is the callback, and whatever batchedUpdates does around it is React's render. That
38
+ // boundary is only visible from here — React flushes when it decides to, not reliably at the batch
39
+ // call the C side can see — so marking it in C measured the call overhead and left reconcile at ~0.
40
+ // The instrumented wrapper is chosen ONCE, here, so an ordinary build runs the plain one.
41
+ NativeUI.setBatcher?.(
42
+ NativeUI.perfRenderBegin
43
+ ? (fn, a, b) => {
44
+ NativeUI.perfRenderBegin();
45
+ try {
46
+ return reconciler.batchedUpdates(() => {
47
+ NativeUI.perfCallbackBegin();
48
+ try {
49
+ return fn(a, b);
50
+ } finally {
51
+ NativeUI.perfCallbackEnd();
52
+ }
53
+ });
54
+ } finally {
55
+ NativeUI.perfRenderEnd();
56
+ }
57
+ }
58
+ : (fn, a, b) => reconciler.batchedUpdates(() => fn(a, b)),
59
+ );
60
+
30
61
  /**
31
62
  * Creates a root bound to a screen-sized container node.
32
63
  *