embedded-react 0.10.0 → 0.10.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "embedded-react",
3
- "version": "0.10.0",
3
+ "version": "0.10.2",
4
4
  "type": "module",
5
5
  "description": "React Native-style component package + reconciler that drives the embedded-react C engine through the QuickJS NativeUI bridge (Flow A).",
6
6
  "license": "Apache-2.0",
Binary file
@@ -23,6 +23,7 @@ import {NativeUI} from './native-ui.js';
23
23
  import {
24
24
  buildProps,
25
25
  buildTextSpans,
26
+ deepEqualProps,
26
27
  isEventProp,
27
28
  isTextContent,
28
29
  } from './props.js';
@@ -40,23 +41,28 @@ import {splitAnimatedStyle} from './embedded-react/split-style.js';
40
41
  * style={{ transform: [{ scale: v }] }}>` binds without an Animated.* wrapper — which is what the
41
42
  * Flow B AOT compiler does too, so the two render paths stay in parity. An Animated.* wrapper has
42
43
  * already stripped its bindings into a ref, so splitAnimatedStyle finds none here (no double bind).
44
+ *
45
+ * Returns the resolved flat style so a paired applyTextSpans call can reuse it instead of
46
+ * re-flattening props.style from scratch — style flattening (recursive array walk and object merge)
47
+ * is one of the costlier steps in commitUpdate's JS-side work on a bytecode-interpreted MCU target.
43
48
  */
44
49
  function applyProps(type, handle, props) {
45
50
  const {staticStyle, bindings} = splitAnimatedStyle(props.style);
46
- NativeUI.setProps(
47
- handle,
48
- buildProps(type, bindings.length ? {...props, style: staticStyle} : props),
49
- );
51
+ NativeUI.setProps(handle, buildProps(type, props, staticStyle));
50
52
  for (const b of bindings) b.value.__bind(handle, b.prop);
53
+ return staticStyle;
51
54
  }
52
55
 
53
56
  /**
54
57
  * Applies inline-styled text spans for a <Text> node (no-op for other types). buildTextSpans returns
55
58
  * [] for uniform text, which reverts the node to plain-text rendering — so this also clears stale
56
- * spans when a Text changes from styled-runs to a single style across renders.
59
+ * spans when a Text changes from styled-runs to a single style across renders. `flatStyle`, when
60
+ * given, is the already-resolved style from a paired applyProps call on the same props (avoids a
61
+ * second full flattening of props.style).
57
62
  */
58
- function applyTextSpans(type, handle, props) {
59
- if (type === 'Text') NativeUI.setTextSpans(handle, buildTextSpans(props));
63
+ function applyTextSpans(type, handle, props, flatStyle) {
64
+ if (type === 'Text')
65
+ NativeUI.setTextSpans(handle, buildTextSpans(props, flatStyle));
60
66
  }
61
67
 
62
68
  /** Resolves an <Svg>'s render-box dimension from style/props, falling back to the source's intrinsic size. */
@@ -132,20 +138,12 @@ function applyVectorOps(type, handle, props) {
132
138
  }
133
139
 
134
140
  /**
135
- * Decides whether a committed <Svg> update actually needs its op-tape re-uploaded.
136
- *
137
- * Re-marshaling a baked vector op-tape across the JS->C bridge every frame is expensive (it dominates an
138
- * interactive drag on PSRAM-QuickJS), and it's pure waste when only the node's POSITION changed: a
139
- * `<Svg source>` whose imported artifact and resolved box are unchanged renders identical geometry, and its
140
- * on-screen movement is already handled by the layout props (left/top/width/height) via applyProps. So we
141
- * re-upload only when the source artifact reference changes or the resolved box size changes. Declarative
142
- * `<Svg><Path/></Svg>` has no `source` — its shapes live in props/children, which we can't cheaply diff
143
- * here, so it always re-flattens (unchanged behavior).
141
+ * True when a `<Svg source={vectorArtifact}>`'s resolved render box changed between renders — the
142
+ * one style-driven input to the op-tape (the tape is scaled from the artifact's intrinsic px to the
143
+ * box, see scaleVectorArtifact). A style change that leaves the box alone (a position move during a
144
+ * drag, a color tweak) does not need the tape re-uploaded.
144
145
  */
145
- function vectorNeedsUpload(type, prevProps, nextProps) {
146
- if (type !== 'Svg') return false;
147
- if (!nextProps.source) return true; // declarative children: re-flatten as before
148
- if (!prevProps || prevProps.source !== nextProps.source) return true;
146
+ function svgSourceBoxChanged(prevProps, nextProps) {
149
147
  const src = nextProps.source;
150
148
  const iw = src && src.kind === 'vector' ? src.width : undefined;
151
149
  const ih = src && src.kind === 'vector' ? src.height : undefined;
@@ -155,6 +153,88 @@ function vectorNeedsUpload(type, prevProps, nextProps) {
155
153
  );
156
154
  }
157
155
 
156
+ /**
157
+ * Diffs a re-rendered host element's props and returns what commitUpdate must re-apply — or null
158
+ * when nothing observable changed, which makes React skip the commit entirely (no Update effect, no
159
+ * bridge traffic). Re-marshaling props across the JS->C bridge is the dominant cost of a Flow A
160
+ * re-render, and a parent's state change re-renders every non-memoized child with identical props,
161
+ * so the null case is the common one on interactive screens.
162
+ *
163
+ * The payload is a set of section flags, so a partial change re-applies only its section:
164
+ * props — NativeUI.setProps (resolved style + passthrough props + text)
165
+ * spans — inline text spans (<Text> only; derived from children + style)
166
+ * vector — the <Svg> op-tape upload (flattenSvg/scaleVectorArtifact + setVectorOps)
167
+ * events — on* handler registration (an inline closure has a new identity every render and must be
168
+ * re-stored — its captures went stale — but that alone shouldn't re-serialize the props)
169
+ *
170
+ * The vector flag tracks the op-tape's actual inputs. A source artifact tape depends on the source
171
+ * reference and the resolved box; a declarative <Svg><Path/></Svg> tape depends on the shape
172
+ * children, viewBox, and the direct width/height props (flattenSvg never reads style). So a
173
+ * position-only style move of either kind — the interactive-drag hot path — skips the upload.
174
+ *
175
+ * Props buildProps never reads still set the props flag when they change — conservative, and only
176
+ * paid when such a value actually changed.
177
+ */
178
+ function diffUpdate(type, prevProps, nextProps) {
179
+ let props = false;
180
+ let spans = false;
181
+ let events = false;
182
+ let vector = false;
183
+ const diffKey = key => {
184
+ const a = prevProps[key];
185
+ const b = nextProps[key];
186
+ if (Object.is(a, b)) return;
187
+ if (isEventProp(key, a) || isEventProp(key, b)) {
188
+ events = true;
189
+ return;
190
+ }
191
+ switch (key) {
192
+ case 'children':
193
+ // Only subtree-owning types render children through their OWN props (shouldSetTextContent);
194
+ // everywhere else children are separate host instances React mutates directly.
195
+ if (type === 'Text') {
196
+ if (!deepEqualProps(a, b)) {
197
+ props = true; // flat.text is built from children
198
+ spans = true;
199
+ }
200
+ } else if (type === 'Svg') {
201
+ if (!deepEqualProps(a, b)) vector = true;
202
+ }
203
+ return;
204
+ case 'style':
205
+ if (!deepEqualProps(a, b)) {
206
+ props = true;
207
+ if (type === 'Text') spans = true;
208
+ if (type === 'Svg' && nextProps.source)
209
+ vector = vector || svgSourceBoxChanged(prevProps, nextProps);
210
+ }
211
+ return;
212
+ case 'source':
213
+ props = true; // imageName (Image / raster Svg fallback)
214
+ if (type === 'Svg') vector = true; // artifact swap → new tape
215
+ return;
216
+ case 'width':
217
+ case 'height':
218
+ case 'viewBox':
219
+ if (type === 'Svg') {
220
+ props = true; // width/height fold into the resolved style box
221
+ vector = true; // and drive the tape's root transform / artifact scale
222
+ return;
223
+ }
224
+ // fall through: on other types these are ordinary (ignored or passthrough) props
225
+ default:
226
+ if (!deepEqualProps(a, b)) props = true;
227
+ }
228
+ };
229
+ for (const key in prevProps) diffKey(key);
230
+ for (const key in nextProps) {
231
+ if (!Object.prototype.hasOwnProperty.call(prevProps, key)) diffKey(key);
232
+ }
233
+ return props || spans || events || vector
234
+ ? {props, spans, events, vector}
235
+ : null;
236
+ }
237
+
158
238
  /**
159
239
  * Registers/clears on* event handlers. A handler present in old but not new props is cleared.
160
240
  */
@@ -214,8 +294,8 @@ export const hostConfig = {
214
294
  return handle;
215
295
  }
216
296
  const handle = NativeUI.createNode(type);
217
- applyProps(type, handle, props);
218
- applyTextSpans(type, handle, props);
297
+ const flatStyle = applyProps(type, handle, props);
298
+ applyTextSpans(type, handle, props, flatStyle);
219
299
  applyVectorOps(type, handle, props);
220
300
  applyEvents(handle, null, props);
221
301
  return handle;
@@ -266,23 +346,27 @@ export const hostConfig = {
266
346
  clearContainer() {
267
347
  // Children are removed individually via removeChildFromContainer.
268
348
  },
269
- prepareUpdate() {
270
- // Always re-apply: NativeUI.setProps is fully declarative, so a non-null payload is enough.
271
- return true;
349
+ prepareUpdate(instance, type, oldProps, newProps) {
350
+ // Runs in the render phase. null → React schedules no Update effect and commitUpdate never
351
+ // fires for this node — the win that makes an unchanged re-render bridge-free.
352
+ return diffUpdate(type, oldProps, newProps);
272
353
  },
273
- commitUpdate(instance, _payload, type, prevProps, nextProps) {
354
+ commitUpdate(instance, payload, type, prevProps, nextProps) {
274
355
  const raster = rasterSvgArtifact(type, nextProps);
275
356
  if (raster) {
276
357
  // The Svg instance is an Image node (raster fallback); re-apply as image props, never vector ops.
277
- applyProps('Image', instance, rasterImageProps(nextProps, raster));
278
- applyEvents(instance, prevProps, nextProps);
358
+ if (payload.props)
359
+ applyProps('Image', instance, rasterImageProps(nextProps, raster));
360
+ if (payload.events) applyEvents(instance, prevProps, nextProps);
279
361
  return;
280
362
  }
281
- applyProps(type, instance, nextProps);
282
- applyTextSpans(type, instance, nextProps);
283
- if (vectorNeedsUpload(type, prevProps, nextProps))
284
- applyVectorOps(type, instance, nextProps);
285
- applyEvents(instance, prevProps, nextProps);
363
+ // payload.spans is only ever set alongside payload.props for Text nodes (see diffUpdate), so
364
+ // flatStyle is defined whenever applyTextSpans actually needs it.
365
+ let flatStyle;
366
+ if (payload.props) flatStyle = applyProps(type, instance, nextProps);
367
+ if (payload.spans) applyTextSpans(type, instance, nextProps, flatStyle);
368
+ if (payload.vector) applyVectorOps(type, instance, nextProps);
369
+ if (payload.events) applyEvents(instance, prevProps, nextProps);
286
370
  },
287
371
  commitTextUpdate(textInstance, _oldText, newText) {
288
372
  NativeUI.setProps(textInstance, {text: String(newText)});
package/src/props.js CHANGED
@@ -118,9 +118,9 @@ export function flattenTextChildren(children, baseStyle) {
118
118
  * is uniformly the base style (plain text — no spans needed); otherwise one span per styled segment,
119
119
  * each carrying only the span-relevant style keys (the engine inherits the rest from the node).
120
120
  */
121
- export function buildTextSpans(props) {
121
+ export function buildTextSpans(props, flatStyle) {
122
122
  if (!isTextContent(props.children)) return [];
123
- const baseStyle = flattenStyleObj(props.style);
123
+ const baseStyle = flatStyle || flattenStyleObj(props.style);
124
124
  const segments = flattenTextChildren(props.children, baseStyle);
125
125
  if (!segments.some(s => s.style !== baseStyle)) return [];
126
126
  return segments.map(s => {
@@ -148,9 +148,9 @@ export function resolveImageSource(source) {
148
148
  return null;
149
149
  }
150
150
 
151
- export function buildProps(type, props) {
152
- const flat = {};
153
- flattenStyle(props.style, flat);
151
+ export function buildProps(type, props, flatStyle) {
152
+ const flat = flatStyle ? {...flatStyle} : {};
153
+ if (!flatStyle) flattenStyle(props.style, flat);
154
154
  for (const k of PASSTHROUGH) {
155
155
  if (props[k] !== undefined) flat[k] = props[k];
156
156
  }
@@ -172,7 +172,7 @@ export function buildProps(type, props) {
172
172
  // The engine renders this when no spans are set; with spans (buildTextSpans) it carries the same
173
173
  // text for the plain-text fallback. Non-flattenable children fall back to mounted child instances.
174
174
  if (type === 'Text' && isTextContent(props.children)) {
175
- const base = flattenStyleObj(props.style);
175
+ const base = flatStyle || flattenStyleObj(props.style);
176
176
  flat.text = flattenTextChildren(props.children, base)
177
177
  .map(s => s.text)
178
178
  .join('');
@@ -180,6 +180,60 @@ export function buildProps(type, props) {
180
180
  return flat;
181
181
  }
182
182
 
183
+ // Bail-out depth for deepEqualProps. Real host props are shallow (style ≈ 2 levels, a nested-<Text>
184
+ // span or <Svg> shape tree ≈ 3 levels per element); anything deeper is pathological and bails to
185
+ // "changed", whose worst case is a redundant re-apply — exactly the pre-diff behavior.
186
+ const DEEP_EQUAL_MAX_DEPTH = 24;
187
+
188
+ /**
189
+ * Deep value equality for host-prop diffing (the reconciler's prepareUpdate). Re-renders recreate
190
+ * prop objects every time, so identity alone can't tell "nothing changed" — this compares by value:
191
+ * primitives via Object.is, arrays elementwise, React elements by type and props (keys don't affect a
192
+ * flattened <Text>/<Svg> subtree's output), and plain objects key-by-key. Class instances (e.g.,
193
+ * Animated.Value) compare by IDENTITY only: a fresh instance must read as changed, so it gets bound
194
+ * to the node, while the same instance is already bound and can be skipped.
195
+ */
196
+ export function deepEqualProps(a, b, depth = 0) {
197
+ if (Object.is(a, b)) return true;
198
+ if (depth >= DEEP_EQUAL_MAX_DEPTH) return false;
199
+ if (
200
+ typeof a !== 'object' ||
201
+ typeof b !== 'object' ||
202
+ a === null ||
203
+ b === null
204
+ )
205
+ return false;
206
+ if (Array.isArray(a) || Array.isArray(b)) {
207
+ if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length)
208
+ return false;
209
+ for (let i = 0; i < a.length; i++) {
210
+ if (!deepEqualProps(a[i], b[i], depth + 1)) return false;
211
+ }
212
+ return true;
213
+ }
214
+ // React elements: equal when they render the same host output — same type, same props.
215
+ if (a.$$typeof !== b.$$typeof) return false;
216
+ if (a.$$typeof !== undefined) {
217
+ return a.type === b.type && deepEqualProps(a.props, b.props, depth + 1);
218
+ }
219
+ // Plain objects only; class instances fall through to "changed" (identity already ruled equal out).
220
+ const protoA = Object.getPrototypeOf(a);
221
+ const protoB = Object.getPrototypeOf(b);
222
+ if (
223
+ (protoA !== null && protoA !== Object.prototype) ||
224
+ (protoB !== null && protoB !== Object.prototype)
225
+ )
226
+ return false;
227
+ const keysA = Object.keys(a);
228
+ const keysB = Object.keys(b);
229
+ if (keysA.length !== keysB.length) return false;
230
+ for (const k of keysA) {
231
+ if (!Object.prototype.hasOwnProperty.call(b, k)) return false;
232
+ if (!deepEqualProps(a[k], b[k], depth + 1)) return false;
233
+ }
234
+ return true;
235
+ }
236
+
183
237
  /**
184
238
  * True for an `onXxx` prop whose value is a function (an event handler to route via setEvent).
185
239
  */